diff --git a/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json b/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json index 60d9a661b9e..f7e699a68b7 100644 --- a/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json +++ b/devenv/dev-dashboards/panel-graph/graph-ng-stacking2.json @@ -3382,6 +3382,89 @@ ], "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" + }, + { + "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, @@ -3405,4 +3488,4 @@ "uid": "1KxMUdE7k", "version": 5, "weekStart": "" -} +} \ No newline at end of file diff --git a/packages/grafana-ui/src/components/uPlot/utils.ts b/packages/grafana-ui/src/components/uPlot/utils.ts index 5e0254c7fee..17cdd023cc2 100644 --- 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 65e17fbe0eb..032ae4df1b2 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) {