-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
chart-emit-legend-highlight.ts
70 lines (58 loc) · 1.62 KB
/
chart-emit-legend-highlight.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
import { Chart } from '../../../src';
import { profit } from '../../data/profit';
export function chartEmitLegendHighlight(context) {
const { container, canvas } = context;
// button
const button = document.createElement('button');
button.innerText = 'highlight';
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',
data: profit,
axis: { y: { labelFormatter: '~s' } },
encode: {
x: 'month',
y: ['end', 'start'],
color: (d) =>
d.month === 'Total' ? 'Total' : d.profit > 0 ? 'Increase' : 'Decrease',
},
state: { inactive: { opacity: 0.5 } },
legend: {
color: { state: { inactive: { labelOpacity: 0.5, markerOpacity: 0.5 } } },
},
interaction: {
legendHighlight: true,
tooltip: false,
},
});
chart.on('legend:highlight', (e) => {
const { nativeEvent, data } = e;
if (!nativeEvent) return;
console.log(data);
});
chart.on('legend:unhighlight', (e) => {
const { nativeEvent } = e;
if (!nativeEvent) return;
console.log('unhighlight');
});
button.onclick = () => {
chart.emit('legend:highlight', {
data: { channel: 'color', value: 'Increase' },
});
};
button1.onclick = () => {
chart.emit('legend:unhighlight', {});
};
const finished = chart.render();
return { chart, finished };
}