Skip to content

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

Fix missing labels for bar chart (#68299)

Denis Berezin18c485d06c8f
diff --git a/.loki/reference/chrome_laptop_Components_Table_Default.png b/.loki/reference/chrome_laptop_Components_Table_Default.png
deleted file mode 100644
index 8f3ffc5e96b..00000000000
Binary files a/.loki/reference/chrome_laptop_Components_Table_Default.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_All_Overlays.png b/.loki/reference/chrome_laptop_Patterns_Overlays_All_Overlays.png
deleted file mode 100644
index a582c7a246c..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_All_Overlays.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_Legacy_Modal_Can_Launch_Mantine_Modal.png b/.loki/reference/chrome_laptop_Patterns_Overlays_Legacy_Modal_Can_Launch_Mantine_Modal.png
deleted file mode 100644
index 4dee2cbfc70..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_Legacy_Modal_Can_Launch_Mantine_Modal.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Legacy_Modal.png b/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Legacy_Modal.png
deleted file mode 100644
index 421de12809f..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Legacy_Modal.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Legacy_Select.png b/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Legacy_Select.png
deleted file mode 100644
index c54c266db75..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Legacy_Select.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Mantine_Hovercard.png b/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Mantine_Hovercard.png
deleted file mode 100644
index 263db4a80e1..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Mantine_Hovercard.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Mantine_Select.png b/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Mantine_Select.png
deleted file mode 100644
index 75856cd2c2e..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Modal_Can_Launch_Mantine_Select.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Popover_Can_Launch_Legacy_Modal.png b/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Popover_Can_Launch_Legacy_Modal.png
deleted file mode 100644
index 7a3ca9a92ae..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Popover_Can_Launch_Legacy_Modal.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Popover_Can_Launch_Legacy_Select.png b/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Popover_Can_Launch_Legacy_Select.png
deleted file mode 100644
index ac3981bdc5e..00000000000
Binary files a/.loki/reference/chrome_laptop_Patterns_Overlays_Mantine_Popover_Can_Launch_Legacy_Select.png and /dev/null differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels.png
index 8dd76455c18..f32c4ced957 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Ordinal_X_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Ordinal_X_Scale.png
index c76ccad07b7..dad69802480 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Ordinal_X_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Ordinal_X_Scale.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale.png
index 5ef781a2f10..3df539cce27 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale.png differ
diff --git a/.loki/reference/chrome_laptop_viz_BarChart_Default.png b/.loki/reference/chrome_laptop_viz_BarChart_Default.png
deleted file mode 100644
index 6e7a4c4d8bd..00000000000
Binary files a/.loki/reference/chrome_laptop_viz_BarChart_Default.png and /dev/null differ
diff --git a/e2e/test/scenarios/visualizations-charts/bar_chart.cy.spec.js b/e2e/test/scenarios/visualizations-charts/bar_chart.cy.spec.js
index 91bd7eed5df..201f0e89ea8 100644
--- a/e2e/test/scenarios/visualizations-charts/bar_chart.cy.spec.js
+++ b/e2e/test/scenarios/visualizations-charts/bar_chart.cy.spec.js
@@ -1089,4 +1089,68 @@ describe("scenarios > visualizations > bar chart", () => {
.should("be.visible");
});
});
+
+ it("should rotate axis labels when they do not fit horizontally instead of hiding them (metabase#68048)", () => {
+ // Use a smaller viewport to ensure labels need to rotate
+ cy.viewport(940, 800);
+
+ const query = `
+ SELECT * FROM (
+ VALUES
+ ('Alnyba', 390000),
+ ('Bvsieginlri', 500000),
+ ('Cflonta', 700000),
+ ('Dgamruh', 50000),
+ ('Eitstrugb', 130000),
+ ('Farnotcs', 107000),
+ ('Gkro', 750000)
+ ) AS Data(LABEL, amount)
+ `;
+
+ H.visitQuestionAdhoc({
+ display: "bar",
+ dataset_query: {
+ type: "native",
+ native: { query, "template-tags": {} },
+ database: SAMPLE_DB_ID,
+ },
+ visualization_settings: {
+ "graph.dimensions": ["LABEL"],
+ "graph.metrics": ["amount"],
+ },
+ });
+
+ cy.wait("@dataset");
+ H.echartsContainer().should("be.visible");
+
+ // Verify all 7 labels are visible and rotated (not hidden)
+ const expectedLabels = [
+ "Alnyba",
+ "Bvsieginlri",
+ "Cflonta",
+ "Dgamruh",
+ "Eitstrugb",
+ "Farnotcs",
+ "Gkro",
+ ];
+
+ H.echartsContainer().within(() => {
+ // When labels don't fit horizontally, ECharts rotates them
+ // Rotated labels have a transform attribute containing rotation
+ expectedLabels.forEach((label) => {
+ cy.contains("text", label).should("be.visible");
+ });
+
+ // Verify labels are rotated by checking for transform attribute with rotation
+ // ECharts applies rotation via transform attribute when labels don't fit
+ cy.get("text")
+ .filter((_, el) =>
+ expectedLabels.some((label) => el.textContent?.includes(label)),
+ )
+ .should("have.length", 7)
+ .first()
+ .should("have.attr", "transform")
+ .and("match", /matrix/);
+ });
+ });
});
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/chart-measurements/index.ts b/frontend/src/metabase/visualizations/echarts/cartesian/chart-measurements/index.ts
index 47e01fa69c3..1ea9a613625 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/chart-measurements/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/chart-measurements/index.ts
@@ -12,6 +12,7 @@ import type {
XAxisModel,
YAxisModel,
} from "metabase/visualizations/echarts/cartesian/model/types";
+import { getPaddedAxisLabel } from "metabase/visualizations/echarts/cartesian/option/utils";
import type {
ComputedVisualizationSettings,
Padding,
@@ -641,18 +642,20 @@ const areHorizontalXAxisTicksOverlapping = (
return;
}
const prevDatum = dataset[index - 1];
- const leftTickWidth = measureText(
- formatter(datum[X_AXIS_DATA_KEY]),
+ const rightTickWidth = measureText(
+ getPaddedAxisLabel(formatter(datum[X_AXIS_DATA_KEY])), // in some cases we render labels with extra spaces, so we have to be pessimistic with width measurement
fontStyle,
);
- const rightTickWidth = measureText(
- formatter(prevDatum[X_AXIS_DATA_KEY]),
+ const leftTickWidth = measureText(
+ getPaddedAxisLabel(formatter(prevDatum[X_AXIS_DATA_KEY])), // in some cases we render labels with extra spaces, so we have to be pessimistic with width measurement
fontStyle,
);
return (
leftTickWidth / 2 + rightTickWidth / 2 + HORIZONTAL_TICKS_GAP >
- dimensionWidth
+ dimensionWidth ||
+ rightTickWidth + HORIZONTAL_TICKS_GAP / 2 > dimensionWidth ||
+ leftTickWidth + HORIZONTAL_TICKS_GAP / 2 > dimensionWidth
);
});
};
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts
index 584cccd8539..55c2024bde2 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts
@@ -10,6 +10,7 @@ import type {
TimeSeriesXAxisModel,
YAxisModel,
} from "metabase/visualizations/echarts/cartesian/model/types";
+import { getPaddedAxisLabel } from "metabase/visualizations/echarts/cartesian/option/utils";
import type {
ComputedVisualizationSettings,
RenderingContext,
@@ -258,7 +259,7 @@ export const buildNumericDimensionAxis = (
if (isPadded && (rawValue < min || rawValue > max)) {
return "";
}
- return ` ${formatter(fromEChartsAxisValue(rawValue))} `;
+ return getPaddedAxisLabel(formatter(fromEChartsAxisValue(rawValue)));
},
},
...(isPadded
@@ -298,7 +299,9 @@ export const buildTimeSeriesDimensionAxis = (
formatter: (rawValue: number) => {
const value = xAxisModel.fromEChartsAxisValue(rawValue);
if (canRender(value)) {
- return ` ${formatter(value.format("YYYY-MM-DDTHH:mm:ss[Z]"))} `; // spaces force padding between ticks
+ return getPaddedAxisLabel(
+ formatter(value.format("YYYY-MM-DDTHH:mm:ss[Z]")),
+ );
}
return "";
},
@@ -347,10 +350,10 @@ export const buildCategoricalDimensionAxis = (
formatter: (value: string) => {
const numberValue = parseNumberValue(value);
if (isNumericBaseType(column) && numberValue !== null) {
- return ` ${formatter(numberValue)} `;
+ return getPaddedAxisLabel(formatter(numberValue));
}
- return ` ${formatter(value)} `; // spaces force padding between ticks
+ return getPaddedAxisLabel(formatter(value));
},
},
};
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/utils.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/utils.ts
index 9d88f26792e..c4fe0306312 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/utils.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/utils.ts
@@ -13,3 +13,6 @@ export function getSeriesYAxisIndex(
return leftAxisModel.seriesKeys.includes(dataKey) ? 0 : 1;
}
+
+export const getPaddedAxisLabel = (label: string | number): string =>
+ ` ${label} `; // spaces force padding between ticks