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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<template>
<div :class="className" :style="{ height: height, width: width }"></div>
</template>
<script>
import * as echarts from 'echarts';
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
const animationDuration = 2600;
export default {
mixins: [resize],
props: {
className: {
type: String,
default: "chart"
},
width: {
type: String,
default: "100%"
},
height: {
type: String,
default: "100%",
},
autoResize: {
type: Boolean,
default: true
},
chartData: {
// type: Object
required: true
},
chartConfig: {
type: Object,
default: () => {
return {};
}
},
title: {
type: String,
default: "分布图"
}
},
data() {
return {
chart: null,
config: {
title: {
text: "",
top: "3%",
left: "1%",
// padding: 20,
textStyle: {
color: "rgba(255, 255, 255, 0.8)",
fontStyle: "normal",
fontSize: 16
}
// subtext: "筛查人群总数\n" + "10,234",
// subtextStyle: {
// color: "#80FFFF",
// fontStyle: "normal",
// fontSize: 14,
// align: "center",
// formatter: [
// "{a|这段文本采用样式a}",
// "{b|这段文本采用样式b}这段用默认样式{x|这段用样式x}"
// ].join("\n"),
// rich: {
// a: {
// color: "red",
// lineHeight: 10
// },
// b: {
// backgroundColor: {
// image: "xxx/xxx.jpg"
// },
// height: 40
// },
// x: {
// fontSize: 18,
// fontFamily: "Microsoft YaHei",
// borderColor: "#449933",
// borderRadius: 4
// }
// }
// }
},
color: [
"rgba(2, 167, 240, 1)",
"rgba(245, 166, 0, 1)",
"rgba(248, 60, 83, 1)"
],
// 设置图表的位置
grid: {
// x: 60, // 左间距
// y: 80, // 上间距
// x2: 60, // 右间距
// y2: 40, // 下间距
left: 20,
right: 20,
bottom: 20,
top: "23%", //grid 组件离容器上侧的距离
containLabel: true // grid 区域是否包含坐标轴的刻度标签, 常用于『防止标签溢出』的场景
},
// 提示框组件
tooltip: {
trigger: "axis", // 触发类型, axis: 坐标轴触发
borderWidth:0,
axisPointer: {
// 指示器类型 'line' 直线指示器 'shadow' 阴影指示器 'none' 无指示器
// 'cross' 十字准星指示器 其实是种简写,表示启用两个正交的轴的 axisPointer
type: "none"
},
textStyle: {
color: "#cdd3ee" // 文字颜色
},
// 提示框浮层内容格式器,支持字符串模板和回调函数两种形式 折线(区域)图、柱状(条形)图、K线图
// {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)
// formatter: "{b}<br />{a0}: {c0}%<br />{a1}: {c1}%<br />{a2}: {c2}%"
// formatter: "{b}<br />{a}: {c}%<br />"
formatter: function(params) {
var relVal = params[0].name;
for (var i = 0, l = params.length; i < l; i++) {
relVal +=
"<br/>" +
params[i].marker +
params[i].seriesName +
" : " +
params[i].value +
"%";
}
return relVal;
}
},
// 图例组件
legend: {
show: false,
textStyle: {
// 文本样式
fontSize: 16,
color: "#cdd3ee"
},
top: 13, // 定位
data: ["已完成", "进行中", "未完成"] // 图例的数据数组
},
// X轴
xAxis: {
type: "value", // 坐标轴类型, 'value' 数值轴,适用于连续数据
// 坐标轴刻度
axisTick: {
show: false // 是否显示坐标轴刻度 默认显示
},
// 坐标轴轴线
axisLine: {
// 是否显示坐标轴轴线 默认显示
show: false // 是否显示坐标轴轴线 默认显示
},
// 坐标轴在图表区域中的分隔线
splitLine: {
show: false // 是否显示分隔线。默认数值轴显示
},
splitArea: false,
// 坐标轴刻度标签
axisLabel: {
show: false // 是否显示刻度标签 默认显示
},
max: [100] //相当于百分比饼图
},
yAxis: [
// 左侧Y轴
{
// 坐标轴类型, 'category' 类目轴,适用于离散的类目数据
// 为该类型时必须通过 data 设置类目数据
type: "category",
// 坐标轴刻度
axisTick: {
show: false // 是否显示坐标轴刻度 默认显示
},
// 坐标轴轴线
axisLine: {
// 是否显示坐标轴轴线 默认显示
show: false, // 是否显示坐标轴轴线 默认显示
lineStyle: {
// 坐标轴线线的颜色
color: "#cdd3ee"
}
},
// 坐标轴在图表区域中的分隔线
splitLine: {
show: false // 是否显示分隔线。默认数值轴显示
},
splitArea: false,
// 坐标轴刻度标签
axisLabel: {
show: false, // 是否显示刻度标签 默认显示
fontSize: 16, // 文字的字体大小
color: "#cdd3ee", // 刻度标签文字的颜色
// 使用字符串模板,模板变量为刻度默认标签 {value}
formatter: "{value}"
},
// 类目数据,在类目轴(type: 'category')中有效
data: ["比例"]
}
],
// 系列列表
series: [
{
type: "bar", // 系列类型
name: "", // 系列名称, 用于tooltip的显示, legend 的图例筛选
stack: "total", // 数据堆叠,同个类目轴上系列配置相同的stack值后,后一个系列的值会在前一个系列的值上相加
barMaxWidth: 20, // 柱条的最大宽度,不设时自适应
// 图形上的文本标签
label: {
show: true,
// 标签的位置 left right bottom top inside // 绝对的像素值 position: [10, 10]
// 相对的百分比 position: ['50%', '50%']
position: "inside",
color: "white",
fontSize: 16,
distance: 0,
formatter: "{a0}\n\n\n\n{c0}%"
},
// 图形样式
// itemStyle: {
// barBorderRadius: [10, 0, 0, 10] // 圆角半径, 单位px, 支持传入数组分别指定 4 个圆角半径
// },
data: [] // 系列中的数据内容数组
}
// {
// type: "bar",
// name: "",
// stack: "总量",
// barMaxWidth: 20,
// label: {
// show: true,
// position: "inside",
// color: "white",
// fontSize: 16,
// distance: 0,
// formatter: "{a0}\n\n\n\n{c0}%"
// },
// data: []
// },
// {
// type: "bar",
// name: "",
// stack: "总量",
// barMaxWidth: 20,
// label: {
// show: true,
// position: "inside",
// color: "white",
// fontSize: 16,
// distance: 0,
// formatter: "{a0}\n\n\n\n{c0}%"
// },
// data: []
// }
]
}
};
},
watch: {
chartData: {
deep: true,
handler(val) {
this.setOptions(val);
}
}
},
mounted() {
this.$nextTick(() => {
this.initChart();
});
},
beforeDestroy() {
if (!this.chart) {
return;
}
this.chart.dispose();
this.chart = null;
},
methods: {
initChart() {
// this.chart = echarts.init(document.getElementById('chart'));
this.chart = echarts.init(this.$el, "macarons");
// 监听屏幕变化自动缩放图表
// window.addEventListener('resize', function () { this.chart.resize() })
this.setOptions(this.chartData);
},
setOptions(chartData) {
// let option = {};
// option = this.config;
if (!chartData.data) return;
const option = Object.assign(this.config, this.chartConfig);
const series = chartData.data.map(v => {
if (v.color) {
return {
name: v.name,
value: Number(v.value),
color: v.color
};
}
return {
name: v.name,
value: Number(v.value)
};
});
const seriesList = [];
const colorList = [];
const legendList = [];
series.forEach((item, index) => {
const seriesItem = {
type: "bar",
name: "",
stack: "total",
// barGap: '-100%', //重叠
barMaxWidth: 20,
label: {
show: true,
position: "inside",
color: "white",
fontSize: 16,
distance: 0,
formatter: "{a0}\n\n\n\n{c0}%"
},
data: []
};
// item.data = [series[index].value];
// item.name = [series[index].name];
if (item.color) {
colorList.push(item.color);
}
if (item.name) {
legendList.push(item.name);
seriesItem.name = item.name;
}
if (item.value) {
seriesItem.data = [item.value];
}
seriesList.push(seriesItem);
});
// if (chartData.legend && chartData.legend.data) {
// option.legend.data = chartData.legend.data;
// }
// // option.xAxis[0].data = series.map(_ => _.name);
// // option.series[0].data = series.map(_ => _.value);
option.title.text = this.title;
option.series = seriesList;
option.color = series.map(_ => _.color);
option.legend.data = series.map(_ => _.name);
this.chart.setOption(option);
this.$forceUpdate();
}
}
};
</script>