Vue中使用echarts

本文介绍Vue中如何使用echarts

1安装

代码如下:

1
2

npm install echarts --save

2按需引入

1
2
3
4
5
6
7
8
9
10
11
12

import echarts from 'echarts/lib/echarts'
// 引入饼状图
import 'echarts/lib/chart/pie'
// 引入折线图
import 'echarts/lib/chart/line'
// 引入柱状图
import 'echarts/lib/chart/bar'
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip'
import 'echarts/lib/component/title'
import 'echarts/lib/component/legend'

3使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

drawLine(x,y1,y2){
const myChart=echarts.init(document.getElementById('line'))
myChart.setOption({
backgroundColor: "#fff",
grid: {
left: '20px',
right: '20px',
bottom: '20px',
containLabel: true
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['收入', '支出'],
right: '20px',
top: '20px'
},
xAxis: {
type: 'category',
data: x,
axisTick: {
show: false
},
axisLine:{
lineStyle:{
color: "#cccccc"
}
}
},
yAxis: {
type: 'value',
axisLine: {
show: false,
lineStyle:{
color: "#cccccc"
}
},
axisTick: {
show: false
},
},
series: [{
data: y1,
name: '收入',
type: 'line',
smooth:true,
symbol: 'circle',
symbolSize: 5,
itemStyle: {
color: '#67C23A',
lineStyle: {
color: '#67C23A',
}
},
},{
data: y2,
name: '支出',
type: 'line',
smooth:true,
symbol: 'circle',
symbolSize: 5,
itemStyle: {
color: '#E6A23C',
lineStyle: {
color: '#E6A23C',
}
},
}]
});
myChart.on('click', function (params) { //图表绑定点击事件
alert(params.name)
});
}

注意:渲染图标的外部容器必须指定宽高

扫一扫,请老师喝水