Skip to content
Snippets Groups Projects
Unverified Commit e9eeab1a authored by adam-james's avatar adam-james Committed by GitHub
Browse files

Static Viz Line/Area/Bar Should now respect set Y-axis min/max (#28240)

* Static Viz Line/Area/Bar Should now respect set Y-axis min/max

The values for y-axis min and max are available to the static-viz js, it is just a matter of pulling them into the y
domain calculations.

This PR also adds a rendering test to the backend unit tests to confirm that the setting is respected through the
render pipeline.

* Make tests better by not re-rendering for every node lookup.

Just render each hiccup tree once, and search that tree multiple times instead. Just straight up a better way to do this test!

* Make variable names clearer so they don't get mistaken for fns
parent 0d4b3077
Branches
Tags
No related merge requests found
......@@ -62,7 +62,14 @@ export const XYChart = ({
series = calculateStackedItems(series);
}
const yDomains = calculateYDomains(series, settings.goal?.value);
const minValueSetting = settings.visualization_settings["graph.y_axis.min"];
const maxValueSetting = settings.visualization_settings["graph.y_axis.max"];
const yDomains = calculateYDomains(
series,
minValueSetting,
maxValueSetting,
settings.goal?.value,
);
const yTickWidths = getYTickWidths(
settings.y.format,
style.axes.ticks.fontSize,
......
......@@ -90,6 +90,8 @@ export const createXScale = (
const calculateYDomain = (
series: HydratedSeries[],
minValueSetting?: number,
maxValueSetting?: number,
goalValue?: number,
): ContinuousDomain => {
const values = series
......@@ -101,13 +103,15 @@ const calculateYDomain = (
const maxValue = max(values);
return [
Math.min(0, minValue, goalValue ?? 0),
Math.max(0, maxValue, goalValue ?? 0),
Math.min(0, minValue, minValueSetting ?? 0, goalValue ?? 0),
Math.max(0, maxValue, maxValueSetting ?? 0, goalValue ?? 0),
];
};
export const calculateYDomains = (
series: HydratedSeries[],
minValueSetting?: number,
maxValueSetting?: number,
goalValue?: number,
) => {
const leftScaleSeries = series.filter(
......@@ -118,15 +122,34 @@ export const calculateYDomains = (
);
if (leftScaleSeries.length > 0 && rightScaleSeries.length === 0) {
return { left: calculateYDomain(leftScaleSeries, goalValue) };
return {
left: calculateYDomain(
leftScaleSeries,
minValueSetting,
maxValueSetting,
goalValue,
),
};
}
if (rightScaleSeries.length > 0 && leftScaleSeries.length === 0) {
return { right: calculateYDomain(rightScaleSeries, goalValue) };
return {
right: calculateYDomain(
rightScaleSeries,
minValueSetting,
maxValueSetting,
goalValue,
),
};
}
return {
left: calculateYDomain(leftScaleSeries, goalValue),
right: calculateYDomain(rightScaleSeries),
left: calculateYDomain(
leftScaleSeries,
minValueSetting,
maxValueSetting,
goalValue,
),
right: calculateYDomain(rightScaleSeries, minValueSetting, maxValueSetting),
};
};
......
......@@ -674,3 +674,34 @@
(is (= {:bottom "X custom", :left "Y custom"}
(#'body/x-and-y-axis-label-info x-col y-col {:graph.x_axis.title_text "X custom"
:graph.y_axis.title_text "Y custom"}))))))
(deftest lab-charts-respect-y-axis-range
(let [rows [["Category" "Series A" "Series B"]
["A" 1 1.3]
["B" 2 1.9]
["C" -3 6]]
renderfn (fn [viz]
(-> rows
(render.tu/make-card-and-data :bar)
(render.tu/merge-viz-settings viz)
render.tu/render-as-hiccup))]
(testing "Graph min and max values are respected in the render. #27927"
(let [to-find ["14" "2" "-2" "-14"]
no-viz-render (renderfn {})
viz-a-render (renderfn {:graph.y_axis.max 14
:graph.y_axis.min -14})
nodes-without-viz (mapv #(last (last (render.tu/nodes-with-text no-viz-render %))) to-find)
nodes-with-viz (mapv #(last (last (render.tu/nodes-with-text viz-a-render %))) to-find)]
;; we only see 14/-14 in the render where min and max are explicitly set.
;; this is because the data's min and max values are only -3 and 6, and the viz will minimize the axis range
;; without cutting off the chart's actual values
(is (= {:without-viz ["2" "-2"]
:with-viz ["14" "2" "-2" "-14"]}
{:without-viz (remove nil? nodes-without-viz)
:with-viz nodes-with-viz}))))
(testing "Graph min and max values do not cut off the chart."
(let [viz-b-render (renderfn {:graph.y_axis.max 1
:graph.y_axis.min -1})
to-find ["14" "2" "-2" "-14"]
nodes-with-viz (mapv #(last (last (render.tu/nodes-with-text viz-b-render %))) to-find)]
(is (= ["2" "-2"] (remove nil? nodes-with-viz)))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment