Ignore points with same value in Chart.js - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Ignore points with same value in Chart.js

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from w  w w. j av  a  2 s .c om
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;
}
var eventCounts = [3, 1, 1, 1, 4, 2, 5];
var myChart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'],
      datasets: [{
         label: 'LINE',
         data: factorData(eventCounts),
         backgroundColor: 'rgba(0, 119, 290, 0.2)',
         borderColor: 'rgba(0, 119, 290, 0.6)',
         lineTension: 0,
         spanGaps: true
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 1
            }
         }]
      }
   }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="ctx"></canvas>  
   </body>
</html>

Related Tutorials