Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Chartjs is wildly slow with large numbers but small data set

Open chooserich opened this issue 5 years ago • 3 comments

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

chooserich avatar Sep 18 '21 03:09 chooserich

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)

kurkle avatar Sep 18 '21 04:09 kurkle

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.

kurkle avatar Sep 18 '21 04:09 kurkle

A sanity check (with a console.warn) makes sense to me

etimberg avatar Sep 20 '21 18:09 etimberg