1.安装echarts依赖
npm install echarts -S
2.在main.js中全局引入
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
3.具体代码
<template>
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
</template>
export default {
name: 'Dashboard',
data () {
return {
}
},
mounted(){
this.lineechars();
},
methods: {
lineechars(){
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById('myChart'))
// 绘制图表
myChart.setOption({
title: { text: '在Vue中使用echarts' },
tooltip: {},
xAxis: {
data: ["球鞋","羽毛球拍","篮球","滑板","乒乓球拍","溜冰鞋"]
},
yAxis: {},
series: [{
name: '价格',
type: 'bar',
data: [35, 220, 316, 60, 80, 90]
}]
});
}
}
}
注意:我们要在mounted生命周期函数中实例化echarts对象。因为我们要确保dom元素已经挂载到页面中。