/
stack.js
106 lines (105 loc) · 2.04 KB
/
stack.js
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
import F2 from '@antv/f2';
import _ from 'lodash';
const data = [{
name: 'London',
月份: 'Jan.',
月均降雨量: 18.9
}, {
name: 'London',
月份: 'Feb.',
月均降雨量: 28.8
}, {
name: 'London',
月份: 'Mar.',
月均降雨量: 39.3
}, {
name: 'London',
月份: 'Apr.',
月均降雨量: 81.4
}, {
name: 'London',
月份: 'May.',
月均降雨量: 47
}, {
name: 'London',
月份: 'Jun.',
月均降雨量: 20.3
}, {
name: 'London',
月份: 'Jul.',
月均降雨量: 24
}, {
name: 'London',
月份: 'Aug.',
月均降雨量: 35.6
}, {
name: 'Berlin',
月份: 'Jan.',
月均降雨量: 12.4
}, {
name: 'Berlin',
月份: 'Feb.',
月均降雨量: 23.2
}, {
name: 'Berlin',
月份: 'Mar.',
月均降雨量: 34.5
}, {
name: 'Berlin',
月份: 'Apr.',
月均降雨量: 99.7
}, {
name: 'Berlin',
月份: 'May.',
月均降雨量: 52.6
}, {
name: 'Berlin',
月份: 'Jun.',
月均降雨量: 35.5
}, {
name: 'Berlin',
月份: 'Jul.',
月均降雨量: 37.4
}, {
name: 'Berlin',
月份: 'Aug.',
月均降雨量: 42.4
}];
const chart = new F2.Chart({
id: 'container',
pixelRatio: window.devicePixelRatio
});
chart.source(data, {
月均降雨量: {
tickCount: 5
}
});
chart.tooltip({
custom: true, // 自定义 tooltip 内容框
onChange: function onChange(obj) {
const legend = chart.get('legendController').legends.top[0];
const tooltipItems = obj.items;
const legendItems = legend.items;
const map = {};
legendItems.forEach(function(item) {
map[item.name] = _.clone(item);
});
tooltipItems.forEach(function(item) {
const name = item.name;
const value = item.value;
if (map[name]) {
map[name].value = value;
}
});
legend.setItems(_.values(map));
},
onHide: function onHide() {
const legend = chart.get('legendController').legends.top[0];
legend.setItems(chart.getLegendItems().country);
}
});
chart.interval()
.position('月份*月均降雨量')
.color('name')
.adjust('stack');
chart.render();