FieldDisplay: remove auto min/max for percent units (#34234)

This commit is contained in:
Ryan McKinley 2021-05-17 10:54:06 -07:00 committed by GitHub
parent 8f50b9abb4
commit eba6f66fba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 18 deletions

View File

@ -150,23 +150,6 @@ export function applyFieldOverrides(options: ApplyFieldOverrideOptions): DataFra
}
}
// Some units have an implied range
if (config.unit === 'percent') {
if (!isNumber(config.min)) {
config.min = 0;
}
if (!isNumber(config.max)) {
config.max = 100;
}
} else if (config.unit === 'percentunit') {
if (!isNumber(config.min)) {
config.min = 0;
}
if (!isNumber(config.max)) {
config.max = 1;
}
}
// Set the Min/Max value automatically
let range: NumericRange | undefined = undefined;
if (field.type === FieldType.number) {

View File

@ -229,4 +229,22 @@ describe('sharedSingleStatMigrationHandler', () => {
expect(panel.fieldConfig.defaults.min).toBe(undefined);
expect(panel.fieldConfig.defaults.max).toBe(undefined);
});
it('auto set min/max for percent units before 8.0', () => {
const panel = ({
options: {
fieldOptions: {
defaults: {
unit: 'percentunit',
},
},
},
title: 'Usage',
type: 'bargauge',
} as unknown) as PanelModel;
sharedSingleStatMigrationHandler(panel as any);
expect(panel.fieldConfig.defaults.unit).toBe('percentunit');
expect(panel.fieldConfig.defaults.min).toBe(0);
expect(panel.fieldConfig.defaults.max).toBe(1);
});
});

View File

@ -1,4 +1,4 @@
import { cloneDeep, omit } from 'lodash';
import { cloneDeep, isNumber, omit } from 'lodash';
import {
fieldReducers,
@ -216,6 +216,27 @@ export function sharedSingleStatMigrationHandler(panel: PanelModel<SingleStatBas
}
}
if (previousVersion < 8.0) {
// Explicit min/max was removed for percent/percentunit in 8.0
const config = panel.fieldConfig?.defaults;
let unit = config?.unit;
if (unit === 'percent') {
if (!isNumber(config.min)) {
config.min = 0;
}
if (!isNumber(config.max)) {
config.max = 100;
}
} else if (unit === 'percentunit') {
if (!isNumber(config.min)) {
config.min = 0;
}
if (!isNumber(config.max)) {
config.max = 1;
}
}
}
return options as SingleStatBaseOptions;
}