tmroyal / Chart.HeatMap

A heat map plugin for chart.js
MIT License
27 stars 18 forks source link

scaleFactor calculation for gradient could cause divide by zero error… #6

Open dustradio opened 7 years ago

dustradio commented 7 years ago

This should fix:

https://github.com/tmroyal/Chart.HeatMap/issues/2

If the range of heatmap data isn't sufficiently large you can get divide by zero errors. Looks like maybe should be adding one here instead of subtracting.

Not sure what version of Charts.js you were basing this off off, so I didn't rebuild the whole project, just changed the main source file.

sharozmirza commented 7 years ago

I fixed this issue by using a conditional statement before it calculates the scaleFactor for 'gradient' colorInterpolation. Since I was not having any issue if the datalength is equal to zero, I just used the conditional statement for datalength 1 or 2. I tried to use your fix but I was not getting the exact color gradient, so here it looks like in my code:

var colorFunction, scaleFactor;
var dataLength = max-min;
var base = min;
if (dataLength === 1 || dataLength === 2){
      colorInterpolation = 'palette';
}
if (colorInterpolation === 'gradient'){
  colorFunction = getGradientColor;
  scaleFactor = (colors.length-1)/(dataLength -1);
} else {
  colorFunction = getIndexedColor;
  scaleFactor = (colors.length)/(dataLength+1);
}