Solution 1 :

You can use a global plugin that draws ‘No data available’ text in case no data is available.

Chart.plugins.register({
  afterDraw: chart => {
    if (chart.data.datasets[0].data.length === 0) {
      var ctx = chart.chart.ctx;
      ctx.save();
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.font = "22px Arial";
      ctx.fillStyle = "gray";
      ctx.fillText('No data available', chart.chart.width / 2, chart.chart.height / 2);
      ctx.restore();
    }
  }
});

new Chart(document.getElementById('canvas'), {
  type: 'bar',
  data: {
    labels: [],
    datasets: [{
      data: []
    }]
  },
  options: {
    legend: {
      display: false
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="canvas" height="80"></canvas>

Problem :

I would like to add a background with text saying that there is no data available if it’s the case, using ChartJS V2.9.3 .

For example, I would like to go from this (empty canva) :
empty canvas

To this (background color with text display) :

no more empty background

Do you have any suggestions for doing that ?

Thank you in advance

Comments

Comment posted by jsfiddle.net/x04ptfuu

Check the fiddle for sample implementation

Comment posted by lilouch

Works perflectly ! Thank you !

Comment posted by uminder

Glad I could help you.

By