-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
chart-emit-item-tooltip.ts
76 lines (64 loc) · 1.51 KB
/
chart-emit-item-tooltip.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
73
74
75
76
import { Chart } from '../../../src';
export function chartEmitItemTooltip(context) {
const { container, canvas } = context;
// wrapperDiv
const wrapperDiv = document.createElement('div');
container.appendChild(wrapperDiv);
// button
const button = document.createElement('button');
button.innerText = 'Hide tooltip';
container.appendChild(button);
// p
const p = document.createElement('p');
p.innerText = '';
container.appendChild(p);
const chart = new Chart({
container: wrapperDiv,
canvas,
});
chart
.interval()
.data([
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
])
.encode('x', 'genre')
.encode('y', 'sold')
.encode('color', 'genre')
.interaction('tooltip', {
offset: [0, 0],
css: {
'.g2-tooltip': {
transform: 'translate(-50%, -100%)',
},
},
});
const finished = chart.render();
finished.then((chart) =>
chart.emit('tooltip:show', {
offsetY: 0,
data: { data: { sold: 115 } },
}),
);
chart.on('tooltip:show', ({ data }) => {
p.innerText = JSON.stringify(data);
});
const hide = () => {
console.log('hide');
};
chart.on('tooltip:hide', hide);
button.onclick = () => {
chart.emit('tooltip:hide');
};
return {
chart,
button,
finished,
clear: () => {
chart.off('tooltip:hide', hide);
},
};
}