-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
chart-emit-element-select.ts
72 lines (60 loc) · 1.59 KB
/
chart-emit-element-select.ts
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
import { Chart } from '../../../src';
export function chartEmitElementSelect(context) {
const { container, canvas } = context;
// button
const button = document.createElement('button');
button.innerText = 'Select';
container.appendChild(button);
const button1 = document.createElement('button');
button1.innerText = 'reset';
container.appendChild(button1);
// wrapperDiv
const wrapperDiv = document.createElement('div');
container.appendChild(wrapperDiv);
const chart = new Chart({
container: wrapperDiv,
canvas,
});
chart.options({
type: 'interval',
transform: [
{ type: 'sortX', by: 'y', reverse: true, reducer: 'sum', slice: 6 },
{ type: 'dodgeX' },
],
data: {
type: 'fetch',
value: 'data/stateages.csv',
},
encode: {
x: 'state',
y: 'population',
color: 'age',
},
state: {
selected: { fill: 'red' },
unselected: { opacity: 0.6 },
},
interaction: {
elementSelectByX: { delay: 0 },
tooltip: false,
},
});
const finished = chart.render();
chart.on('element:select', (event) => {
const { data, nativeEvent } = event;
if (nativeEvent) console.log('element:select', data);
});
chart.on('element:unselect', (event) => {
const { nativeEvent } = event;
if (nativeEvent) console.log('reset');
});
button.onclick = () => {
chart.emit('element:select', {
data: { data: [{ population: 5038433 }, { population: 3983091 }] },
});
};
button1.onclick = () => {
chart.emit('element:unselect', {});
};
return { chart, finished };
}