Vue中Echart用法

一、先下载echarts

1
cnpm install echarts --save-dev

二、在main.js中引入

1
2
3
import echarts from 'echarts'

Vue.prototype.$echarts = echarts

三、新建hours.vue组件

1
2
3
4
5
6
7
<div>
<div id="myChart" :style="{ height: '140px'}"></div>
<p class="title">伦敦金</p>
<p class="percent1">{{percent1}}.00%</p>
<p class="percent2">{{percent2}}.00%</p>
<p class="time">数据更新时间:{{year}}&nbsp;{{time}}</p>
</div>

四、在methods里面写这段代码

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 mounted() {
this.drawLine(); //执行下面的函数
},
methods: {
drawLine() {
// var i=0;
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById("myChart"));
var color = ["#19D672", "#FD517D"];
// 绘制图表
myChart.setOption({
color: ["#23D864", "#FF4D51"],

// title: { text: '伦敦金' },

tooltip: {
trigger: "item",
formatter: "{a} <br/>{b}: {c} ({d}%)"
},

legend: [
{
// orient: "vertical",
itemWidth: 15,
x: "2%",
y: "20%",

textStyle: {
// color: '#ccc',
},

data: ["买跌"]
},
{
itemWidth: 15,

// orient: "vertical",
x: "80%",
y: "20%",
data: ["买涨"]
}
],
series: [
{
name: "访问来源",
type: "pie",
data: ["20%", "80%"],
radius: ["42%", "50%"],
center: ["50%", "35%"],
avoidLabelOverlap: false,
label: {
normal: {
show: false,
position: "center"
},

emphasis: {
show: true,
textStyle: {
fontSize: "0",
fontWeight: "bold"
}
}
},
labelLine: {
normal: {
show: false
}
},

itemStyle: {
top: "50"

// borderWidth: 50, //设置border的宽度有多大
},

data: [
{
value: this.percent1,
name: "买跌",
a: "1"
},
{
value: this.percent2,
name: "买涨",
a: "1"
}
]
}
]
});
}
}

五、整体代码

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<template>
<div>

<div>
<div id="myChart" :style="{ height: '140px'}"></div>
<p class="title">伦敦金</p>
<p class="percent1">{{percent1}}.00%</p>
<p class="percent2">{{percent2}}.00%</p>
<p class="time">数据更新时间:{{year}}&nbsp;{{time}}</p>
</div>
<pieChart />

</div>
</template>

<script>
import pieChart from "./pieChart.vue";

export default {
components: {
pieChart
},
data() {
return {
percent1: 30,
percent2: 70,
year: "2018-10-20",
time: "10:05:03"
};
},
mounted() {
this.drawLine();
},
methods: {
drawLine() {
// var i=0;
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById("myChart"));
var color = ["#19D672", "#FD517D"];
// 绘制图表
myChart.setOption({
color: ["#23D864", "#FF4D51"],

// title: { text: '伦敦金' },

tooltip: {
trigger: "item",
formatter: "{a} <br/>{b}: {c} ({d}%)"
},

legend: [
{
// orient: "vertical",
itemWidth: 15,
x: "2%",
y: "20%",

textStyle: {
// color: '#ccc',
},

data: ["买跌"]
},
{
itemWidth: 15,

// orient: "vertical",
x: "80%",
y: "20%",
data: ["买涨"]
}
],
series: [
{
name: "访问来源",
type: "pie",
data: ["20%", "80%"],
radius: ["42%", "50%"],
center: ["50%", "35%"],
avoidLabelOverlap: false,
label: {
normal: {
show: false,
position: "center"
},

emphasis: {
show: true,
textStyle: {
fontSize: "0",
fontWeight: "bold"
}
}
},
labelLine: {
normal: {
show: false
}
},

itemStyle: {
top: "50"

// borderWidth: 50, //设置border的宽度有多大
},

data: [
{
value: this.percent1,
name: "买跌",
a: "1"
},
{
value: this.percent2,
name: "买涨",
a: "1"
}
]
}
]
});
}
}
};
</script>

<style lang="scss" scoped>
@import "../../assets/css/reset.scss";

div {
padding: 0 rem(30);

#myChart {
border-bottom: rem(1) solid $line_color;
}

position: relative;

.title {
position: absolute;
left: 44%;
top: 29%;
font-size: rem(30);
}

.percent1 {
position: absolute;
left: 10%;
top: 35%;
font-size: rem(32);
color: #333;
font-weight: bold;
line-height: rem(55);
}

.percent2 {
position: absolute;
right: 10%;
top: 35%;
font-size: rem(32);
color: #333;
font-weight: bold;
line-height: rem(55);
}

.time {
position: absolute;
left: 24%;
top: 70%;
}
}
</style>

效果图如下:

1545913923396