From a90b3956e037762bac31d871873af2d5e2d43503 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 21 Oct 2021 09:28:52 +0100 Subject: [PATCH] Scale: Fixes handling of NaN percent when data min = data max (#40622) (#40628) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit c174664e636d885251dd1da659715e2b14b15f88) Co-authored-by: Torkel Ödegaard --- packages/grafana-data/src/field/scale.test.ts | 16 +++++++++++++++- packages/grafana-data/src/field/scale.ts | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/grafana-data/src/field/scale.test.ts b/packages/grafana-data/src/field/scale.test.ts index 2e6924ffb8b..da8340a4bef 100644 --- a/packages/grafana-data/src/field/scale.test.ts +++ b/packages/grafana-data/src/field/scale.test.ts @@ -1,4 +1,4 @@ -import { ThresholdsMode, Field, FieldType } from '../types'; +import { ThresholdsMode, Field, FieldType, FieldColorModeId } from '../types'; import { sortThresholds } from './thresholds'; import { ArrayVector } from '../vector/ArrayVector'; import { getScaleCalculator } from './scale'; @@ -49,4 +49,18 @@ describe('getScaleCalculator', () => { threshold: undefined, }); }); + + it('should handle min = max', () => { + const field: Field = { + name: 'test', + config: { color: { mode: FieldColorModeId.ContinuousGrYlRd } }, + type: FieldType.number, + values: new ArrayVector([1]), + }; + + const theme = createTheme(); + const calc = getScaleCalculator(field, theme); + + expect(calc(1).color).toEqual('rgb(115, 191, 105)'); + }); }); diff --git a/packages/grafana-data/src/field/scale.ts b/packages/grafana-data/src/field/scale.ts index 51acd3af63f..f97277966a6 100644 --- a/packages/grafana-data/src/field/scale.ts +++ b/packages/grafana-data/src/field/scale.ts @@ -27,6 +27,10 @@ export function getScaleCalculator(field: Field, theme: GrafanaTheme2): ScaleCal if (value !== -Infinity) { percent = (value - info.min!) / info.delta; + + if (Number.isNaN(percent)) { + percent = 0; + } } const threshold = getActiveThresholdForValue(field, value, percent);