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()
})
複製代碼