Echarts中数据集的使用

前言

官网:https://echarts.apache.org/handbook/zh/get-started

配置项:https://echarts.apache.org/zh/option.html#title

官方示例:https://echarts.apache.org/examples/zh/index.html

第三方示例平台:https://www.makeapie.cn/echarts

主题:https://echarts.apache.org/zh/theme-builder.html

从 ECharts4 支持数据集开始,更推荐使用数据集来管理数据。

https://echarts.apache.org/handbook/zh/concepts/dataset

数据集最大的特点就是数据和数据展示配置的分离。

ECharts的数据集(dataset)是一个用于管理数据的组件,可以方便地进行“数据和其他配置”分离的配置风格。虽然每个系列都可以在 series.data 中设置数据,但是从 ECharts4 支持数据集开始,更推荐使用数据集来管理数据。因为这样,数据可以被多个组件复用。同时,使用数据集还能支持更多的数据格式,例如二维数组、对象数组等,一定程度上避免使用者为了数据格式而进行转换。

在 ECharts 中,大部分图形都可以使用数据集来管理数据。

但是,有一些图形不支持使用数据集来管理数据,例如:mapgraphsankeyfunnelgaugewordCloud 等。

以前我们都是在系列(series)中设置数据。

如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
option = {
xAxis: {
type: 'category',
data: ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie']
},
yAxis: {},
series: [
{
type: 'bar',
name: '2015',
data: [89.3, 92.1, 94.4, 85.4]
},
{
type: 'bar',
name: '2016',
data: [95.8, 89.4, 91.2, 76.9]
},
{
type: 'bar',
name: '2017',
data: [97.7, 83.1, 92.5, 78.1]
}
]
};

使用数据集后,序列中只需要设置x,y展示的列即可。

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
{
"legend": {},
"tooltip": {},
"dataset": {
"dimensions": [
"product",
"2015",
"2016",
"2017"
],
"source": [
{
"2015": 43.3,
"2016": 85.8,
"2017": 93.7,
"product": "Matcha Latte"
},
{
"2015": 83.1,
"2016": 73.4,
"2017": 55.1,
"product": "Milk Tea"
},
{
"2015": 86.4,
"2016": 65.2,
"2017": 82.5,
"product": "Cheese Cocoa"
},
{
"2015": 72.4,
"2016": 53.9,
"2017": 39.1,
"product": "Walnut Brownie"
}
]
},
"xAxis": {
"type": "category"
},
"yAxis": {},
"series": [
{
"type": "bar",
"name": "2015",
"encode": {
"tooltip": [
"product"
],
"x": "product",
"y": "2015"
}
},
{
"type": "bar",
"name": "2016",
"encode": {
"tooltip": [
"product"
],
"x": "product",
"y": "2016"
}
},
{
"type": "bar",
"name": "2017",
"encode": {
"tooltip": [
"product"
],
"x": "product",
"y": "2017"
}
}
]
}

encode的参数

目前测试的雷达图不能使用数据集的方式。

可以使用的

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
// 在任何坐标系和系列中,都支持:
encode: {
// 使用 “名为 product 的维度” 和 “名为 score 的维度” 的值在 tooltip 中显示
tooltip: ['product', 'score']
// 使用 “维度 1” 和 “维度 3” 的维度名连起来作为系列名。(有时候名字比较长,这可以避免在 series.name 重复输入这些名字)
seriesName: [1, 3],
// 表示使用 “维度2” 中的值作为 id。这在使用 setOption 动态更新数据时有用处,可以使新老数据用 id 对应起来,从而能够产生合适的数据更新动画。
itemId: 2,
// 指定数据项的名称使用 “维度3” 在饼图等图表中有用,可以使这个名字显示在图例(legend)中。
itemName: 3
}

// 直角坐标系(grid/cartesian)特有的属性:
encode: {
// 把 “维度1”、“维度5”、“名为 score 的维度” 映射到 X 轴:
x: [1, 5, 'score'],
// 把“维度0”映射到 Y 轴。
y: 0
}

