Home Forums Chart Support What if, all y : data is 0 ? Reply To: What if, all y : data is 0 ?

#18595

@nik-sol,

One more help in similar case,
We need to hide legends in this situation as well.

You can toggle showInLegend property if you are looking to hide legend for dataPoints with y-values as 0 or null as shown in the code snippet below:

showDefaultText(chart, "No Data Found!");

function showDefaultText(chart, text) {
  var dataPoints = chart.options.data[0].dataPoints;
  var isEmpty = !(dataPoints && dataPoints.length > 0);

  if (!isEmpty) {
    for (var i = 0; i < dataPoints.length; i++) {
      isEmpty = !dataPoints[i].y;
      if (!isEmpty)
        break;
    }
  }

  if (!chart.options.subtitles)
    chart.options.subtitles = [];
  if (isEmpty) {
    chart.options.subtitles.push({
      text: text,
      verticalAlign: 'center',
    });
    chart.options.data[0].showInLegend = false;
  } else {
    chart.options.subtitles = [];
    chart.options.data[0].showInLegend = true;
  }
}

Please take a look at this updated JSFiddle for a working example with sample code.

CanvasJS pie chart with no data found message and no legend


Sanjoy Debnath
Team CanvasJS