Chartjs-Cannot read property '_index' of undefined ChartistJs for value 0 or null

0👍

I would suggest to at least handling when ‘item’ has no elements:


 public horizontalBarchartoptions = {
      'onClick' : function (evt, item) {
        if (item != null && item != undefined) {
           if (item.length > 0) {
              var index = item[0]['_index']
              var reportId = latestScanReportID[index];
              routingFunct.navigate(["./resultPage/"+reportId]);
           }
        }
      },
      tooltips: {
              callbacks: {
                  label: function(tooltipItem) {
                      return  " "+ Number(tooltipItem.xLabel)+" issue(s)";
                  }
              }
          },
          scales: {
            yAxes: [{
              scaleLabel: {
                display: true,
                labelString: 'Volume'
              }, 
              display: true,
              labelString: 'Density',
              ticks: {
                  mirror: true,
                  autoSkip: false,
                  padding: -30,
                  fontColor: '#fff'
                }}],
            xAxes: [{
              scaleLabel: {
                display: true,
                labelString: '# Issues'
              }, 
              ticks: {
                min: 0,
                userCallback: function(label, index, labels) {
                  // when the floored value is the same as the value we have a whole number
                  if (Math.ceil(label) === label) {
                      return label;
                  }
                }                
            },
            }]
         }       
        };

Hope this helps.

Leave a comment