-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart-emit-series-tooltip.ts
62 lines (50 loc) · 1.25 KB
/
chart-emit-series-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
import { Chart } from '../../../src';
export function chartEmitSeriesTooltip(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.options({
type: 'line',
data: { type: 'fetch', value: 'data/aapl.csv' },
encode: { x: 'date', y: 'close' },
tooltip: { title: (d) => new Date(d.date).toUTCString() },
});
const finished = chart.render();
finished.then((chart) =>
chart.emit('tooltip:show', {
offsetY: 20,
data: { data: { x: new Date('2010-11-16') } },
}),
);
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);
},
};
}