Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix y-axis currency symbol clipping in compact view (#76138)

Anderson05e9b21565db
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Log_Y_Scale_Custom_Y_Axis_Range.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Log_Y_Scale_Custom_Y_Axis_Range.png
index 802196d9d27..b797aec5bbe 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Log_Y_Scale_Custom_Y_Axis_Range.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Log_Y_Scale_Custom_Y_Axis_Range.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Goal_Lines_With_Scaling_Pow_Scale_Custom_Range.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Goal_Lines_With_Scaling_Pow_Scale_Custom_Range.png
index a3252372834..d2731cbdfea 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Goal_Lines_With_Scaling_Pow_Scale_Custom_Range.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Goal_Lines_With_Scaling_Pow_Scale_Custom_Range.png differ
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts
index cc1b0a56472..ac732c2886e 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts
@@ -1,6 +1,5 @@
import _ from "underscore";
-import { isNotNull } from "metabase/utils/types";
import { X_AXIS_DATA_KEY } from "metabase/visualizations/echarts/cartesian/constants/dataset";
import {
CHART_STYLE,
@@ -93,6 +92,32 @@ const getValuesToMeasure = (min: number, max: number): number[] => {
return [...middleValues, min, max];
};
+const getYAxisExtentToMeasure = (
+ axisModel: YAxisModel,
+ settings: ComputedVisualizationSettings,
+ yAxisScaleTransforms: NumericAxisScaleTransforms,
+): [number, number] => {
+ const [min, max] = axisModel.extent.map((extent) =>
+ yAxisScaleTransforms.fromEChartsAxisValue(extent),
+ );
+
+ if (!settings["graph.y_axis.auto_range"]) {
+ return [
+ settings["graph.y_axis.min"] ?? min,
+ settings["graph.y_axis.max"] ?? max,
+ ];
+ }
+
+ if (
+ settings["graph.y_axis.unpin_from_zero"] ||
+ settings["graph.y_axis.scale"] === "log"
+ ) {
+ return [min, max];
+ }
+
+ return [Math.min(min, 0), Math.max(max, 0)];
+};
+
/**
* For log scales, ECharts places ticks at regular intervals in transformed space.
* These intervals can be at integer positions (giving 1, 10, 100...) or half-integer
@@ -133,9 +158,10 @@ const getYAxisTicksWidth = (
size: theme.cartesian.label.fontSize,
};
- // extents need to be untransformed to get the value of the tick label
- const [min, max] = axisModel.extent.map((extent) =>
- yAxisScaleTransforms.fromEChartsAxisValue(extent),
+ const [min, max] = getYAxisExtentToMeasure(
+ axisModel,
+ settings,
+ yAxisScaleTransforms,
);
const isFormattingAutoOrCompact =
@@ -145,23 +171,18 @@ const getYAxisTicksWidth = (
: [min, max];
if (settings["graph.y_axis.scale"] === "log") {
- const [transformedMin, transformedMax] = axisModel.extent;
- valuesToMeasure.push(
- ...getLogScaleTickValues(
- transformedMin,
- transformedMax,
- yAxisScaleTransforms.fromEChartsAxisValue,
- ),
- );
- }
-
- if (!settings["graph.y_axis.auto_range"]) {
- const customRangeValues = [
- settings["graph.y_axis.min"],
- settings["graph.y_axis.max"],
- ].filter(isNotNull);
-
- valuesToMeasure.push(...customRangeValues);
+ const transformedMin = yAxisScaleTransforms.toEChartsAxisValue(min);
+ const transformedMax = yAxisScaleTransforms.toEChartsAxisValue(max);
+
+ if (transformedMin != null && transformedMax != null) {
+ valuesToMeasure.push(
+ ...getLogScaleTickValues(
+ transformedMin,
+ transformedMax,
+ yAxisScaleTransforms.fromEChartsAxisValue,
+ ),
+ );
+ }
}
if (settings["graph.show_goal"] && settings["graph.goal_value"] != null) {
@@ -180,10 +201,10 @@ const getYAxisTicksWidth = (
(value) => value > -5 && value < 5,
);
- const measuredValues = valuesToMeasure.map((rawValue) => {
- const isPercent =
- settings.column?.(axisModel.column).number_style === "percent";
+ const columnSettings = settings.column?.(axisModel.column);
+ const isPercent = columnSettings?.number_style === "percent";
+ const measuredValues = valuesToMeasure.map((rawValue) => {
let value = rawValue;
if (!isPercent && !areDecimalTicksExpected) {
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.unit.spec.ts b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.unit.spec.ts
new file mode 100644
index 00000000000..66136fa0777
--- /dev/null
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.unit.spec.ts
@@ -0,0 +1,109 @@
+import { DEFAULT_VISUALIZATION_THEME } from "metabase/visualizations/shared/utils/theme";
+import type { RenderingContext } from "metabase/visualizations/types";
+import {
+ createMockColumn,
+ createMockVisualizationSettings,
+} from "metabase-types/api/mocks";
+
+import { CHART_STYLE } from "../constants/style";
+import type { XAxisModel, YAxisModel } from "../model/types";
+
+import { type ChartLayoutInput, getChartLayout } from ".";
+
+const WIDEST_MEASURED_TICK_WIDTH = 64;
+
+const formatCurrency = (value: unknown) => {
+ const numberValue = Number(value);
+
+ if (Math.abs(numberValue) >= 1000) {
+ return `$${(numberValue / 1000).toFixed(2)}k`;
+ }
+
+ return `$${numberValue.toFixed(2)}`;
+};
+
+const xAxisModel: XAxisModel = {
+ axisType: "category",
+ isHistogram: false,
+ valuesCount: 3,
+ formatter: (value) => String(value),
+};
+
+const yAxisModel: YAxisModel = {
+ seriesKeys: ["price"],
+ extent: [1200, 1800],
+ column: createMockColumn({ name: "price" }),
+ formatter: formatCurrency,
+ formatGoal: formatCurrency,
+ splitNumber: 5,
+};
+
+const input: ChartLayoutInput = {
+ xAxisModel,
+ leftAxisModel: yAxisModel,
+ rightAxisModel: null,
+ yAxisScaleTransforms: {
+ toEChartsAxisValue: (value) => {
+ return typeof value === "number" ? value : null;
+ },
+ fromEChartsAxisValue: (value) => value,
+ },
+};
+
+const settings = createMockVisualizationSettings({
+ "graph.label_value_formatting": "compact",
+ "graph.x_axis.axis_enabled": false,
+ "graph.y_axis.axis_enabled": true,
+ "graph.y_axis.auto_range": true,
+});
+
+const currencySettings = createMockVisualizationSettings({
+ ...settings,
+ column: () => ({ number_style: "currency" }),
+});
+
+const getChartContext = (): RenderingContext => {
+ const measureText = jest.fn((text: string) => {
+ if (text === "$720.00") {
+ return WIDEST_MEASURED_TICK_WIDTH;
+ }
+
+ return 20;
+ });
+
+ return {
+ getColor: (name) => name,
+ measureText,
+ measureTextHeight: () => 0,
+ fontFamily: "",
+ theme: DEFAULT_VISUALIZATION_THEME,
+ };
+};
+
+describe("getChartLayout", () => {
+ it("measures actual y-axis tick labels for a zero-pinned axis (#74568)", () => {
+ const chartContext = getChartContext();
+
+ const chartLayout = getChartLayout(
+ input,
+ currencySettings,
+ false,
+ 480,
+ 274,
+ chartContext,
+ );
+
+ expect(chartContext.measureText).toHaveBeenCalledWith(
+ "$720.00",
+ expect.anything(),
+ );
+ expect(chartLayout.ticksDimensions.yTicksWidthLeft).toBe(
+ WIDEST_MEASURED_TICK_WIDTH + CHART_STYLE.axisTicksMarginY,
+ );
+ expect(chartLayout.ticksDimensions.yTicksWidthLeft).not.toBe(
+ WIDEST_MEASURED_TICK_WIDTH +
+ CHART_STYLE.axisTicksMarginY +
+ CHART_STYLE.padding.x,
+ );
+ });
+});