BarGauge: Show empty bar when value, minValue and maxValue are all equal (#53314) (#53371)

* prevent returning NaN from getValuePercent

* return 0 instead of NaN always

(cherry picked from commit 2fea3f0d9a)

Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2022-08-08 04:29:57 -04:00
committed by GitHub
co-authored by Ashley Harrison
parent f9a0926c59
commit 66a3c0fa7a
2 changed files with 7 additions and 1 deletions
@@ -159,6 +159,10 @@ describe('BarGauge', () => {
it('-30 to 30 and value 30', () => {
expect(getValuePercent(30, -30, 30)).toEqual(1);
});
it('returns 0 if the min, max and value are all the same value', () => {
expect(getValuePercent(25, 25, 25)).toEqual(0);
});
});
describe('Vertical bar', () => {
@@ -430,7 +430,9 @@ export function getCellColor(
}
export function getValuePercent(value: number, minValue: number, maxValue: number): number {
return Math.min((value - minValue) / (maxValue - minValue), 1);
// Need special logic for when minValue === maxValue === value to prevent returning NaN
const valueRatio = Math.min((value - minValue) / (maxValue - minValue), 1);
return isNaN(valueRatio) ? 0 : valueRatio;
}
/**