Skip to content

Commit 9ec2240

Browse files
committed
fix: guard against empty points array and Plotly render errors
1 parent ee80317 commit 9ec2240

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

ide/templates/plotusers.html

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,35 @@
2626
return year + (month - 1) / 12;
2727
}
2828

29-
const xs = points.map(p => toDecimalYear(p.month));
30-
const ys = points.map(p => p.count);
29+
if (points.length === 0) {
30+
document.getElementById('chart').innerHTML = '<p class="no-data">No data points available.</p>';
31+
} else {
32+
const xs = points.map(p => toDecimalYear(p.month));
33+
const ys = points.map(p => p.count);
3134

32-
const lastPoint = points[points.length - 1];
33-
const formattedDate = updated ? updated.replace(/-/g, '/') : '';
34-
const formattedCount = lastPoint.count.toLocaleString();
35-
const title = `As of ${formattedDate} there were ${formattedCount} Web VPython accounts.`;
35+
const lastPoint = points[points.length - 1];
36+
const formattedDate = updated ? updated.replace(/-/g, '/') : '';
37+
const formattedCount = lastPoint.count.toLocaleString();
38+
const title = `As of ${formattedDate} there were ${formattedCount} Web VPython accounts.`;
3639

37-
Plotly.newPlot('chart', [{
38-
x: xs,
39-
y: ys,
40-
mode: 'lines+markers',
41-
type: 'scatter',
42-
marker: { color: 'red', size: 4 },
43-
line: { color: 'red', width: 1 }
44-
}], {
45-
title: title,
46-
xaxis: { title: 'Year' },
47-
yaxis: { title: 'Web VPython accounts' },
48-
margin: { t: 60 }
49-
});
40+
try {
41+
Plotly.newPlot('chart', [{
42+
x: xs,
43+
y: ys,
44+
mode: 'lines+markers',
45+
type: 'scatter',
46+
marker: { color: 'red', size: 4 },
47+
line: { color: 'red', width: 1 }
48+
}], {
49+
title: title,
50+
xaxis: { title: 'Year' },
51+
yaxis: { title: 'Web VPython accounts' },
52+
margin: { t: 60 }
53+
});
54+
} catch (e) {
55+
document.getElementById('chart').innerHTML = '<p class="no-data">Chart failed to render: ' + e.message + '</p>';
56+
}
57+
}
5058
</script>
5159
{% endif %}
5260
</body>

0 commit comments

Comments
 (0)