From d3815a851cbda28c9261171af6cff51e7b481c64 Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Thu, 7 Oct 2021 03:48:49 -0500 Subject: [PATCH] Stat: recompute shared y range during streaming updates (#39485) --- public/app/plugins/panel/stat/StatPanel.tsx | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/public/app/plugins/panel/stat/StatPanel.tsx b/public/app/plugins/panel/stat/StatPanel.tsx index 771a054397a..13a62627b47 100644 --- a/public/app/plugins/panel/stat/StatPanel.tsx +++ b/public/app/plugins/panel/stat/StatPanel.tsx @@ -10,14 +10,18 @@ import { import { DisplayValueAlignmentFactors, FieldDisplay, + FieldType, getDisplayValueAlignmentFactors, getFieldDisplayValues, + NumericRange, PanelProps, } from '@grafana/data'; import { config } from 'app/core/config'; import { StatPanelOptions } from './types'; import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu'; +import { findNumericFieldMinMax } from '@grafana/data/src/field/fieldOverrides'; +import { isNumber } from 'lodash'; export class StatPanel extends PureComponent> { renderComponent = ( @@ -83,6 +87,28 @@ export class StatPanel extends PureComponent> { getValues = (): FieldDisplay[] => { const { data, options, replaceVariables, fieldConfig, timeZone } = this.props; + let globalRange: NumericRange | undefined = undefined; + + for (let frame of data.series) { + for (let field of frame.fields) { + let { config } = field; + // mostly copied from fieldOverrides, since they are skipped during streaming + // Set the Min/Max value automatically + if (field.type === FieldType.number) { + if (field.state?.range) { + continue; + } + if (!globalRange && (!isNumber(config.min) || !isNumber(config.max))) { + globalRange = findNumericFieldMinMax(data.series); + } + const min = config.min ?? globalRange!.min; + const max = config.max ?? globalRange!.max; + field.state = field.state ?? {}; + field.state.range = { min, max, delta: max! - min! }; + } + } + } + return getFieldDisplayValues({ fieldConfig, reduceOptions: options.reduceOptions,