[Chartjs]-How to ignore points with same value in Chart.js

3👍

First off, use the following function to factor your data :

function factorData(data) {
   let _data = data.map((e, i, a) => {
      let prev = a[i - 1];
      let next = a[i + 1];
      if (e === prev && e === next) return '' + e;
      return e;
   }).map(e => typeof e === 'string' ? null : e);
   return _data;
}

then, set the spanGaps property to true for your dataset, like so :

...
datasets: [{
         data: factorData(eventCounts),
         spanGaps: true
         ...

see a working example.

1👍

Improving on ɢʀᴜɴᴛ’s answer

To increase performance by 2x:

function factorData(data) {
   return data.map((e, i, a) => {
      let prev = a[i - 1];
      let next = a[i + 1];
      if (e === prev && e === next) return null;
      return e;
   });
}

(not iterating over the array a second time)

Also don’t forget!

...
datasets: [{
  data: factorData(eventCounts),
  spanGaps: true
...

Leave a comment