Scale: Fixes handling of NaN percent when data min = data max (#40622) (#40628)

(cherry picked from commit c174664e63)

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
This commit is contained in:
Grot (@grafanabot)
2021-10-21 09:28:52 +01:00
committed by GitHub
co-authored by Torkel Ödegaard
parent f3b8c1a89d
commit a90b3956e0
2 changed files with 19 additions and 1 deletions
+15 -1
View File
@@ -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)');
});
});
+4
View File
@@ -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);