Thresholds: Fixes color assigned to null values (#29010)

* Thresholds: Fixes color assigned to null values

* Updates
This commit is contained in:
Torkel Ödegaard 2020-11-11 11:57:55 +01:00 committed by GitHub
parent ab078133bf
commit 145901c0db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 5 deletions

View File

@ -16,9 +16,8 @@ function getDisplayProcessorFromConfig(config: FieldConfig) {
function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) {
processors.forEach(processor => {
const value = processor(input);
expect(value.text).toEqual(match.text);
if (match.hasOwnProperty('numeric')) {
expect(value.numeric).toEqual(match.numeric);
for (const key of Object.keys(match)) {
expect((value as any)[key]).toEqual((match as any)[key]);
}
});
}
@ -89,6 +88,27 @@ describe('Process simple display values', () => {
});
});
describe('Process null values', () => {
const processors = [
getDisplayProcessorFromConfig({
min: 0,
max: 100,
thresholds: {
mode: ThresholdsMode.Absolute,
steps: [
{ value: -Infinity, color: '#000' },
{ value: 0, color: '#100' },
{ value: 100, color: '#200' },
],
},
}),
];
it('Null should get -Infinity (base) color', () => {
assertSame(null, processors, { text: '', numeric: NaN, color: '#000' });
});
});
describe('Format value', () => {
it('should return if value isNaN', () => {
const valueMappings: ValueMapping[] = [];

View File

@ -115,7 +115,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
}
}
return { text, numeric, prefix, suffix, ...scaleFunc(0) };
return { text, numeric, prefix, suffix, ...scaleFunc(-Infinity) };
};
}

View File

@ -18,7 +18,12 @@ export function getScaleCalculator(field: Field, theme: GrafanaTheme): ScaleCalc
const info = getMinMaxAndDelta(field);
return (value: number) => {
const percent = (value - info.min!) / info.delta;
let percent = 0;
if (value !== -Infinity) {
percent = (value - info.min!) / info.delta;
}
const threshold = getActiveThresholdForValue(field, value, percent);
return {