第一步:在main中引入
npm install echarts --save
import * as echarts from 'echarts'
Vue.prototype.echarts = echarts
第二步在目标组件中使用
data() {
return {
option:{ // 图表配置项
toolbox:{
right:'3%',
feature: {
saveAsImage: {
},
}
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search']
},
grid: {
left: '4%',
right: '5%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Email',
type: 'line',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Union Ads',
type: 'line',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Video Ads',
type: 'line',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Direct',
type: 'line',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'Search',
type: 'line',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
}
}
},
第三步
//在生命周期中
mounted() {
this.drawLine()
},
//绘图
methods:{
// 绘制收入折线图
drawLine(){
// 基于准备好的dom,初始化echarts实例
let myChart = this.echarts.init(document.getElementById('myChart'))
// 绘制图表
myChart.setOption(this.option)
}
}