// 单轴(singleAxis)特有的属性:
encode: {
single: 3
}

// 极坐标系(polar)特有的属性:
encode: {
radius: 3,
angle: 2
}

// 地理坐标系(geo)特有的属性:
encode: {
lng: 3,
lat: 2
}

// 对于一些没有坐标系的图表,例如饼图、漏斗图等,可以是:
encode: {
value: 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
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
<template>
<div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
import resize from "@/assets/utils/chat_resize.js";

export default {
name: "BarChart",
mixins: [resize],
props: {
className: {
type: String,
default: "chart",
},
width: {
type: String,
default: "600px",
},
height: {
type: String,
default: "400px",
},
title: {
type: String,
default: "",
},
colorField: {
type: String,
default() {
return "name";
},
},
keyFields: {
type: Array,
default() {
return ["name", "subject"];
},
},
valueFields: {
type: Array,
default() {
return ["score", "socreHistory"];
},
},
dataList: {
type: Array,
default() {
return [
{
id: 1001,
name: "王宁",
subject: "语文",
score: 80,
sex: "女",
socreHistory: 100,
},
{
id: 1002,
name: "王宁",
subject: "数学",
score: 60,
sex: "女",
socreHistory: 80,
},
{
id: 1003,
name: "王宁",
subject: "英语",
score: 93,
sex: "女",
socreHistory: 60,
},
{
id: 1004,
name: "张瑞红",
subject: "语文",
score: 99,
sex: "女",
socreHistory: 30,
},
{
id: 1005,
name: "张瑞红",
subject: "数学",
score: 70,
sex: "女",
socreHistory: 80,
},
{
id: 1006,
name: "张瑞红",
subject: "英语",
score: 80.5,
sex: "女",
socreHistory: 60,
},
{
id: 1007,
name: "王中露",
subject: "语文",
score: 60,
sex: "男",
socreHistory: 70,
},
{
id: 1008,
name: "王中露",
subject: "数学",
score: 90,
sex: "男",
socreHistory: 100,
},
{
id: 1009,
name: "王中露",
subject: "英语",
score: 70,
sex: "男",
socreHistory: 90,
},
{
id: 1010,
name: "郭忠博",
subject: "语文",
score: 80,
sex: "男",
socreHistory: 60,
},
{
id: 1011,
name: "郭忠博",
subject: "数学",
score: 70,
sex: "男",
socreHistory: 50,
},
{
id: 1012,
name: "郭忠博",
subject: "英语",
score: 99,
sex: "男",
socreHistory: 70,
},
{
id: 1013,
name: "雍文秀",
subject: "语文",
score: 30,
sex: "女",
socreHistory: 80,
},
{
id: 1014,
name: "雍文秀",
subject: "数学",
score: 100,
sex: "女",
socreHistory: 98,
},
{
id: 1015,
name: "雍文秀",
subject: "英语",
score: 98,
sex: "女",
socreHistory: 65,
},
];
},
},
},
data() {
return {
chart: null,
};
},
watch: {
dataList: function () {
this.initChart();
},
},
async mounted() {
await this.$nextTick();
this.initChart();
},
beforeDestroy() {
if (!this.chart) {
return;
}
this.chart.dispose();
this.chart = null;
},
methods: {
initChart() {
if (!this.chart) {
this.chart = echarts.init(this.$el);
}

let dataList = this.dataList;

let option = {
legend: {
show: true,
top: 2,
right: 2,
},
tooltip: {
show: true,
},
color: [
"#20afff",
"#2cd9ff",
"#9dfff0",
"#77fef6",
"#fffc91",
"#fdc67f",
"#f2528b",
],
dataset: {
dimensions: dataList.length ? Object.keys(dataList[0]) : [],
source: dataList,
},
xAxis: { type: "category" },
yAxis: {},
series: [
{
type: "bar",
stack: "分数",
name: "分数",
showBackground: true,
backgroundStyle: {
color: "#f3f3f3",
},
seriesLayoutBy: "column",
encode: {
tooltip: ["name", "subject", "score"],
x: ["name"],
y: "score",
itemGroupID: "subject",
itemName: "subject",
},
},
{
type: "bar",
name: "历史分数",
showBackground: true,
backgroundStyle: {
color: "#f3f3f3",
},
stack: "历史分数",
encode: {
tooltip: ["name", "subject", "score"],
x: "name",
y: "socreHistory",
},
},
],
};
this.chart.setOption(option);
},
},
};
</script>

数据分组

如果我们想把数据按科目进行分组

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
<template>
<div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
import resize from "@/assets/utils/chat_resize.js";

export default {
name: "BarChart",
mixins: [resize],
props: {
className: {
type: String,
default: "chart",
},
width: {
type: String,
default: "600px",
},
height: {
type: String,
default: "400px",
},
title: {
type: String,
default: "",
},
colorField: {
type: String,
default() {
return "subject";
},
},
keyFields: {
type: Array,
default() {
return ["name"];
},
},
valueFields: {
type: Array,
default() {
return ["score", "socreHistory"];
},
},
dataList: {
type: Array,
default() {
return [
{
id: 1001,
name: "王宁",
subject: "语文",
score: 80,
sex: "女",
socreHistory: 100,
},
{
id: 1002,
name: "王宁",
subject: "数学",
score: 60,
sex: "女",
socreHistory: 80,
},
{
id: 1003,
name: "王宁",
subject: "英语",
score: 93,
sex: "女",
socreHistory: 60,
},
{
id: 1004,
name: "张瑞红",
subject: "语文",
score: 99,
sex: "女",
socreHistory: 30,
},
{
id: 1005,
name: "张瑞红",
subject: "数学",
score: 70,
sex: "女",
socreHistory: 80,
},
{
id: 1006,
name: "张瑞红",
subject: "英语",
score: 80.5,
sex: "女",
socreHistory: 60,
},
{
id: 1007,
name: "王中露",
subject: "语文",
score: 60,
sex: "男",
socreHistory: 70,
},
{
id: 1008,
name: "王中露",
subject: "数学",
score: 90,
sex: "男",
socreHistory: 100,
},
{
id: 1009,
name: "王中露",
subject: "英语",
score: 70,
sex: "男",
socreHistory: 90,
},
{
id: 1010,
name: "郭忠博",
subject: "语文",
score: 80,
sex: "男",
socreHistory: 60,
},
{
id: 1011,
name: "郭忠博",
subject: "数学",
score: 70,
sex: "男",
socreHistory: 50,
},
{
id: 1012,
name: "郭忠博",
subject: "英语",
score: 99,
sex: "男",
socreHistory: 70,
},
{
id: 1013,
name: "雍文秀",
subject: "语文",
score: 30,
sex: "女",
socreHistory: 80,
},
{
id: 1014,
name: "雍文秀",
subject: "数学",
score: 100,
sex: "女",
socreHistory: 98,
},
{
id: 1015,
name: "雍文秀",
subject: "英语",
score: 98,
sex: "女",
socreHistory: 65,
},
];
},
},
},
data() {
return {
chart: null,
};
},
watch: {
dataList() {
this.initChart();
},
keyFields: function () {
if (window.z_chart_timer) {
clearTimeout(window.z_chart_timer);
}
window.z_chart_timer = setTimeout(() => {
this.initChart();
}, 100);
},
valueFields() {
if (window.z_chart_timer) {
clearTimeout(window.z_chart_timer);
}
window.z_chart_timer = setTimeout(() => {
this.initChart();
}, 100);
},
colorField: function () {
if (window.z_chart_timer) {
clearTimeout(window.z_chart_timer);
}
window.z_chart_timer = setTimeout(() => {
this.initChart();
}, 100);
},
},
async mounted() {
await this.$nextTick();
this.initChart();
},
beforeDestroy() {
if (!this.chart) {
return;
}
this.chart.dispose();
this.chart = null;
},
methods: {
initChart() {
if (!this.chart) {
this.chart = echarts.init(this.$el);
}

console.info("执行了");

let mColors = [
"#20afff",
"#2cd9ff",
"#9dfff0",
"#77fef6",
"#fffc91",
"#fdc67f",
"#f2528b",
];
let colorField = this.colorField;

let dataList = this.dataList;
let mDataset = [];
let mDimensions = dataList.length ? Object.keys(dataList[0]) : [];
mDataset.push({
dimensions: mDimensions,
source: dataList,
});
let mSeries = [];
let keyFields = this.keyFields;
let valueFields = this.valueFields;
if (colorField) {
let groupNames = [];
for (let i = 0; i < dataList.length; i++) {
let item = dataList[i];
if (groupNames.indexOf(item[colorField]) === -1) {
groupNames.push(item[colorField]);
}
}

for (let i = 0; i < groupNames.length; i++) {
let groupName = groupNames[i];
mDataset.push({
transform: {
type: "filter",
config: { dimension: colorField, value: groupName },
// print: true,
},
});

for (let j = 0; j < valueFields.length; j++) {
let valueField = valueFields[j];
mSeries.push({
type: "bar",
name: groupName,

showBackground: true,
datasetIndex: i + 1,
backgroundStyle: {
color: "#f3f3f3",
},
encode: {
tooltip: mDimensions,
x: keyFields,
y: valueField,
},
});
}
}
} else {
for (let i = 0; i < valueFields.length; i++) {
let valueField = valueFields[i];
mSeries.push({
type: "bar",
showBackground: true,
backgroundStyle: {
color: "#f3f3f3",
},
color: mColors,
encode: {
tooltip: mDimensions,
x: keyFields,
y: valueField,
},
});
}
}

let option = {
colorBy: colorField ? "series" : "data",
legend: {
show: true,
top: 2,
right: 2,
},
tooltip: {
show: true,
},
color: mColors,
dataset: mDataset,
xAxis: { type: "category" },
yAxis: {},
series: mSeries,
};
// console.info(option);
this.chart.setOption(option, {
notMerge: true,
lazyUpdate: false,
silent: false,
});
},
},
};
</script>

tooltip

trigger

tooltip 有一个 trigger 属性,用于指定触发提示框显示的方式。

默认为'item'

以下是常见的几种 trigger 的取值及其意义:

  • 'item':当鼠标悬停在图形(如柱状图、折线图等)上时触发提示框显示,并显示与该数据项关联的详细信息。
  • 'axis':当鼠标悬停在坐标轴上时触发提示框显示,并显示该坐标轴上所有数据项的详细信息(如折线图中的指定时间点的数据)。
  • 'none':不触发提示框显示。

优先级

series.tooltip > tooltip

注意:series.tooltip 仅在 tooltip.trigger'item' 时有效。

格式化 formatter

方式1 字符串模板

1
tooltip: { formatter: "{a}:{b}:{c}" },

模板变量有 {a}, {b}{c}{d}{e},分别表示系列名,数据名,数据值等。

trigger'axis' 的时候,会有多个系列的数据,此时可以通过 {a0}, {a1}, {a2} 这种后面加索引的方式表示系列的索引。

不同图表类型下的 {a}{b}{c}{d} 含义不一样。

其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:

  • 折线(区域)图、柱状(条形)图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)
  • 散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}(数值数组), {d}(无)
  • 地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)
  • 饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)

