From 11b8cefe09c220ce4fecfff17fc2e29860d9e830 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 7 Jul 2022 10:07:47 +0100 Subject: [PATCH] [v8.5.x] Bar chart: Fix stacking bug when data produces 0 accumulators (#51809) * Bar chart: Fix stacking bug when data produces 0 accumulators (#51450) * Don't divide by 0 ... * Add test panel to gdev dashboard (cherry picked from commit daf0e3cb4ebfc78221aff906d86ea9040dc2db5a) * betterer Co-authored-by: Dominik Prokop --- .../panel-graph/graph-ng-stacking2.json | 84 +++++++++++++++++++ .../grafana-ui/src/components/uPlot/utils.ts | 2 +- public/app/plugins/panel/barchart/bars.ts | 13 ++- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json b/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json index 06ecfcbadb3..b213b054e90 100644 --- a/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json +++ b/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json @@ -3409,6 +3409,90 @@ ], "title": "'undefined' join artifacts (lines)", "type": "timeseries" + }, + { + "datasource": { + "type": "testdata", + "uid": "PD8C576611E62080A" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "axisLabel": "", + "axisPlacement": "auto", + "axisSoftMin": 0, + "fillOpacity": 80, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineWidth": 1, + "scaleDistribution": { + "type": "linear" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 9, + "w": 12, + "x": 0, + "y": 65 + }, + "id": 39, + "options": { + "barRadius": 0, + "barWidth": 0.97, + "groupWidth": 0.7, + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom" + }, + "orientation": "auto", + "showValue": "auto", + "stacking": "percent", + "tooltip": { + "mode": "single", + "sort": "none" + }, + "xTickLabelRotation": 0, + "xTickLabelSpacing": 0 + }, + "pluginVersion": "9.1.0-pre", + "targets": [ + { + "csvContent": "name, number, number2, number3, number4\nName1, 40, 5, 20, 10\nName2, 0,0,0,0\nName3, 6, 3, 5, 1\nName4, 1, 1, 1, 2", + "datasource": { + "type": "testdata", + "uid": "PD8C576611E62080A" + }, + "refId": "A", + "scenarioId": "csv_content" + } + ], + "title": "Bar chart stack with 0 only series", + "type": "barchart" } ], "refresh": false, diff --git a/packages/grafana-ui/src/components/uPlot/utils.ts b/packages/grafana-ui/src/components/uPlot/utils.ts index 5e0254c7fee..17cdd023cc2 100755 --- a/packages/grafana-ui/src/components/uPlot/utils.ts +++ b/packages/grafana-ui/src/components/uPlot/utils.ts @@ -283,7 +283,7 @@ export function preparePlotData2( if (v != null) { // v / accum will always be pos, so properly (re)sign by group stacking dir - stacked[i] = group.dir * (v / accum[i]); + stacked[i] = accum[i] === 0 ? 0 : group.dir * (v / accum[i]); } } } diff --git a/public/app/plugins/panel/barchart/bars.ts b/public/app/plugins/panel/barchart/bars.ts index 841b20cc411..c3e3b548b64 100644 --- a/public/app/plugins/panel/barchart/bars.ts +++ b/public/app/plugins/panel/barchart/bars.ts @@ -307,11 +307,16 @@ export function getConfig(opts: BarsOptions, theme: GrafanaTheme2) { qt.add(barRect); if (showValue !== VisibilityMode.Never) { + const raw = rawValue(seriesIdx, dataIdx)!; + let divider = 1; + + if (pctStacked && alignedTotals![seriesIdx][dataIdx]!) { + divider = alignedTotals![seriesIdx][dataIdx]!; + } + + const v = divider === 0 ? 0 : raw / divider; // Format Values and calculate label offsets - const text = formatValue( - seriesIdx, - rawValue(seriesIdx, dataIdx)! / (pctStacked ? alignedTotals![seriesIdx][dataIdx]! : 1) - ); + const text = formatValue(seriesIdx, v); labelOffset = Math.min(labelOffset, Math.round(LABEL_OFFSET_FACTOR * (isXHorizontal ? wid : hgt))); if (labels[dataIdx] === undefined) {