Chart js: when all the values passed to data are zeros - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Chart js: when all the values passed to data are zeros

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.6.0/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from w  w w  . j  ava 2s.  co  m
Chart.plugins.register({
   beforeInit: function(chart) {
      var data = chart.data.datasets[0].data;
      var isAllZero = data.reduce((a, b) => a + b) > 0 ? false : true;
      if (!isAllZero) 
         return;
      chart.data.datasets[0].data = data.map((e, i) => i > 0 ? 0 : 1); 
      chart.data.datasets[0].backgroundColor = '#d2dee2'; 
      chart.data.datasets[0].borderWidth = 0; 
      chart.options.tooltips = false; 
      chart.options.legend.onClick = null; 
   }
});
var chart = new Chart(ctx, {
   type: 'doughnut',
   data: {
      labels: ['Red', 'Green', 'Yellow'],
      datasets: [{
         data: [0, 0, 0],
         backgroundColor: ['#F7464A', '#46BFBD', '#FDB45C']
      }]
   }
});
    }

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

Related Tutorials