Skip to content

Commit

Permalink
Fixed div by 0 issue and assigning max or mid value in these cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Sep 13, 2023
1 parent 6147245 commit 09ca171
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions frontend/src/lib/utils/ColorScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,28 @@ export class ColorScale {
}

private calcNormalizedValue(value: number, min: number, max: number): number {
let normalizedValue = 0;
if (this._gradientType === ColorScaleGradientType.Sequential) {
normalizedValue = (value - min) / (max - min);
} else if (this._gradientType === ColorScaleGradientType.Diverging) {
if (max === min) {
return 1;
}
return (value - min) / (max - min);
}
if (this._gradientType === ColorScaleGradientType.Diverging) {
if (max === min) {
return 0.5;
}
if (value < this._divMidPoint) {
normalizedValue = ((value - min) / (this._divMidPoint - min)) * 0.5;
} else {
normalizedValue = 0.5 * (1 + (value - this._divMidPoint) / (max - this._divMidPoint));
if (this._divMidPoint === min) {
return 0.5;
}
return ((value - min) / (this._divMidPoint - min)) * 0.5;
}
if (this._divMidPoint === max) {
return 0.5;
}
return 0.5 * (1 + (value - this._divMidPoint) / (max - this._divMidPoint));
}

return normalizedValue;
return 0;
}

getColorPalette(): ColorPalette {
Expand Down Expand Up @@ -128,6 +138,12 @@ export class ColorScale {
}

getPlotlyColorScale(): [number, string][] {
if (this._min === this._max) {
return [
[0, this.getColorForValue(this._min)],
[1, this.getColorForValue(this._max)],
];
}
const plotlyColorScale: [number, string][] = [];
for (let i = 0; i <= 100; i++) {
if (i > 0) {
Expand Down

0 comments on commit 09ca171

Please sign in to comment.