Skip to content Skip to sidebar Skip to footer

Show No Data Message When Chart Has No Data

I am trying to show no data message when chart has no data. Is it correct? var ctx = document.getElementById('mycanvas').getContext('2d'); var chart = new Chart(ctx, {

Solution 1:

Easiest is to use conditional rendering.

Assuming you're using AngularJS (you referenced tag), you can use ngIf directive. If data array is empty, then don't display chart.

<canvas id="myChart" ng-if="data != 0"></canvas>
<span ng-if="data == 0">No data</span>

Else, you can solve it in your script, by checking before or after draw the data length. I recommend you this snippet.

Other solution following your wish could be to adapt drawn data to received data.

    if($scope.requestedLeaveTypeCount.length > 0){
      data = {
        labels: ["EL", "AL", "PL", "CL"],
        datasets: [{
            label: "My First dataset",
            backgroundColor: ['#F0CB8C', '#A9D5D4', '#E8A3D7', '#CFA3FD'],
            data: $scope.requestedLeaveTypeCount,
        }]
      }
    } else {
      data = {
        labels: ["No data"],
        datasets: [{
          labels:'No data',
          backgroundColor: ['#D3D3D3'],
          data: [100]
        }]
      }
    }

https://plnkr.co/edit/QXljAeeM4Ul3Y47EPcbq?p=preview


Post a Comment for "Show No Data Message When Chart Has No Data"