diff --git a/packages/grafana-ui/src/components/uPlot/utils.test.ts b/packages/grafana-ui/src/components/uPlot/utils.test.ts index 873e26ba4c1..8cf0e7c1445 100644 --- a/packages/grafana-ui/src/components/uPlot/utils.test.ts +++ b/packages/grafana-ui/src/components/uPlot/utils.test.ts @@ -88,6 +88,87 @@ describe('preparePlotData', () => { ] `); }); + + it('negative-y transform with null/undefined values', () => { + const df = new MutableDataFrame({ + fields: [ + { name: 'time', type: FieldType.time, values: [9997, 9998, 9999] }, + { name: 'a', values: [-10, 20, 10, 30] }, + { name: 'b', values: [10, 10, 10, null] }, + { name: 'c', values: [null, 20, 20, 20], config: { custom: { transform: GraphTransform.NegativeY } } }, + { name: 'd', values: [20, 20, 20, null], config: { custom: { transform: GraphTransform.NegativeY } } }, + { name: 'e', values: [20, null, 20, 20], config: { custom: { transform: GraphTransform.NegativeY } } }, + { name: 'f', values: [10, 10, 10, undefined] }, + { name: 'g', values: [undefined, 20, 20, 20], config: { custom: { transform: GraphTransform.NegativeY } } }, + { name: 'h', values: [20, 20, 20, undefined], config: { custom: { transform: GraphTransform.NegativeY } } }, + { name: 'i', values: [20, undefined, 20, 20], config: { custom: { transform: GraphTransform.NegativeY } } }, + ], + }); + expect(preparePlotData([df])).toMatchInlineSnapshot(` + Array [ + Array [ + 9997, + 9998, + 9999, + undefined, + ], + Array [ + -10, + 20, + 10, + 30, + ], + Array [ + 10, + 10, + 10, + null, + ], + Array [ + null, + -20, + -20, + -20, + ], + Array [ + -20, + -20, + -20, + null, + ], + Array [ + -20, + null, + -20, + -20, + ], + Array [ + 10, + 10, + 10, + undefined, + ], + Array [ + undefined, + -20, + -20, + -20, + ], + Array [ + -20, + -20, + -20, + undefined, + ], + Array [ + -20, + undefined, + -20, + -20, + ], + ] + `); + }); it('constant transform', () => { const df = new MutableDataFrame({ fields: [ diff --git a/packages/grafana-ui/src/components/uPlot/utils.ts b/packages/grafana-ui/src/components/uPlot/utils.ts index 35cc9b39b65..96ceaf2eb5d 100755 --- a/packages/grafana-ui/src/components/uPlot/utils.ts +++ b/packages/grafana-ui/src/components/uPlot/utils.ts @@ -64,8 +64,9 @@ export function preparePlotData( const customConfig: GraphFieldConfig = f.config.custom || {}; const values = f.values.toArray(); + if (customConfig.transform === GraphTransform.NegativeY) { - result.push(values.map((v) => v * -1)); + result.push(values.map((v) => (v == null ? v : v * -1))); } else if (customConfig.transform === GraphTransform.Constant) { result.push(new Array(values.length).fill(values[0])); } else {