score:1

i suspect that it works in fact correctly.

code sample before some possible explaination:

var ctx = document.getelementbyid("mychart");
var nbpoints = 1000;
var samplespoints = 100;
var thresholdspoints = 900;
var dataarr = []
for (let i = 0; i < nbpoints; i++) {
  dataarr.push({x: i, y: math.floor(math.random() * 100)})
}
var mychart = new chart(ctx, {
    type: 'line',
    data: {
        datasets: [{
            label: '# of votes',
            data: dataarr
        }]
    },
    options: {
      parsing: false,
      normalized: true,
      animation: false,
      responsive: false,
      plugins: {
        decimation: {
          enabled: true,
          samples: samplespoints,
          threshold: thresholdspoints,
          algorithm: 'lttb'
        },
        zoom: {
          limits: {
            x: { min: 0, max: nbpoints }
          },
          pan: {
            enabled: true,
            mode: 'x'
          },
          zoom: {
            wheel: {
              enabled: true
            },
            pinch: {
              enabled: true
            },
            mode: 'x'
          }
        }
      },
      scales: {
        x: {
          type: 'linear'
        }
      }
    }
});

function resetzoom() {
  mychart.resetzoom();
}
.mychartdiv {
  max-width: 600px;
  max-height: 400px;
}
<script src="https://npmcdn.com/chart.js@3.7.1/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
<script src="https://npmcdn.com/chartjs-plugin-zoom@1.2.1/dist/chartjs-plugin-zoom.min.js"></script>
<div class="mychartdiv">
  <canvas id="mychart" width="600" height="400"></canvas>
</div>
<div class="mybutton">
  <button onclick="resetzoom()">reset zoom</button>
</div>

it was due to a modification that was proposed a while ago: the idea was to re-perform decimation each time you zoom in order to have the "highest resolution" possible (for history: https://github.com/chartjs/chart.js/issues/8833)

but the thing that you probably missed (i suppose) are the following properties of the decimation plugin (https://www.chartjs.org/docs/master/configuration/decimation.html#configuration-options):

  • samples: how many points you want to have after decimation
  • threshold: above which number of point you want the decimation happen. often, you might want samples = threshold, but that is not mandatory.

and the "problem" is probably the default values of each of these:

  • sample: defaults to the canvas width to pick 1 sample per pixel.
  • threshold: defaults to 4 times the canvas width.

meaning that for a 800px graph, you will have 800 points, and decimation will happen only if you have more than 800*4 points on the current range.

so what i suppose is happening: you have let say 1000 points that you display on a 200px graph. at first everything is ok, but once you zoom, you have 750 points, which will be less than 200*4, so decimation won't happen and you will have in fact 750 points (while you would expect 200)

in the end, you might want to update your decimation plugin configuration with something like:

decimation: {
    enabled: true,
    algorithm: 'lttb',
    samples: 800,
    threshold: 800
}

Related Query

More Query from same tag