diff --git a/packages/grafana-ui/src/components/uPlot/config/UPlotConfigBuilder.test.ts b/packages/grafana-ui/src/components/uPlot/config/UPlotConfigBuilder.test.ts index 4936f853c1e..01a17fb41b0 100644 --- a/packages/grafana-ui/src/components/uPlot/config/UPlotConfigBuilder.test.ts +++ b/packages/grafana-ui/src/components/uPlot/config/UPlotConfigBuilder.test.ts @@ -281,6 +281,33 @@ describe('UPlotConfigBuilder', () => { }); }); + it('disables autoscaling when both (and only) hardMin and hardMax are specified', () => { + const builder = new UPlotConfigBuilder(); + + builder.addScale({ + isTime: false, + scaleKey: 'scale-y', + orientation: ScaleOrientation.Vertical, + direction: ScaleDirection.Up, + min: -100, + max: 100, + }); + + expect(builder.getConfig().scales!['scale-y']!.auto).toEqual(false); + + builder.addScale({ + isTime: false, + scaleKey: 'scale-y2', + orientation: ScaleOrientation.Vertical, + direction: ScaleDirection.Up, + min: -100, + max: 100, + softMin: -50, + }); + + expect(builder.getConfig().scales!['scale-y2']!.auto).toEqual(true); + }); + it('allows axes configuration', () => { const builder = new UPlotConfigBuilder(); diff --git a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts index 57a2dab9436..b9123890cc5 100644 --- a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts +++ b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts @@ -65,15 +65,15 @@ export class UPlotScaleBuilder extends PlotConfigBuilder { }, }; + let hardMinOnly = softMin == null && hardMin != null; + let hardMaxOnly = softMax == null && hardMax != null; + // uPlot range function const rangeFn = (u: uPlot, dataMin: number, dataMax: number, scaleKey: string) => { const scale = u.scales[scaleKey]; let minMax = [dataMin, dataMax]; - let hardMinOnly = softMin == null && hardMin != null; - let hardMaxOnly = softMax == null && hardMax != null; - if (scale.distr === 1 || scale.distr === 2) { // @ts-ignore here we may use hardMin / hardMax to make sure any extra padding is computed from a more accurate delta minMax = uPlot.rangeNum(hardMinOnly ? hardMin : dataMin, hardMaxOnly ? hardMax : dataMax, rangeConfig); @@ -96,7 +96,7 @@ export class UPlotScaleBuilder extends PlotConfigBuilder { return { [scaleKey]: { time: isTime, - auto: !isTime, + auto: !isTime && !(hardMinOnly && hardMaxOnly), range: range ?? rangeFn, dir: direction, ori: orientation,