Skip to content
Snippets Groups Projects
Unverified Commit 0f65cde6 authored by Ryan Laurie's avatar Ryan Laurie Committed by GitHub
Browse files

Round small y-axis values to zero with no min/max difference on the Y scale (#33179)

* Round small values to zero with no min/max difference on the Y scale

* dont show nulls as zero
parent 8dabbdff
No related merge requests found
...@@ -352,7 +352,10 @@ export function applyChartOrdinalXAxis( ...@@ -352,7 +352,10 @@ export function applyChartOrdinalXAxis(
// The tolerance is arbitrarily set to one millionth of the yExtent. // The tolerance is arbitrarily set to one millionth of the yExtent.
const TOLERANCE_TO_Y_EXTENT = 1e6; const TOLERANCE_TO_Y_EXTENT = 1e6;
export function maybeRoundValueToZero(value, [yMin, yMax]) { export function maybeRoundValueToZero(value, [yMin, yMax]) {
const tolerance = Math.abs(yMax - yMin) / TOLERANCE_TO_Y_EXTENT; const tolerance =
yMin !== yMax
? Math.abs(yMax - yMin) / TOLERANCE_TO_Y_EXTENT
: 1 / TOLERANCE_TO_Y_EXTENT;
return Math.abs(value) < tolerance ? 0 : value; return Math.abs(value) < tolerance ? 0 : value;
} }
...@@ -490,7 +493,8 @@ export function getYValueFormatter(chart, series, yExtent) { ...@@ -490,7 +493,8 @@ export function getYValueFormatter(chart, series, yExtent) {
const metricColumn = series[seriesIndex].data.cols[1]; const metricColumn = series[seriesIndex].data.cols[1];
const columnSettings = chart.settings.column(metricColumn); const columnSettings = chart.settings.column(metricColumn);
const columnExtent = options.extent ?? yExtent; const columnExtent = options.extent ?? yExtent;
const roundedValue = maybeRoundValueToZero(value, columnExtent); const roundedValue =
value === null ? "" : maybeRoundValueToZero(value, columnExtent);
return formatValue(roundedValue, { ...columnSettings, ...options }); return formatValue(roundedValue, { ...columnSettings, ...options });
}; };
} }
...@@ -61,5 +61,11 @@ describe("visualization.lib.apply_axis", () => { ...@@ -61,5 +61,11 @@ describe("visualization.lib.apply_axis", () => {
expect(value).toBe(0.0000000000018); expect(value).toBe(0.0000000000018);
}); });
it("should work on single-value charts (where minExtent === maxExtent)", () => {
const value = maybeRoundValueToZero(0.0000000000018, [0.00001, 0.00001]);
expect(value).toBe(0);
});
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment