Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 19fb4bc

Browse files
committed
fix #27 handle empty array during scale calc
1 parent 781345c commit 19fb4bc

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

samples/empty.html

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<title>Box Plot Chart</title>
6+
<script src="../node_modules/chart.js/dist/Chart.bundle.js"></script>
7+
<script src="../build/Chart.BoxPlot.js" type="text/javascript"></script>
8+
<style>
9+
canvas {
10+
-moz-user-select: none;
11+
-webkit-user-select: none;
12+
-ms-user-select: none;
13+
}
14+
</style>
15+
</head>
16+
17+
<body>
18+
<div id="container" style="width: 75%;">
19+
<canvas id="canvas"></canvas>
20+
</div>
21+
<script>
22+
function randomValues(count, min, max) {
23+
const delta = max - min;
24+
return Array.from({length: count}).map(() => Math.random() * delta + min);
25+
}
26+
const boxplotData = {
27+
labels: ['A', 'B'],
28+
datasets: [{
29+
label: 'Dataset 1',
30+
borderColor: 'red',
31+
borderWidth: 1,
32+
outlierRadius: 3,
33+
itemRadius: 3,
34+
outlierColor: '#999999',
35+
data: [
36+
[],
37+
randomValues(100, 0, 10)
38+
],
39+
}]
40+
};
41+
42+
window.onload = function() {
43+
var ctx = document.getElementById("canvas").getContext("2d");
44+
window.myBar = new Chart(ctx, {
45+
type: 'boxplot',
46+
data: boxplotData,
47+
options: {
48+
responsive: true,
49+
legend: {
50+
position: 'top',
51+
},
52+
title: {
53+
display: true,
54+
text: 'Chart.js Box Plot Chart'
55+
},
56+
scales: {
57+
xAxes: [{
58+
// Specific to Bar Controller
59+
categoryPercentage: 0.9,
60+
barPercentage: 0.8
61+
}]
62+
}
63+
}
64+
});
65+
};
66+
</script>
67+
</body>
68+
69+
</html>

src/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function asValueStats(value, minStats, maxStats) {
139139
if (typeof value[minStats] === 'number' && typeof value[maxStats] === 'number') {
140140
return value;
141141
}
142-
if (!Array.isArray(value)) {
142+
if (!Array.isArray(value) || value.length === 0) {
143143
return undefined;
144144
}
145145
return asBoxPlotStats(value);

0 commit comments

Comments
 (0)