From 1d56bd112582f50aa4b3510c1e8fc054d7b00031 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Tue, 23 Aug 2022 10:22:35 +0200 Subject: [PATCH] TimeSeries: Fix crash when min >= max in config (#54069) (#54077) * TimeSeries: fix crash when min === max in config * stat sparkline, too * better types (cherry picked from commit 530dd63ac62fac5d5f5a4ece3081db0f9ceb6709) Co-authored-by: Leon Sorokin --- .betterer.results | 3 +-- .../grafana-ui/src/components/Sparkline/Sparkline.tsx | 9 ++++----- .../src/components/uPlot/config/UPlotScaleBuilder.ts | 6 ++++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.betterer.results b/.betterer.results index a1fb3cf1be9..75c095e1d2a 100644 --- a/.betterer.results +++ b/.betterer.results @@ -2040,8 +2040,7 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "5"] ], "packages/grafana-ui/src/components/Sparkline/Sparkline.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"] + [0, 0, 0, "Do not use any type assertions.", "0"] ], "packages/grafana-ui/src/components/Spinner/Spinner.story.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] diff --git a/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx b/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx index 270b103aa9f..ba02f8db302 100644 --- a/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx +++ b/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx @@ -93,7 +93,7 @@ export class Sparkline extends PureComponent { } } - getYRange(field: Field) { + getYRange(field: Field): Range.MinMax { let { min, max } = this.state.alignedDataFrame.fields[1].state?.range!; if (min === max) { @@ -103,12 +103,11 @@ export class Sparkline extends PureComponent { min = 0; max! *= 2; } + + return [min, max!]; } - return [ - Math.max(min!, field.config.min ?? -Infinity), - Math.min(max!, field.config.max ?? Infinity), - ] as Range.MinMax; + return [Math.max(min!, field.config.min ?? -Infinity), Math.min(max!, field.config.max ?? Infinity)]; } prepareConfig(data: DataFrame) { diff --git a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts index db2bb0309d1..448d68d1f15 100644 --- a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts +++ b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts @@ -83,6 +83,12 @@ export class UPlotScaleBuilder extends PlotConfigBuilder { minMax[1] = hardMax!; } + // guard against invalid y ranges + if (minMax[0]! >= minMax[1]!) { + minMax[0] = 0; + minMax[1] = 100; + } + return minMax; };