diff --git a/public/app/plugins/panel/timeseries/utils.test.ts b/public/app/plugins/panel/timeseries/utils.test.ts index 78af574e426..a48b861781b 100644 --- a/public/app/plugins/panel/timeseries/utils.test.ts +++ b/public/app/plugins/panel/timeseries/utils.test.ts @@ -1,4 +1,5 @@ import { createTheme, FieldType, createDataFrame, toDataFrame } from '@grafana/data'; +import { LineInterpolation } from '@grafana/ui'; import { prepareGraphableFields } from './utils'; @@ -142,4 +143,39 @@ describe('prepare timeseries graph', () => { `); expect(frames![0].length).toEqual(6); }); + + describe('boolean fields', () => { + it('will set line interpolation to an appropriate mode for boolean fields', () => { + const df = createDataFrame({ + fields: [ + { name: 'time', type: FieldType.time, values: [1, 2, 3] }, + { name: 'a', type: FieldType.boolean, values: [true, false, true] }, + ], + }); + + const frames = prepareGraphableFields([df], createTheme()); + const field = frames![0].fields.find((f) => f.name === 'a'); + expect(field?.config.custom.lineInterpolation).toEqual(LineInterpolation.StepAfter); + expect(df.fields[1].config?.custom).toBeUndefined(); + }); + + // #112194 - mutating this value directly can cause a memory leak + it('does not mutate the underlying lineInterpolation value', () => { + const df = createDataFrame({ + fields: [ + { name: 'time', type: FieldType.time, values: [1, 2, 3] }, + { + name: 'a', + type: FieldType.boolean, + values: [true, false, true], + config: { custom: { lineInterpolation: LineInterpolation.Smooth } }, + }, + ], + }); + + const frames = prepareGraphableFields([df], createTheme()); + expect(df.fields[1].config.custom.lineInterpolation).toEqual(LineInterpolation.Smooth); + expect(frames![0].fields[1].config.custom.lineInterpolation).toEqual(LineInterpolation.StepAfter); + }); + }); }); diff --git a/public/app/plugins/panel/timeseries/utils.ts b/public/app/plugins/panel/timeseries/utils.ts index 2975ff8a219..ac2f7e425ae 100644 --- a/public/app/plugins/panel/timeseries/utils.ts +++ b/public/app/plugins/panel/timeseries/utils.ts @@ -176,12 +176,12 @@ export function prepareGraphableFields( ...field.config, max: 1, min: 0, - custom, + custom: { ...custom }, }; // smooth and linear do not make sense - if (custom.lineInterpolation !== LineInterpolation.StepBefore) { - custom.lineInterpolation = LineInterpolation.StepAfter; + if (config.custom.lineInterpolation !== LineInterpolation.StepBefore) { + config.custom.lineInterpolation = LineInterpolation.StepAfter; } copy = {