Skip to content Skip to sidebar Skip to footer

Chartjs Bar Issue - Showing "zeros" And Not Center Bars

Im having some trouble with ChartJs and hope you can help. I would like to create a Bar Chart where: Zero Lables that only contains dataset with zeros are not showing If only one

Solution 1:

ChartJS and the DevExtreme chart are 2 different things. If you want the functionality of dxChart in chartJs then you would have to write it yourself (if you don't have access to the source code to see how they did it).

Helping you with the first point is easy - simply unset the data you don't want. Check here. Don't forget to remove the label you don't want to show.

for (let i = 0; i <= Data1.data.length; i++){
  if (Data1.data[i] === 0 && Data2.data[i] === 0) {
    Data2.data.splice(i, 1);
    Data1.data.splice(i, 1);
    labels.splice(i, 1); // also remove the corresponding label
    i--; // important not to skip any records after removing
  }
}

However this sort of data manipulation I would recommend doing in the back end. Just don't set it before sending to the front end and you're done.

For your second point it looks like chartJs cannot help. There is a reason why dxChart is paid and chartJs is free. Learn to make compromises or become a JS ninja. Cheers

Solution 2:

Changing intersect to false for the tooltips should fix the problem. (Intersect: true only shows the tooltips if you're hovering over a datapoint, of which there are none if it's zero)

tooltips configuration can be found here: https://www.chartjs.org/docs/2.6.0/configuration/tooltip.html

varchartOptions= {
  scales: {
    xAxes: [{
      barPercentage:1,
      categoryPercentage:0.6
    }],
    yAxes: [{
      id:"y-axis"
    }]
  },
  tooltips: {
    intersect:false
  }
};

Post a Comment for "Chartjs Bar Issue - Showing "zeros" And Not Center Bars"