From d7af7d01c8daf1c5b393f767a7590b5ed9c61b38 Mon Sep 17 00:00:00 2001 From: fowindee Date: Sat, 20 Jan 2024 09:40:43 +0800 Subject: [PATCH] Stat: Support no value in spark line (#78986) --- .../grafana-data/src/transformations/index.ts | 1 + .../components/BigValue/BigValue.story.tsx | 56 +++++++++++++++++++ .../src/components/Sparkline/Sparkline.tsx | 10 +++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/packages/grafana-data/src/transformations/index.ts b/packages/grafana-data/src/transformations/index.ts index 4cdd4ea2f8b..3d3afdebfbe 100644 --- a/packages/grafana-data/src/transformations/index.ts +++ b/packages/grafana-data/src/transformations/index.ts @@ -23,3 +23,4 @@ export { ensureTimeField } from './transformers/convertFieldType'; // Required for Sparklines util to work in @grafana/data, but ideally kept internal export { applyNullInsertThreshold } from './transformers/nulls/nullInsertThreshold'; +export { nullToValue } from './transformers/nulls/nullToValue'; diff --git a/packages/grafana-ui/src/components/BigValue/BigValue.story.tsx b/packages/grafana-ui/src/components/BigValue/BigValue.story.tsx index 34bf52b4920..3527c9bfe76 100644 --- a/packages/grafana-ui/src/components/BigValue/BigValue.story.tsx +++ b/packages/grafana-ui/src/components/BigValue/BigValue.story.tsx @@ -57,6 +57,50 @@ interface StoryProps extends Partial { valueText: string; } +export const ApplyNoValue: Story = ({ + valueText, + title, + colorMode, + graphMode, + height, + width, + color, + textMode, + justifyMode, +}) => { + const theme = useTheme2(); + const sparkline: FieldSparkline = { + y: { + name: '', + values: [1, 2, 3, null, null], + type: FieldType.number, + state: { range: { min: 1, max: 4, delta: 3 } }, + config: { + noValue: '0', + }, + }, + }; + + return ( + + ); +}; + export const Basic: Story = ({ valueText, title, @@ -111,4 +155,16 @@ Basic.args = { textMode: BigValueTextMode.Auto, }; +ApplyNoValue.args = { + valueText: '$5022', + title: 'Total Earnings', + colorMode: BigValueColorMode.Value, + graphMode: BigValueGraphMode.Area, + justifyMode: BigValueJustifyMode.Auto, + width: 400, + height: 300, + color: 'red', + textMode: BigValueTextMode.Auto, +}; + export default meta; diff --git a/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx b/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx index 288aa4077b3..d56ecd4283b 100644 --- a/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx +++ b/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx @@ -10,6 +10,7 @@ import { FieldSparkline, FieldType, getFieldColorModeForField, + nullToValue, } from '@grafana/data'; import { AxisPlacement, @@ -62,7 +63,8 @@ export class Sparkline extends PureComponent { } static getDerivedStateFromProps(props: SparklineProps, state: State) { - const frame = preparePlotFrame(props.sparkline, props.config); + const _frame = preparePlotFrame(props.sparkline, props.config); + const frame = nullToValue(_frame); if (!frame) { return { ...state }; } @@ -96,6 +98,12 @@ export class Sparkline extends PureComponent { getYRange(field: Field): Range.MinMax { let { min, max } = this.state.alignedDataFrame.fields[1].state?.range!; + const noValue = +this.state.alignedDataFrame.fields[1].config?.noValue!; + + if (!Number.isNaN(noValue)) { + min = Math.min(min!, +noValue); + max = Math.max(max!, +noValue); + } if (min === max) { if (min === 0) {