From 9bb7d77cdb3c38ae1fb7c6f1df8ec955bb24e6f7 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Mon, 4 Nov 2024 15:09:28 -0600 Subject: [PATCH] [v11.3.x] Timeseries: Utilize min/max on stacking percentage (#95793) Timeseries: Utilize min/max on stacking percentage (#95581) * Bring in defined min/max into stacking range * simplify logic * different approach --------- Co-authored-by: Leon Sorokin (cherry picked from commit 68aefc73b6e7f240469bab404e3d4950c74218f1) Co-authored-by: Kristina --- .../uPlot/config/UPlotScaleBuilder.ts | 14 ++++++++++- .../app/core/components/TimeSeries/utils.ts | 25 ++++++++----------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts index 47a0595e12b..450486f269f 100644 --- a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts +++ b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts @@ -1,7 +1,7 @@ import uPlot, { Scale, Range } from 'uplot'; import { DecimalCount, incrRoundDn, incrRoundUp, isBooleanUnit } from '@grafana/data'; -import { ScaleOrientation, ScaleDirection, ScaleDistribution } from '@grafana/schema'; +import { ScaleOrientation, ScaleDirection, ScaleDistribution, StackingMode } from '@grafana/schema'; import { PlotConfigBuilder } from '../types'; @@ -20,6 +20,7 @@ export interface ScaleProps { linearThreshold?: number; centeredZero?: boolean; decimals?: DecimalCount; + stackingMode?: StackingMode; } export class UPlotScaleBuilder extends PlotConfigBuilder { @@ -41,8 +42,19 @@ export class UPlotScaleBuilder extends PlotConfigBuilder { orientation, centeredZero, decimals, + stackingMode, } = this.props; + if (stackingMode === StackingMode.Percent) { + if (hardMin == null && softMin == null) { + softMin = 0; + } + + if (hardMax == null && softMax == null) { + softMax = 1; + } + } + const distr = this.props.distribution; const distribution = !isTime diff --git a/public/app/core/components/TimeSeries/utils.ts b/public/app/core/components/TimeSeries/utils.ts index 71a2a740375..8774299b1ed 100644 --- a/public/app/core/components/TimeSeries/utils.ts +++ b/public/app/core/components/TimeSeries/utils.ts @@ -247,24 +247,19 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn = ({ softMin: customConfig.axisSoftMin, softMax: customConfig.axisSoftMax, centeredZero: customConfig.axisCenteredZero, + stackingMode: customConfig.stacking?.mode, range: - customConfig.stacking?.mode === StackingMode.Percent + field.type === FieldType.enum ? (u: uPlot, dataMin: number, dataMax: number) => { - dataMin = dataMin < 0 ? -1 : 0; - dataMax = dataMax > 0 ? 1 : 0; - return [dataMin, dataMax]; + // this is the exhaustive enum (stable) + let len = field.config.type!.enum!.text!.length; + + return [-1, len]; + + // these are only values that are present + // return [dataMin - 1, dataMax + 1] } - : field.type === FieldType.enum - ? (u: uPlot, dataMin: number, dataMax: number) => { - // this is the exhaustive enum (stable) - let len = field.config.type!.enum!.text!.length; - - return [-1, len]; - - // these are only values that are present - // return [dataMin - 1, dataMax + 1] - } - : undefined, + : undefined, decimals: field.config.decimals, }, field