Stat: Fix double-zero percent render (#91848)

fix(stat): percent zero case
This commit is contained in:
Ihor Yeromin 2024-08-20 03:25:15 +02:00 committed by GitHub
parent a0e1028ae6
commit 5233e4b47f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 120 additions and 27 deletions

View File

@ -18,7 +18,6 @@
"editable": true, "editable": true,
"fiscalYearStartMonth": 0, "fiscalYearStartMonth": 0,
"graphTooltip": 0, "graphTooltip": 0,
"id": 508,
"links": [], "links": [],
"liveNow": false, "liveNow": false,
"panels": [ "panels": [
@ -88,6 +87,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -102,7 +102,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__house_locations", "alias": "__house_locations",
@ -174,6 +174,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -188,7 +189,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__house_locations", "alias": "__house_locations",
@ -260,6 +261,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -274,7 +276,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__house_locations", "alias": "__house_locations",
@ -346,6 +348,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "horizontal", "orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -360,7 +363,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -431,6 +434,7 @@
"graphMode": "line", "graphMode": "line",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -445,7 +449,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"datasource": { "datasource": {
@ -560,6 +564,7 @@
"graphMode": "line", "graphMode": "line",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "horizontal", "orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -574,7 +579,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"datasource": { "datasource": {
@ -691,6 +696,7 @@
"graphMode": "none", "graphMode": "none",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "horizontal", "orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -705,7 +711,7 @@
"textMode": "name", "textMode": "name",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -781,6 +787,7 @@
"graphMode": "none", "graphMode": "none",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -795,7 +802,7 @@
"textMode": "value", "textMode": "value",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -871,6 +878,7 @@
"graphMode": "none", "graphMode": "none",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -885,7 +893,7 @@
"textMode": "none", "textMode": "none",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -972,6 +980,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -986,7 +995,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__house_locations", "alias": "__house_locations",
@ -1058,6 +1067,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1072,7 +1082,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__house_locations", "alias": "__house_locations",
@ -1144,6 +1154,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1158,7 +1169,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__house_locations", "alias": "__house_locations",
@ -1230,6 +1241,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "horizontal", "orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1244,7 +1256,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -1315,6 +1327,7 @@
"graphMode": "line", "graphMode": "line",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1329,7 +1342,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"datasource": { "datasource": {
@ -1444,6 +1457,7 @@
"graphMode": "line", "graphMode": "line",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "horizontal", "orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1458,7 +1472,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"datasource": { "datasource": {
@ -1575,6 +1589,7 @@
"graphMode": "none", "graphMode": "none",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "horizontal", "orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1589,7 +1604,7 @@
"textMode": "name", "textMode": "name",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -1665,6 +1680,7 @@
"graphMode": "none", "graphMode": "none",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1679,7 +1695,7 @@
"textMode": "value", "textMode": "value",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -1755,6 +1771,7 @@
"graphMode": "none", "graphMode": "none",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"mean" "mean"
@ -1769,7 +1786,7 @@
"textMode": "none", "textMode": "none",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"alias": "__server_names", "alias": "__server_names",
@ -1819,7 +1836,7 @@
}, },
"gridPos": { "gridPos": {
"h": 8, "h": 8,
"w": 8, "w": 4,
"x": 0, "x": 0,
"y": 78 "y": 78
}, },
@ -1829,6 +1846,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"lastNotNull" "lastNotNull"
@ -1840,7 +1858,7 @@
"textMode": "value", "textMode": "value",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"csvContent": "time, value\n2023-12-13T00:00:00Z, 0\n2023-12-13T00:00:00Z, 100", "csvContent": "time, value\n2023-12-13T00:00:00Z, 0\n2023-12-13T00:00:00Z, 100",
@ -1855,6 +1873,73 @@
"title": "Infinity Percent Change", "title": "Infinity Percent Change",
"type": "stat" "type": "stat"
}, },
{
"datasource": {
"default": true,
"type": "grafana-testdata-datasource",
"uid": "PD8C576611E62080A"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 4,
"x": 4,
"y": 78
},
"id": 32,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"showPercentChange": true,
"textMode": "value",
"wideLayout": true
},
"pluginVersion": "11.2.0-pre",
"targets": [
{
"csvContent": "time, value\n2023-12-13T00:00:00Z, 50\n2023-12-13T00:00:00Z, 100\n2023-12-13T00:00:00Z, 50",
"datasource": {
"type": "grafana-testdata-datasource",
"uid": "PD8C576611E62080A"
},
"refId": "A",
"scenarioId": "csv_content"
}
],
"title": "Zero Percent Change",
"type": "stat"
},
{ {
"datasource": { "datasource": {
"type": "grafana-testdata-datasource", "type": "grafana-testdata-datasource",
@ -1894,6 +1979,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"lastNotNull" "lastNotNull"
@ -1905,7 +1991,7 @@
"textMode": "value", "textMode": "value",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"csvContent": "time, value\n2023-12-13T00:00:00Z, 0\n2023-12-13T00:00:00Z, 0", "csvContent": "time, value\n2023-12-13T00:00:00Z, 0\n2023-12-13T00:00:00Z, 0",
@ -1959,6 +2045,7 @@
"graphMode": "area", "graphMode": "area",
"justifyMode": "auto", "justifyMode": "auto",
"orientation": "auto", "orientation": "auto",
"percentChangeColorMode": "standard",
"reduceOptions": { "reduceOptions": {
"calcs": [ "calcs": [
"lastNotNull" "lastNotNull"
@ -1970,7 +2057,7 @@
"textMode": "auto", "textMode": "auto",
"wideLayout": true "wideLayout": true
}, },
"pluginVersion": "10.3.0-pre", "pluginVersion": "11.2.0-pre",
"targets": [ "targets": [
{ {
"csvContent": "Name, value\nName1, 10\nName2, 20", "csvContent": "Name, value\nName1, 10\nName2, 20",
@ -2017,6 +2104,6 @@
"timezone": "", "timezone": "",
"title": "Panel Tests - Stat", "title": "Panel Tests - Stat",
"uid": "EJ8_d9jZk", "uid": "EJ8_d9jZk",
"version": 11, "version": 15,
"weekStart": "" "weekStart": ""
} }

View File

@ -1,3 +1,5 @@
import { IconName } from '@grafana/data';
import { Icon } from '../Icon/Icon'; import { Icon } from '../Icon/Icon';
import { PercentChangeStyles } from './BigValueLayout'; import { PercentChangeStyles } from './BigValueLayout';
@ -8,8 +10,12 @@ export interface Props {
} }
export const PercentChange = ({ percentChange, styles }: Props) => { export const PercentChange = ({ percentChange, styles }: Props) => {
const percentChangeIcon = let percentChangeIcon: IconName | undefined = undefined;
percentChange && (percentChange > 0 ? 'arrow-up' : percentChange < 0 ? 'arrow-down' : undefined); if (percentChange > 0) {
percentChangeIcon = 'arrow-up';
} else if (percentChange < 0) {
percentChangeIcon = 'arrow-down';
}
return ( return (
<div style={styles.containerStyles}> <div style={styles.containerStyles}>
@ -22,5 +28,5 @@ export const PercentChange = ({ percentChange, styles }: Props) => {
}; };
export const percentChangeString = (percentChange: number) => { export const percentChangeString = (percentChange: number) => {
return percentChange?.toLocaleString(undefined, { style: 'percent', maximumSignificantDigits: 3 }) ?? ''; return percentChange.toLocaleString(undefined, { style: 'percent', maximumSignificantDigits: 3 });
}; };