方式2 回调函数

1
2
3
4
5
6
7
tooltip: {
show: true,
formatter: function (params, ticket, callback) {
console.info(params);
return params.name;
},
},

回调函数格式:

1
(params: Object|Array, ticket: string, callback: (ticket: string, html: string)) => string | HTMLElement | HTMLElement[]

支持返回 HTML 字符串或者创建的 DOM 实例。

第一个参数 params 是 formatter 需要的数据集。

格式如下:

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
{
componentType: 'series',
// 系列类型
seriesType: string,
// 系列在传入的 option.series 中的 index
seriesIndex: number,
// 系列名称
seriesName: string,
// 数据名,类目名
name: string,
// 数据在传入的 data 数组中的 index
dataIndex: number,
// 传入的原始数据项
data: Object,
// 传入的数据值。在多数系列下它和 data 相同。在一些系列下是 data 中的分量(如 map、radar 中)
value: number|Array|Object,
// 坐标轴 encode 映射信息,
// key 为坐标轴(如 'x' 'y' 'radius' 'angle' 等)
// value 必然为数组,不会为 null/undefined,表示 dimension index 。
// 其内容如:
// {
// x: [2] // dimension index 为 2 的数据映射到 x 轴
// y: [0] // dimension index 为 0 的数据映射到 y 轴
// }
encode: Object,
// 维度名列表
dimensionNames: Array<String>,
// 数据的维度 index,如 0 或 1 或 2 ...
// 仅在雷达图中使用。
dimensionIndex: number,
// 数据图形的颜色
color: string,
// 饼图/漏斗图的百分比
percent: number,
// 旭日图中当前节点的祖先节点(包括自身)
treePathInfo: Array,
// 树图/矩形树图中当前节点的祖先节点(包括自身)
treeAncestors: Array
}

