From d122449a978192533068ac8b6105b5cd968a6bcc Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Wed, 6 Jul 2022 07:47:49 -0500 Subject: [PATCH] BarChart: fix explicit yAxis min (#51768) --- .../app/plugins/panel/barchart/models.gen.ts | 1 - public/app/plugins/panel/barchart/utils.ts | 20 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/public/app/plugins/panel/barchart/models.gen.ts b/public/app/plugins/panel/barchart/models.gen.ts index 8068fa61aa3..1653e84f877 100644 --- a/public/app/plugins/panel/barchart/models.gen.ts +++ b/public/app/plugins/panel/barchart/models.gen.ts @@ -45,5 +45,4 @@ export const defaultBarChartFieldConfig: BarChartFieldConfig = { lineWidth: 1, fillOpacity: 80, gradientMode: GraphGradientMode.None, - axisSoftMin: 0, }; diff --git a/public/app/plugins/panel/barchart/utils.ts b/public/app/plugins/panel/barchart/utils.ts index 1a45b42a2bb..9d24655bf16 100644 --- a/public/app/plugins/panel/barchart/utils.ts +++ b/public/app/plugins/panel/barchart/utils.ts @@ -173,6 +173,18 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn = ({ const scaleColor = getFieldSeriesColor(field, theme); const seriesColor = scaleColor.color; + // make barcharts start at 0 unless explicitly overridden + let softMin = customConfig.axisSoftMin; + let softMax = customConfig.axisSoftMax; + + if (softMin == null && field.config.min == null) { + softMin = 0; + } + + if (softMax == null && field.config.max == null) { + softMax = 0; + } + builder.addSeries({ scaleKey, pxAlign: true, @@ -187,8 +199,8 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn = ({ thresholds: field.config.thresholds, hardMin: field.config.min, hardMax: field.config.max, - softMin: customConfig.axisSoftMin, - softMax: customConfig.axisSoftMax, + softMin, + softMax, // The following properties are not used in the uPlot config, but are utilized as transport for legend config // PlotLegend currently gets unfiltered DataFrame[], so index must be into that field array, not the prepped frame's which we're iterating here @@ -207,8 +219,8 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn = ({ scaleKey, min: field.config.min, max: field.config.max, - softMin: customConfig.axisSoftMin, - softMax: customConfig.axisSoftMax, + softMin, + softMax, orientation: vizOrientation.yOri, direction: vizOrientation.yDir, distribution: customConfig.scaleDistribution?.type,