From a318b15cab92011d2642e687944a30d75a72d1dd Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Fri, 21 Oct 2022 21:12:16 +0200 Subject: [PATCH] TimeSeries: Fix stacking when first value is negative zero (#57257) (#57472) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * TimeSeries: Fix stacking when first value is negative zero * More test + refactor (cherry picked from commit 7f3b567657b87ea0cd52cdf5efce066bf3c4f596) Co-authored-by: Zoltán Bedi --- .../src/components/uPlot/utils.test.ts | 12 ++++++++++ .../grafana-ui/src/components/uPlot/utils.ts | 22 +++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/grafana-ui/src/components/uPlot/utils.test.ts b/packages/grafana-ui/src/components/uPlot/utils.test.ts index 3f9f6977a8d..e4a5d899e1b 100644 --- a/packages/grafana-ui/src/components/uPlot/utils.test.ts +++ b/packages/grafana-ui/src/components/uPlot/utils.test.ts @@ -1061,6 +1061,11 @@ describe('auto stacking groups', () => { values: [0, 0, 0], config: { custom: { stacking: { mode: StackingMode.Normal } } }, }, + { + name: 'd', + values: [-0, -10, -20], + config: { custom: { stacking: { mode: StackingMode.Normal } } }, + }, ], }); @@ -1070,6 +1075,7 @@ describe('auto stacking groups', () => { "dir": -1, "series": Array [ 1, + 4, ], }, Object { @@ -1102,6 +1108,11 @@ describe('auto stacking groups', () => { values: [0, 0, 0], config: { custom: { stacking: { mode: StackingMode.Normal } } }, }, + { + name: 'd', + values: [-0, null, 3], + config: { custom: { stacking: { mode: StackingMode.Normal }, transform: GraphTransform.NegativeY } }, + }, ], }); @@ -1113,6 +1124,7 @@ describe('auto stacking groups', () => { 1, 2, 3, + 4, ], }, ] diff --git a/packages/grafana-ui/src/components/uPlot/utils.ts b/packages/grafana-ui/src/components/uPlot/utils.ts index 5e0253fb0d2..9be91616cb8 100644 --- a/packages/grafana-ui/src/components/uPlot/utils.ts +++ b/packages/grafana-ui/src/components/uPlot/utils.ts @@ -117,18 +117,7 @@ export function getStackingGroups(frame: DataFrame) { let vals = values.toArray(); let transform = custom.transform; let firstValue = vals.find((v) => v != null); - let stackDir = - transform === GraphTransform.Constant - ? firstValue >= 0 - ? StackDirection.Pos - : StackDirection.Neg - : transform === GraphTransform.NegativeY - ? firstValue >= 0 - ? StackDirection.Neg - : StackDirection.Pos - : firstValue >= 0 - ? StackDirection.Pos - : StackDirection.Neg; + let stackDir = getStackDirection(transform, firstValue); let drawStyle = custom.drawStyle as GraphDrawStyle; let drawStyle2 = @@ -352,6 +341,15 @@ export function findMidPointYPosition(u: uPlot, idx: number) { return y; } +function getStackDirection(transform: GraphTransform, firstValue: number) { + // Check if first value is negative zero. This can happen with a binary operation transform. + const isNegativeZero = Object.is(firstValue, -0); + if (transform === GraphTransform.NegativeY) { + return !isNegativeZero && firstValue >= 0 ? StackDirection.Neg : StackDirection.Pos; + } + return !isNegativeZero && firstValue >= 0 ? StackDirection.Pos : StackDirection.Neg; +} + // Dev helpers /** @internal */