From 6570ec7afe3e58591ece5a830ada4f37c5f840dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Wed, 3 May 2023 13:37:45 +0200 Subject: [PATCH] Gauge: Set min and max for percent unit (#67517) * Gauge: Set min and max for percent unit * Remove exclamation mark * Add parentheses around ternary --- public/app/plugins/panel/gauge/GaugePanel.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/panel/gauge/GaugePanel.tsx b/public/app/plugins/panel/gauge/GaugePanel.tsx index 8254065cd0b..1ac500ed62e 100644 --- a/public/app/plugins/panel/gauge/GaugePanel.tsx +++ b/public/app/plugins/panel/gauge/GaugePanel.tsx @@ -1,6 +1,6 @@ import React, { PureComponent } from 'react'; -import { FieldDisplay, getFieldDisplayValues, PanelProps } from '@grafana/data'; +import { FieldDisplay, getDisplayProcessor, getFieldDisplayValues, PanelProps } from '@grafana/data'; import { DataLinksContextMenu, Gauge, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui'; import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu'; import { config } from 'app/core/config'; @@ -54,6 +54,19 @@ export class GaugePanel extends PureComponent> { getValues = (): FieldDisplay[] => { const { data, options, replaceVariables, fieldConfig, timeZone } = this.props; + + for (let frame of data.series) { + for (let field of frame.fields) { + // Set the Min/Max value automatically for percent and percentunit + if (field.config.unit === 'percent' || field.config.unit === 'percentunit') { + const min = field.config.min ?? 0; + const max = field.config.max ?? (field.config.unit === 'percent' ? 100 : 1); + field.state = field.state ?? {}; + field.state.range = { min, max, delta: max - min }; + field.display = getDisplayProcessor({ field, theme: config.theme2 }); + } + } + } return getFieldDisplayValues({ fieldConfig, reduceOptions: options.reduceOptions,