StatPanels: Fixes auto min max when latest value is zero (#28982)

This commit is contained in:
Torkel Ödegaard 2020-11-11 07:43:56 +01:00 committed by GitHub
parent b5379c5335
commit 10f226c4c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 12 deletions

View File

@ -98,8 +98,30 @@ describe('Global MinMax', () => {
});
const { min, max } = findNumericFieldMinMax([frame]);
expect(min).toBeNull();
expect(max).toBeNull();
expect(min).toBe(Number.MIN_VALUE);
expect(max).toBe(Number.MAX_VALUE);
});
});
describe('when value values are zeo', () => {
it('then global min max should be correct', () => {
const frame = toDataFrame({
fields: [
{ name: 'Time', type: FieldType.time, values: [1, 2] },
{ name: 'Value', type: FieldType.number, values: [1, 2] },
],
});
const frame2 = toDataFrame({
fields: [
{ name: 'Time', type: FieldType.time, values: [1, 2] },
{ name: 'Value', type: FieldType.number, values: [0, 0] },
],
});
const { min, max } = findNumericFieldMinMax([frame, frame2]);
expect(min).toBe(0);
expect(max).toBe(2);
});
});
});

View File

@ -58,25 +58,25 @@ export function findNumericFieldMinMax(data: DataFrame[]): GlobalMinMax {
const statsMin = stats[ReducerID.min];
const statsMax = stats[ReducerID.max];
if (!statsMin) {
if (statsMin !== undefined && statsMin !== null && statsMin < min) {
min = statsMin;
}
if (!statsMax) {
max = statsMax;
}
if (statsMin && statsMin < min) {
min = statsMin;
}
if (statsMax && statsMax > max) {
if (statsMax !== undefined && statsMax !== null && statsMax > max) {
max = statsMax;
}
}
}
}
if (min === Number.MAX_VALUE) {
min = Number.MIN_VALUE;
}
if (max === Number.MIN_VALUE) {
max = Number.MAX_VALUE;
}
return { min, max };
}