-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
default.html
109 lines (97 loc) · 3.81 KB
/
default.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
<!DOCTYPE html>
<html>
<head>
<title>Box Plot Chart</title>
<script src="https://unpkg.com/chart.js@3.8.0/dist/chart.js"></script>
<script src="../build/index.umd.js"></script>
<script src="https://unpkg.com/d3-random@latest/dist/d3-random.min.js"></script>
<script src="./utils.js"></script>
</head>
<body>
<div id="container" style="width: 75%">
<canvas id="canvas"></canvas>
</div>
<button id="randomizeData">Randomize Data</button>
<button id="addDataset">Add Dataset</button>
<button id="removeDataset">Remove Dataset</button>
<button id="addData">Add Data</button>
<button id="removeData">Remove Data</button>
<script>
const samples = this.Samples.utils;
const color = Chart.helpers.color;
const b = d3.randomNormal();
const random = (min, max) => () => b() * ((max || 1) - (min || 0)) + (min || 0);
const boxplotData = {
labels: samples.months({ count: 7 }),
datasets: [
{
label: 'Dataset 1',
backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
borderColor: window.chartColors.red,
borderWidth: 1,
data: samples.boxplots({ count: 7, random: random }),
outlierBackgroundColor: '#999999',
},
{
label: 'Dataset 2',
backgroundColor: color(window.chartColors.blue).alpha(0.5).rgbString(),
borderColor: window.chartColors.blue,
borderWidth: 1,
data: samples.boxplotsArray({ count: 7, random: random }),
outlierBackgroundColor: '#999999',
lowerBackgroundColor: '#461e7d',
},
],
};
window.onload = () => {
const ctx = document.getElementById('canvas').getContext('2d');
const myBar = new Chart(ctx, {
type: 'boxplot',
data: boxplotData,
});
document.getElementById('randomizeData').addEventListener('click', () => {
boxplotData.datasets.forEach(function (dataset) {
dataset.data = samples.boxplots({ count: 7 });
});
myBar.update();
});
const colorNames = Object.keys(window.chartColors);
document.getElementById('addDataset').addEventListener('click', () => {
const colorName = colorNames[boxplotData.datasets.length % colorNames.length];
const dsColor = window.chartColors[colorName];
const newDataset = {
label: 'Dataset ' + boxplotData.datasets.length,
backgroundColor: color(dsColor).alpha(0.5).rgbString(),
borderColor: dsColor,
borderWidth: 1,
data: samples.boxplots({ count: boxplotData.labels.length }),
};
boxplotData.datasets.push(newDataset);
myBar.update();
});
document.getElementById('addData').addEventListener('click', () => {
if (boxplotData.datasets.length > 0) {
const month = samples.nextMonth(boxplotData.labels.length);
boxplotData.labels.push(month);
for (let index = 0; index < boxplotData.datasets.length; ++index) {
//window.myBar.addData(randomBoxPlot(), index);
boxplotData.datasets[index].data.push(samples.randomBoxPlot());
}
myBar.update();
}
});
document.getElementById('removeDataset').addEventListener('click', () => {
boxplotData.datasets.splice(0, 1);
myBar.update();
});
document.getElementById('removeData').addEventListener('click', () => {
boxplotData.labels.splice(-1, 1); // remove the label first
boxplotData.datasets.forEach((dataset) => {
dataset.data.pop();
});
myBar.update();
});
};
</script>
</body>
</html>