score:0

the plugin core api offers a range of hooks that may be used for performing custom code. you can use the afterdraw hook to write the text "no data" directly on the canvas using canvasrenderingcontext2d in case the sum of all data values is zero.

please take a look at your amended code and see how it works.

new chart('doughnutchart', {
  type: 'doughnut',
  plugins: [{
    afterdraw: function(chart) {
      let sum = chart.data.datasets[0].data.map(v => parseint(v)).reduce((a, b) => a + b, 0);
      if (sum == 0) {
        let width = chart.chart.width,
          height = chart.chart.height + 35,
          ctx = chart.chart.ctx;
        ctx.save();
        let fontsize = (height / 200).tofixed(2);
        ctx.font = fontsize + "em sans-serif";
        ctx.textbaseline = "middle";
        let text = 'no data',
          textx = math.round((width - ctx.measuretext(text).width) / 2),
          texty = height / 2;
        ctx.filltext(text, textx, texty);
        ctx.restore();
      }
    }
  }],
  data: {
    labels: ['d1', 'd2'],
    datasets: [{
      data: ['0', '0'],
      backgroundcolor: ["#f7464a", "#46bfbd", "#fdb45c", "#949fb1", "#4d5360"],
      hoverbackgroundcolor: ["#ff5a5e", "#5ad3d1", "#ffc870", "#a8b3c5", "#616774"]
    }]
  },
  options: {
    responsive: true
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.4/chart.min.js"></script>
<canvas id="doughnutchart" height="80"></canvas>


Related Query

More Query from same tag