forked from chartjs/chartjs-plugin-annotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpie.html
118 lines (106 loc) · 2.85 KB
/
pie.html
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!doctype html>
<html>
<head>
<title>Pie Chart</title>
<script src="../node_modules/chart.js/dist/chart.js"></script>
<script src="../dist/chartjs-plugin-annotation.js"></script>
<script src="https://chartjs.org/samples/master/utils.js"></script>
</head>
<body>
<div id="canvas-holder" style="width:40%">
<canvas id="chart-area"></canvas>
</div>
<button id="randomizeData">Randomize Data</button>
<button id="addDataset">Add Dataset</button>
<button id="removeDataset">Remove Dataset</button>
<script>
const randomScalingFactor = function() {
return Math.round(Math.random() * 100);
};
const config = {
type: 'pie',
data: {
datasets: [{
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
],
backgroundColor: [
window.chartColors.red,
window.chartColors.orange,
window.chartColors.yellow,
window.chartColors.green,
window.chartColors.blue,
],
label: 'Dataset 1'
}],
labels: [
'Red',
'Orange',
'Yellow',
'Green',
'Blue'
]
},
options: {
responsive: true,
plugins: {
annotation: { // not supported, and should not display or error
drawTime: 'beforeDatasetsDraw',
annotations: {
myBox: {
type: 'box',
xScaleID: 'x',
yScaleID: 'y',
xMin: -120,
xMax: 20,
yMin: -120,
yMax: 20,
backgroundColor: 'rgba(101, 33, 171, 1.0)',
borderColor: 'rgb(101, 33, 171)',
borderWidth: 1,
dblclick(context) {
console.log(context.element.options.id, context);
}
}
}
}
}
}
};
window.onload = function() {
const ctx = document.getElementById('chart-area').getContext('2d');
window.myPie = new Chart(ctx, config);
};
document.getElementById('randomizeData').addEventListener('click', () => {
config.data.datasets.forEach((dataset) => {
dataset.data = dataset.data.map(() => randomScalingFactor());
});
window.myPie.update();
});
const colorNames = Object.keys(window.chartColors);
document.getElementById('addDataset').addEventListener('click', () => {
const newDataset = {
backgroundColor: [],
data: [],
label: 'New dataset ' + config.data.datasets.length,
};
for (let index = 0; index < config.data.labels.length; ++index) {
newDataset.data.push(randomScalingFactor());
const colorName = colorNames[index % colorNames.length];
const newColor = window.chartColors[colorName];
newDataset.backgroundColor.push(newColor);
}
config.data.datasets.push(newDataset);
window.myPie.update();
});
document.getElementById('removeDataset').addEventListener('click', () => {
config.data.datasets.splice(0, 1);
window.myPie.update();
});
</script>
</body>
</html>