-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
chart-change-size-custom-shape.ts
63 lines (54 loc) · 1.59 KB
/
chart-change-size-custom-shape.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
import { Chart, register } from '../../../src';
export function chartChangeSizeCustomShape(context) {
const { container, canvas } = context;
const button = document.createElement('button');
button.innerText = 'Update Size';
button.style.display = 'block';
container.appendChild(button);
const div = document.createElement('div');
container.appendChild(div);
register('shape.interval.triangle', (style, context) => {
const { document } = context;
return (P, value, defaults) => {
const { color: defaultColor } = defaults;
const [p0, p1, p2, p3] = P;
const pm = [(p0[0] + p1[0]) / 2, p0[1]];
const { color = defaultColor } = value;
const group = document.createElement('g');
const polygon = document.createElement('polygon', {
style: {
...style,
fill: color,
points: [pm, p2, p3],
},
});
group.appendChild(polygon);
return group;
};
});
const chart = new Chart({
container: div,
canvas,
});
chart
.interval()
.data([
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Infantry', sold: 220 },
{ genre: 'Logistics', sold: 330 },
{ genre: 'Other', sold: 150 },
])
.encode('x', 'genre')
.encode('y', 'sold')
.encode('color', 'genre')
.style('shape', 'triangle')
.animate(false);
const finished = chart.render();
button.onclick = () => {
chart.changeSize(400, 300);
};
return { chart, button, finished };
}