-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart-on-focus-context.ts
80 lines (67 loc) · 2.22 KB
/
chart-on-focus-context.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
77
78
79
80
import { Chart } from '../../../src';
export function chartOnFocusContext(context) {
const { container, canvas1, canvas2 } = context;
// Render focus view.
const focusContainer = document.createElement('div');
container.appendChild(focusContainer);
const focusView = new Chart({
container: focusContainer,
canvas: canvas1,
});
focusView.options({
type: 'area',
height: 360,
data: { type: 'fetch', value: 'data/aapl.csv' },
encode: { x: 'date', y: 'close' },
axis: false,
animate: false,
interaction: { brushXFilter: true, tooltip: false },
});
const focused = focusView.render();
// Render context view.
const contextContainer = document.createElement('div');
container.appendChild(contextContainer);
const contextView = new Chart({
container: contextContainer,
canvas: canvas2,
});
contextView.options({
type: 'area',
height: 120,
data: { type: 'fetch', value: 'data/aapl.csv' },
encode: { x: 'date', y: 'close' },
axis: false,
animate: false,
state: { active: { fill: 'red' } },
interaction: { brushXHighlight: { series: true }, tooltip: false },
});
const contexted = contextView.render();
// Add event listeners.
focusView.on('brush:filter', (e) => {
const { nativeEvent } = e;
if (!nativeEvent) return;
const { selection } = e.data;
const { x: scaleX } = focusView.getScale();
const [[x1, x2]] = selection;
const domainX = scaleX.getOptions().domain;
if (x1 === domainX[0] && x2 === domainX[1]) {
contextView.emit('brush:remove', {});
} else {
contextView.emit('brush:highlight', { data: { selection } });
}
});
contextView.on('brush:highlight', (e) => {
const { nativeEvent, data } = e;
if (!nativeEvent) return;
const { selection } = data;
focusView.emit('brush:filter', { data: { selection } });
});
contextView.on('brush:remove', (e) => {
const { nativeEvent } = e;
if (!nativeEvent) return;
const { x: scaleX, y: scaleY } = contextView.getScale();
const selection = [scaleX.getOptions().domain, scaleY.getOptions().domain];
focusView.emit('brush:filter', { data: { selection } });
});
return { focusView, focused, contexted, contextView };
}