data方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tooltip: {
formatter: function (params) {
console.info(params);
return (
params.seriesName +
"-" +
valueFieldName +
"<br>" +
params.name +
":" +
params.value
);
},
},

数据集方式

纯数据

如果数据为:

1
2
3
4
5
6
7
8
dataset: {
source: [
['Matcha Latte', 43.3, 85.8, 93.7],
['Milk Tea', 83.1, 73.4, 55.1],
['Cheese Cocoa', 86.4, 65.2, 82.5],
['Walnut Brownie', 72.4, 53.9, 39.1]
]
}

则可这样得到 y 轴对应的 value:

1
params.value[params.encode.y[0]]
数据+维度

如果数据为:

1
2
3
4
5
6
7
8
9
dataset: {
dimensions: ['product', '2015', '2016', '2017'],
source: [
{product: 'Matcha Latte', '2015': 43.3, '2016': 85.8, '2017': 93.7},
{product: 'Milk Tea', '2015': 83.1, '2016': 73.4, '2017': 55.1},
{product: 'Cheese Cocoa', '2015': 86.4, '2016': 65.2, '2017': 82.5},
{product: 'Walnut Brownie', '2015': 72.4, '2016': 53.9, '2017': 39.1}
]
}

则可这样得到 y 轴对应的 value:

1
params.value[params.dimensionNames[params.encode.y[0]]]

直角坐标系:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tooltip: {
formatter: function (params) {
return (
params.seriesName +
"-" +
valueFieldName +
"<br>" +
params.name +
":" +
params.value[params.dimensionNames[params.encode.y[0]]]
);
},
},
encode: {
x: keyField,
y: valueField,
},

极坐标系:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tooltip: {
formatter: function (params) {
return (
params.seriesName +
"-" +
valueFieldName +
"<br>" +
params.name +
":" +
params.value[params.dimensionNames[params.encode.radius[0]]]
);
},
},
encode: {
radius: valueField,
angle: keyField,
},

ticket和callback

第二个参数 ticket 是异步回调标识,配合第三个参数 callback 使用。

第三个参数 callback 是异步回调,在提示框浮层内容是异步获取的时候,可以通过 callback 传入上述的 tickethtml 更新提示框浮层内容。

示例:

1
2
3
4
5
6
formatter: function (params, ticket, callback) {
$.get('detail?name=' + params.name, function (content) {
callback(ticket, toHTML(content));
});
return 'Loading';
}