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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
| <template>
| <div class="e-chart-root">
| <div class="e-chart-container">
| <div class="e-chart" ref="chart"></div>
| <div class="e-chart-line" :style="lineStyle"></div>
| </div>
| </div>
| </template>
|
| <script>
| // 引入 ECharts 主模块
| import ECharts from "echarts/lib/echarts";
| //引入折线图
| import "echarts/lib/chart/gauge";
|
| export default {
| chart: "",
| props: {
| lineColor: {
| type: String,
| default: "#ff4887",
| },
| leftColors: {
| type: Array,
| default() {
| return ["#ff4b8750", "#ff4b8750"];
| },
| },
| rightColors: {
| type: Array,
| default() {
| return ["#ff4b87", "#ff84b6"];
| },
| },
| unit: {
| type: String,
| default: "A",
| },
| max: {
| type: Number,
| default: 100,
| },
| },
| data() {
| return {};
| },
| methods: {
| setOption(option) {
| let chart = this.$options.chart;
| if (chart) {
| chart.setOption(option);
| }
| },
| setData(data) {
| let option = this.getInitOption();
| let max = this.max;
| let percent = Math.abs((data / max).toFixed(2));
| option.series[0].axisLine.lineStyle.color[0][0] = percent;
| option.series[0].data[0].value = data;
| this.setOption(option);
| },
| getInitOption() {
| let leftColors = this.leftColors;
| let rightColors = this.rightColors;
| let unit = this.unit;
| let max = this.max;
| let leftColor = new ECharts.graphic.LinearGradient(0, 0, 1, 0, [
| {
| offset: 0,
| color: leftColors[0], // 0% 处的颜色
| },
| {
| offset: 1,
| color: leftColors[1],
| },
| ]);
|
| let rightColor = new ECharts.graphic.LinearGradient(0, 0, 1, 0, [
| {
| offset: 0,
| color: rightColors[0], // 0% 处的颜色
| },
| {
| offset: 1,
| color: rightColors[1],
| },
| ]);
|
| return {
| animation: false,
| series: [
| // 内侧轴线
| {
| type: "gauge",
| radius: "100%", // 位置
| center: ["50%", "90%"],
| min: 0,
| max: max,
| startAngle: 180,
| endAngle: 0,
| axisLine: {
| show: true,
| lineStyle: {
| // 轴线样式
| width: 16, // 宽度
| color: [
| [0, leftColor],
| [1, rightColor],
| ], // 颜色
| },
| },
| pointer: {
| // 仪表盘指针
| show: false,
| },
| axisTick: {
| // 刻度
| show: false,
| },
| splitLine: {
| // 分割线
| show: false,
| },
| axisLabel: {
| // 刻度标签
| show: false,
| },
| detail: {
| lineHeight: 40,
| borderRadius: 8,
| offsetCenter: ["0%", "-12%"],
| fontSize: 18,
| fontWeight: "bolder",
| formatter: function(value) {
| return value + unit;
| },
| color: "#fff",
| },
| data: [
| {
| value: 0,
| },
| ],
| },
| ],
| };
| },
| resize() {
| let chart = this.$options.chart;
| if (chart) {
| chart.resize();
| }
| },
| /**
| * 销毁echarts实例释放内存
| *
| * @return {[type]} [return description]
| */
| dispose() {
| let chart = this.$options.chart;
| if (chart) {
| chart.dispose(); // 销毁实例
| this.$options.chart = "";
| }
| },
| },
| computed: {
| lineStyle() {
| return {
| backgroundColor: this.lineColor,
| };
| },
| },
| mounted() {
| this.$options.chart = ECharts.init(this.$refs.chart);
|
| let data = 0;
| this.setData(data);
| // 监听windows窗口的缩放,绑定resize事件
| window.addEventListener("resize", this.resize);
| },
| beforeDestroy() {
| // 销毁resize事件
| window.removeEventListener("resize", this.resize);
| this.dispose();
| },
| };
| </script>
|
| <style scoped>
| .e-chart-root,
| .e-chart-container,
| .e-chart {
| position: relative;
| height: 100%;
| box-sizing: border-box;
| }
| .e-chart-line {
| position: absolute;
| bottom: 2%;
| left: calc(37.5% - 16px);
| width: calc(25% + 32px);
| height: 4px;
| }
| .e-chart-root.full-screen .e-chart-container {
| position: fixed;
| top: 0;
| left: 0;
| right: 0;
| bottom: 0;
| background-size: 100% 100%;
| z-index: 9999;
| }
|
| .e-chart-tools {
| position: absolute;
| top: 16px;
| right: 16px;
| z-index: 9;
| }
|
| .e-chart-tools .iconfont {
| margin-left: 8px;
| font-size: 24px;
| cursor: pointer;
| color: #00fefe;
| }
|
| .e-chart-tools .iconfont:hover {
| color: #04b1b1;
| }
|
| .e-chart-tools .iconfont:active {
| color: #ff0000;
| }
| </style>
|
|