Chartjs is wildly slow with large numbers but small data set
Expected Behavior
The chart should render in < 500ms
Current Behavior
It takes 8 seconds to render
Possible Solution
No clue!
Steps to Reproduce
The code below attached to a canvas will take something like 8 seconds to load. The cause is the first value in the array which is large. 10 million. Frankly that's not a huge number. If you cut that number down to 100,000 or even 1,000,000 it will render significantly faster.
<div class="chart">
<canvas class="chart-canvas" id="salesChart"></canvas>
</div>
<script type="text/javascript">
new Chart('salesChart', {
type: 'bar',
options: {
animation: false,
// parsing: false,
scales: {
y: {
ticks: {
callback: function(value) {
return '$' + value.toLocaleString();
}
}
},
x: {
ticks: {
// For a category axis, the val is the index so the lookup via getLabelForValue is needed
callback: function(val, index) {
// Hide the label of every 2nd dataset
return index % 3 === 0 ? this.getLabelForValue(val) : '';
}
}
}
}
},
data: {
labels: ["Dec 1", "Dec 2", "Dec 3", "Dec 4", "Dec 5", "Dec 6"],
datasets: [{
label: 'Sales volume',
data: [10000000, 300, 400, 500, 600, 700] // NOTE: the large number at the beginning
}]
}
});
</script>
Context
It's causing my site to load incredibly slow. I tried disabling animation but that doesn't accomplish anything. I'm kind of stuck for the moment. I have large numbers in my data sets (albeit I didn't think millions was a large number).
Environment
- Chart.js version: 3.3.0
- Browser name and version: Chrome
- Link to your project: You can reproduce the issue on this page: https://www.thenifty.com/nft_collection/cryptopunks-375
No it does not: https://codepen.io/kurkle/pen/jOwZXGK
The issue is with the stepSize: 10 you have, that says create me 1000000 ticks for y-axis (and call the formatting function for those)
As this is the 2nd time (that I remember) this is reported, I'd guess there are more people stumbling on this. Maybe we should add a sanity check for the step size.
A sanity check (with a console.warn) makes sense to me