[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 daf0e3cb4e)

* betterer

Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2022-07-07 02:07:47 -07:00
committed by GitHub
co-authored by Dominik Prokop
parent 14e622414b
commit 11b8cefe09
3 changed files with 94 additions and 5 deletions
@@ -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,
@@ -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]);
}
}
}
+9 -4
View File
@@ -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) {