nnpm install echarts --save
复制代码
1.全局引入Echarts(入口文件main.js),且将实例挂载到vue上vue
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
复制代码
<!--先引入Echarts-->
import echarts from 'echarts'
<!--定义图表信息 在data中定义,也能够外部定义,再引入-->
charts4: {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
<!--定义图表大小-->
grid: {
left: '10%',
right: '4%',
bottom: '-1%',
containLabel: true
},
<!--X轴-->
xAxis: {
type: 'value',
<!--为false,则不显示X轴坐标系 若是X轴有文字,则会都隐藏-->
show: false
},
<!--Y轴-->
yAxis: {
type: 'category',
<!--Y轴显示的数据-->
data: ['南京', '长沙', '成都', '济南', '杭州', '合肥', '深州', '北京', '广州', '上海'],
<!--轴线设置-->
axisLine: {
lineStyle: {
color: '#0c78d7'
},
show: false
},
<!--轴坐标设置-->
axisLabel: {
<!--文字设置-->
textStyle: {
fontSize: '10'
}
},
<!--坐标轴设置-->
axisTick: {
show: false
}
},
<!--图表数据的一系列设置-->
series: [
{
type: 'bar',
data: [60, 70, 55, 65, 70, 75, 60, 80, 95, 100],
itemStyle: {
normal: {
<!--设置颜色渐变-->
color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
offset: 1,
color: '#1db7fc'
}, {
offset: 0.5,
color: '#039de3'
}, {
offset: 0,
color: '#02587f'
}]),
shadowColor: 'rgba(0, 0, 0, 0.4)'
}
},
<!--braGap用于设置同一个类目内的柱形之间的间距,而barCategoryGap则用于设置不一样类目之间的间距-->
barCategoryGap: '50%'
}
]
}
复制代码
<!--Echarts初始化, 其中this.$refs.xl是获取的dom元素-->
var myChart0 = this.$echarts.init(this.$refs.xl)
<!--执行Echarts方法-->
myChart0.setOption(this.charts0)
<!--监听图表的变化-->
window.addEventListener('resize', function () {
myChart0.resize()
})
复制代码