安裝echarts依賴
npm install echarts -S
或者使用淘寶的鏡像
npm install -g cnpm --registry=[https://registry.npm.taobao.org](https://registry.npm.taobao.org/)
cnpm install echarts -S
創(chuàng)建圖表
首先需要全局引入
在main.js中
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
在Echarts.vue中
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
export default {
name: 'hello',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted(){
this.drawLine();
},
methods: {
drawLine(){
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
let myChart = this.$echarts.init(document.getElementById('myChart'))
// 繪制圖表
myChart.setOption({
title: { text: '在Vue中使用echarts' },
tooltip: {},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
}
}
注意:我們要在mounted生命周期函數(shù)中實(shí)例化echarts對象。因?yàn)槲覀円_保dom元素已經(jīng)掛載到頁面中

image.png
這樣一個(gè)簡單的圖表就完成了,是不是覺得很簡單?假如在一個(gè)大型的項(xiàng)目中,而且數(shù)據(jù)是非常復(fù)雜的?那么該如何操作?