The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix axis labels being cut off when rotated 45 degrees (#75815)
Kevin Kelleherf9aa7966d3a6
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_45_Degree_Labels.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_45_Degree_Labels.pngnew file mode 100644index 00000000000..1b0b1dd3510Binary files /dev/null and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_45_Degree_Labels.png differdiff --git a/frontend/src/metabase/static-viz/components/ComboChart/stories-data/bar-45-degree-labels.json b/frontend/src/metabase/static-viz/components/ComboChart/stories-data/bar-45-degree-labels.jsonnew file mode 100644index 00000000000..3deab2413d6--- /dev/null+++ b/frontend/src/metabase/static-viz/components/ComboChart/stories-data/bar-45-degree-labels.json@@ -0,0 +1,402 @@+[+ {+ "card": {+ "cache_invalidated_at": null,+ "description": null,+ "archived": false,+ "view_count": 0,+ "collection_position": null,+ "source_card_id": null,+ "table_id": null,+ "can_run_adhoc_query": true,+ "result_metadata": [+ {+ "database_type": "CHARACTER VARYING",+ "semantic_type": null,+ "lib/deduplicated-name": "LABEL",+ "lib/original-name": "LABEL",+ "name": "LABEL",+ "lib/source": "source/native",+ "lib/source-column-alias": "LABEL",+ "source": "native",+ "field_ref": [+ "field",+ "LABEL",+ {+ "base-type": "type/Text"+ }+ ],+ "effective_type": "type/Text",+ "lib/desired-column-alias": "LABEL",+ "display_name": "LABEL",+ "fingerprint": {+ "global": {+ "distinct-count": 15,+ "nil%": 0+ },+ "type": {+ "type/Text": {+ "percent-json": 0,+ "percent-url": 0,+ "percent-email": 0,+ "percent-state": 0,+ "percent-blank": 0,+ "average-length": 25.533333333333335,+ "mode-fraction": 0.06666666666666667,+ "top-3-fraction": 0.2+ }+ }+ },+ "base_type": "type/Text"+ },+ {+ "database_type": "BIGINT",+ "semantic_type": null,+ "lib/deduplicated-name": "value",+ "lib/original-name": "value",+ "name": "value",+ "lib/source": "source/native",+ "lib/source-column-alias": "value",+ "source": "native",+ "field_ref": [+ "field",+ "value",+ {+ "base-type": "type/BigInteger"+ }+ ],+ "effective_type": "type/BigInteger",+ "lib/desired-column-alias": "value",+ "display_name": "value",+ "fingerprint": {+ "global": {+ "distinct-count": 15,+ "nil%": 0+ },+ "type": {+ "type/Number": {+ "skewness": 0,+ "min": 1,+ "q1": 4,+ "q3": 12,+ "excess-kurtosis": -1.1999999999999997,+ "zero-fraction": 0,+ "mode-fraction": 0.06666666666666667,+ "top-3-fraction": 0.2,+ "max": 15,+ "sd": 4.47213595499958,+ "avg": 8+ }+ }+ },+ "base_type": "type/BigInteger"+ }+ ],+ "embedding_type": null,+ "dependency_analysis_version": 0,+ "creator": {+ "email": "[email protected]",+ "first_name": "Kevin",+ "is_data_analyst": false,+ "last_login": "2026-06-12T12:04:40.797893Z",+ "is_qbnewb": false,+ "is_superuser": true,+ "id": 1,+ "last_name": "Kelleher",+ "tenant_id": null,+ "date_joined": "2026-01-07T22:26:30.692014Z",+ "common_name": "Kevin Kelleher"+ },+ "initially_published_at": null,+ "can_write": true,+ "card_schema": 23,+ "database_id": 1,+ "enable_embedding": false,+ "collection_id": 6,+ "query_type": "native",+ "name": "UXW 4474 long label bug for Loki",+ "last_query_start": null,+ "is_remote_synced": false,+ "dashboard_count": 0,+ "document_id": null,+ "last_used_at": "2026-06-12T20:40:10.317029Z",+ "dashboard": null,+ "type": "question",+ "dimensions": null,+ "average_query_time": null,+ "creator_id": 1,+ "can_restore": false,+ "moderation_reviews": [],+ "updated_at": "2026-06-12T20:40:10.317029Z",+ "made_public_by_id": null,+ "embedding_params": null,+ "cache_ttl": null,+ "dataset_query": {+ "lib/type": "mbql/query",+ "database": 1,+ "stages": [+ {+ "lib/type": "mbql.stage/native",+ "native": "SELECT\n CASE WHEN X <= 1 THEN 'Short label ' || x ELSE 'Really loooooooong label ' || x END AS label,\n x AS \"value\"\nFROM SYSTEM_RANGE(1, 15)"+ }+ ]+ },+ "dimension_mappings": null,+ "id": 930,+ "legacy_query": null,+ "parameter_mappings": [],+ "can_manage_db": true,+ "display": "bar",+ "archived_directly": false,+ "entity_id": "Sv76MfJdZHgka8Jo1EsWp",+ "param_fields": {},+ "collection_preview": true,+ "last-edit-info": {+ "id": 1,+ "email": "[email protected]",+ "first_name": "Kevin",+ "last_name": "Kelleher",+ "timestamp": "2026-06-12T20:40:10.367112Z"+ },+ "visualization_settings": {+ "graph.x_axis.scale": "ordinal",+ "graph.dimensions": [+ "LABEL"+ ],+ "graph.metrics": [+ "value"+ ]+ },+ "collection": {+ "authority_level": null,+ "description": null,+ "archived": false,+ "slug": "kevin_kelleher_s_personal_collection",+ "archive_operation_id": null,+ "name": "Kevin Kelleher's Personal Collection",+ "is_remote_synced": false,+ "personal_owner_id": 1,+ "type": null,+ "is_sample": false,+ "id": 6,+ "archived_directly": null,+ "entity_id": "JNVXeRiI0n1JvPH29Siez",+ "location": "/",+ "namespace": null,+ "is_personal": true,+ "created_at": "2026-01-07T22:27:02.302189Z"+ },+ "metabase_version": "vLOCAL_DEV (06d1ba2ae111e66253209c01c244d6379acfc6dcb1911fa9ab6012cec9ce52e5)",+ "parameters": [],+ "dashboard_id": null,+ "created_at": "2026-06-12T20:40:10.317029Z",+ "parameter_usage_count": 0,+ "public_uuid": null,+ "can_delete": false+ },+ "data": {+ "rows": [+ [+ "Short label 1",+ 1+ ],+ [+ "Really loooooooong label 2",+ 2+ ],+ [+ "Really loooooooong label 3",+ 3+ ],+ [+ "Really loooooooong label 4",+ 4+ ],+ [+ "Really loooooooong label 5",+ 5+ ],+ [+ "Really loooooooong label 6",+ 6+ ],+ [+ "Really loooooooong label 7",+ 7+ ],+ [+ "Really loooooooong label 8",+ 8+ ],+ [+ "Really loooooooong label 9",+ 9+ ],+ [+ "Really loooooooong label 10",+ 10+ ],+ [+ "Really loooooooong label 11",+ 11+ ],+ [+ "Really loooooooong label 12",+ 12+ ],+ [+ "Really loooooooong label 13",+ 13+ ],+ [+ "Really loooooooong label 14",+ 14+ ],+ [+ "Really loooooooong label 15",+ 15+ ]+ ],+ "results_timezone": "UTC",+ "download_perms": "full",+ "results_metadata": {+ "columns": [+ {+ "database_type": "CHARACTER VARYING",+ "semantic_type": null,+ "lib/deduplicated-name": "LABEL",+ "lib/original-name": "LABEL",+ "name": "LABEL",+ "lib/source": "source/native",+ "lib/source-column-alias": "LABEL",+ "source": "native",+ "field_ref": [+ "field",+ "LABEL",+ {+ "base-type": "type/Text"+ }+ ],+ "effective_type": "type/Text",+ "lib/desired-column-alias": "LABEL",+ "display_name": "LABEL",+ "fingerprint": {+ "global": {+ "distinct-count": 15,+ "nil%": 0+ },+ "type": {+ "type/Text": {+ "percent-json": 0,+ "percent-url": 0,+ "percent-email": 0,+ "percent-state": 0,+ "percent-blank": 0,+ "average-length": 25.533333333333335,+ "mode-fraction": 0.06666666666666667,+ "top-3-fraction": 0.2+ }+ }+ },+ "base_type": "type/Text"+ },+ {+ "database_type": "BIGINT",+ "semantic_type": null,+ "lib/deduplicated-name": "value",+ "lib/original-name": "value",+ "name": "value",+ "lib/source": "source/native",+ "lib/source-column-alias": "value",+ "source": "native",+ "field_ref": [+ "field",+ "value",+ {+ "base-type": "type/BigInteger"+ }+ ],+ "effective_type": "type/BigInteger",+ "lib/desired-column-alias": "value",+ "display_name": "value",+ "fingerprint": {+ "global": {+ "distinct-count": 15,+ "nil%": 0+ },+ "type": {+ "type/Number": {+ "skewness": 0,+ "min": 1,+ "q1": 4,+ "q3": 12,+ "excess-kurtosis": -1.1999999999999997,+ "zero-fraction": 0,+ "mode-fraction": 0.06666666666666667,+ "top-3-fraction": 0.2,+ "max": 15,+ "sd": 4.47213595499958,+ "avg": 8+ }+ }+ },+ "base_type": "type/BigInteger"+ }+ ]+ },+ "format-rows?": true,+ "pivot-export-options": {+ "show-row-totals": true,+ "show-column-totals": true+ },+ "cols": [+ {+ "database_type": "CHARACTER VARYING",+ "lib/deduplicated-name": "LABEL",+ "lib/original-name": "LABEL",+ "name": "LABEL",+ "lib/source": "source/native",+ "lib/source-column-alias": "LABEL",+ "source": "native",+ "field_ref": [+ "field",+ "LABEL",+ {+ "base-type": "type/Text"+ }+ ],+ "effective_type": "type/Text",+ "lib/desired-column-alias": "LABEL",+ "display_name": "LABEL",+ "base_type": "type/Text"+ },+ {+ "database_type": "BIGINT",+ "lib/deduplicated-name": "value",+ "lib/original-name": "value",+ "name": "value",+ "lib/source": "source/native",+ "lib/source-column-alias": "value",+ "source": "native",+ "field_ref": [+ "field",+ "value",+ {+ "base-type": "type/BigInteger"+ }+ ],+ "effective_type": "type/BigInteger",+ "lib/desired-column-alias": "value",+ "display_name": "value",+ "base_type": "type/BigInteger"+ }+ ],+ "native_form": {+ "lib/type": "mbql.stage/native",+ "query": "SELECT\n CASE WHEN X <= 1 THEN 'Short label ' || x ELSE 'Really loooooooong label ' || x END AS label,\n x AS \"value\"\nFROM SYSTEM_RANGE(1, 15)"+ },+ "is_sandboxed": false,+ "insights": null+ }+ }+]diff --git a/frontend/src/metabase/static-viz/components/ComboChart/stories-data/index.ts b/frontend/src/metabase/static-viz/components/ComboChart/stories-data/index.tsindex fcdaa82af57..3050e5fd1c6 100644--- a/frontend/src/metabase/static-viz/components/ComboChart/stories-data/index.ts+++ b/frontend/src/metabase/static-viz/components/ComboChart/stories-data/index.ts@@ -10,6 +10,7 @@ import areaStackInterpolateMissingValues from "./area-stack-interpolate-missing-import areaStackLinearXAxis from "./area-stack-linear-x-axis.json";import areaStackedAutoDataLabels from "./area-stacked-auto-data-labels.json";import autoYSplit from "./auto-y-split.json";+import bar45DegreeLabels from "./bar-45-degree-labels.json";import barAutoFormattingCompact from "./bar-auto-formatting-compact.json";import barAutoFormattingFull from "./bar-auto-formatting-full.json";import barBreakoutWithLineSeriesStackedRightAxisOnly from "./bar-breakout-with-line-series-stacked-right-axis-only.json";@@ -262,4 +263,5 @@ export const data = {lineChartSplitPanelsTimeseriesDifferentRanges,comboSplitPanelsMixedSeriesDisplaySettings,barSplitPanelsOrdinalMixedTicksWidthsPerPanel,+ bar45DegreeLabels,};diff --git a/frontend/src/metabase/static-viz/components/ComboChart/stories/Bar45DegreeLabels.stories.tsx b/frontend/src/metabase/static-viz/components/ComboChart/stories/Bar45DegreeLabels.stories.tsxnew file mode 100644index 00000000000..567d60b3157--- /dev/null+++ b/frontend/src/metabase/static-viz/components/ComboChart/stories/Bar45DegreeLabels.stories.tsx@@ -0,0 +1,17 @@+import { StaticVisualization } from "../../StaticVisualization";+import { data } from "../stories-data";++import { Template, renderingContext } from "./shared";++export default {+ title: "Viz/Static Viz/ComboChart",+ component: StaticVisualization,+};++export const Bar45DegreeLabels = {+ render: Template,+ args: {+ rawSeries: data.bar45DegreeLabels,+ renderingContext,+ },+};diff --git a/frontend/src/metabase/static-viz/components/ComboChart/ComboChart.stories.tsx b/frontend/src/metabase/static-viz/components/ComboChart/stories/ComboChart.stories.tsxsimilarity index 95%rename from frontend/src/metabase/static-viz/components/ComboChart/ComboChart.stories.tsxrename to frontend/src/metabase/static-viz/components/ComboChart/stories/ComboChart.stories.tsxindex e4ac29b00e8..d630e89a536 100644--- a/frontend/src/metabase/static-viz/components/ComboChart/ComboChart.stories.tsx+++ b/frontend/src/metabase/static-viz/components/ComboChart/stories/ComboChart.stories.tsx@@ -1,40 +1,21 @@-import type { StoryFn } from "@storybook/react";import { updateIn } from "icepick";-import {- measureTextHeight,- measureTextWidth,-} from "metabase/static-viz/lib/text";import { color } from "metabase/ui/colors";-import { DEFAULT_VISUALIZATION_THEME } from "metabase/visualizations/shared/utils/theme";-import type { RenderingContext } from "metabase/visualizations/types";-import type { StaticChartProps } from "../StaticVisualization";-import { StaticVisualization } from "../StaticVisualization";+import { StaticVisualization } from "../../StaticVisualization";+import { data } from "../stories-data";-import { data } from "./stories-data";+import { Template, renderingContext } from "./shared";export default {title: "Viz/Static Viz/ComboChart",component: StaticVisualization,};-const Template: StoryFn<StaticChartProps> = (args) => {- return (- <div style={{ border: "1px solid black", display: "inline-block" }}>- <StaticVisualization {...args} isStorybook />- </div>- );-};--const renderingContext: RenderingContext = {- getColor: color,- measureText: (text, style) =>- measureTextWidth(text, Number(style.size), Number(style.weight)),- measureTextHeight: (_, style) => measureTextHeight(Number(style.size)),- fontFamily: "Lato",- theme: DEFAULT_VISUALIZATION_THEME,-};+// ================================+// the Loki stress test workflow runs on entire files, not individual stories, so stress testing this file is extremely slow+// add new stories to their own file, see Bar45DegreeLabels.stories.tsx for an example+// ================================export const LineLinearXScale = {render: Template,@@ -1121,3 +1102,8 @@ export const BarSplitPanelsOrdinalMixedTicksWidthsPerPanel = {renderingContext,},};++// ================================+// the Loki stress test workflow runs on entire files, not individual stories, so stress testing this file is extremely slow+// add new stories to their own file, see Bar45DegreeLabels.stories.tsx for an example+// ================================diff --git a/frontend/src/metabase/static-viz/components/ComboChart/stories/shared.tsx b/frontend/src/metabase/static-viz/components/ComboChart/stories/shared.tsxnew file mode 100644index 00000000000..5e2d9d40822--- /dev/null+++ b/frontend/src/metabase/static-viz/components/ComboChart/stories/shared.tsx@@ -0,0 +1,29 @@+import type { StoryFn } from "@storybook/react";++import {+ measureTextHeight,+ measureTextWidth,+} from "metabase/static-viz/lib/text";+import { color } from "metabase/ui/colors";+import { DEFAULT_VISUALIZATION_THEME } from "metabase/visualizations/shared/utils/theme";+import type { RenderingContext } from "metabase/visualizations/types";++import type { StaticChartProps } from "../../StaticVisualization";+import { StaticVisualization } from "../../StaticVisualization";++export const Template: StoryFn<StaticChartProps> = (args) => {+ return (+ <div style={{ border: "1px solid black", display: "inline-block" }}>+ <StaticVisualization {...args} isStorybook />+ </div>+ );+};++export const renderingContext: RenderingContext = {+ getColor: color,+ measureText: (text, style) =>+ measureTextWidth(text, Number(style.size), Number(style.weight)),+ measureTextHeight: (_, style) => measureTextHeight(Number(style.size)),+ fontFamily: "Lato",+ theme: DEFAULT_VISUALIZATION_THEME,+};diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.tsindex 167da7db629..cc1b0a56472 100644--- a/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts+++ b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts@@ -508,16 +508,119 @@ const getTicksDimensions = (// "1,000", which is significantly longer than "255".const TICK_OVERFLOW_BUFFER = 4;+// don't allow overflow greater than 12.5% of the chart width+const MAX_OVERFLOW_PERCENTAGE = 0.125;++const getTicksOverflow = (+ input: ChartLayoutInput,+ settings: ComputedVisualizationSettings,+ ticksDimensions: TicksDimensions,+ axisEnabledSetting: ComputedVisualizationSettings["graph.x_axis.axis_enabled"],+ chartWidth: number,+ padding: Padding, // this is not the final padding - the overflow returned here will be added to it+) => {+ const { seriesModels, xAxisModel } = input;++ const maxOverflow = chartWidth * MAX_OVERFLOW_PERCENTAGE;++ // We handle non-categorical scatter plots differently, because echarts places+ // the tick labels on the very edge of the x-axis for scatter plots only.+ const isScatterPlot =+ seriesModels?.some((seriesModel) => {+ const seriesSettings = settings.series?.(+ seriesModel.legacySeriesSettingsObjectKey,+ );+ return seriesSettings?.display === "scatter";+ }) ?? false;++ if (isScatterPlot && xAxisModel.axisType !== "category") {+ const firstTickOverflow = Math.min(+ Math.max(+ ticksDimensions.firstXTickWidth / 2 -+ padding.left ++ TICK_OVERFLOW_BUFFER,+ 0,+ ),+ maxOverflow,+ );+ const lastTickOverflow = Math.min(+ Math.max(+ ticksDimensions.lastXTickWidth / 2 -+ padding.right ++ TICK_OVERFLOW_BUFFER,+ 0,+ ),+ maxOverflow,+ );+ return { firstTickOverflow, lastTickOverflow };+ }++ const currentBoundaryWidth = chartWidth - padding.left - padding.right;+ const dimensionWidth = getDimensionWidth(input, currentBoundaryWidth);++ // labels rotated at 45 degrees have their right edge aligned with the tick mark, so need different treatment+ if (axisEnabledSetting === "rotate-45") {+ const dataset = getDataset(input);+ // the first label does not necessarily extend the farthest to the left+ const numLabelsToCheck = Math.min(+ // since we cap the overflow, we don't need to check all labels+ Math.ceil(dataset.length * MAX_OVERFLOW_PERCENTAGE),+ // this ensures we don't check too many labels - 20 is sufficient for all reasonable cases+ 20,+ );+ let firstTickOverflow = 0;+ for (let i = 0; i < numLabelsToCheck; i++) {+ const label = getPaddedAxisLabel(+ xAxisModel.formatter(dataset[i][X_AXIS_DATA_KEY]),+ );+ const width = ticksDimensions.getXTickWidth(label);+ firstTickOverflow = Math.max(+ firstTickOverflow,+ width -+ dimensionWidth * (i + 0.5) -+ padding.left ++ TICK_OVERFLOW_BUFFER,+ );+ }+ return {+ firstTickOverflow: Math.min(firstTickOverflow, maxOverflow),+ lastTickOverflow: 0,+ };+ }++ const firstTickOverflow = Math.min(+ Math.max(+ ticksDimensions.firstXTickWidth / 2 -+ dimensionWidth / 2 -+ padding.left ++ TICK_OVERFLOW_BUFFER,+ 0,+ ),+ maxOverflow,+ );+ const lastTickOverflow = Math.min(+ Math.max(+ ticksDimensions.lastXTickWidth / 2 -+ dimensionWidth / 2 -+ padding.right ++ TICK_OVERFLOW_BUFFER,+ 0,+ ),+ maxOverflow,+ );+ return { firstTickOverflow, lastTickOverflow };+};+export const getChartPadding = (input: ChartLayoutInput,settings: ComputedVisualizationSettings,ticksDimensions: TicksDimensions,axisEnabledSetting: ComputedVisualizationSettings["graph.x_axis.axis_enabled"],chartWidth: number,- { theme }: RenderingContext,+ renderingContext: RenderingContext,): Padding => {- const { xAxisModel, leftAxisModel, rightAxisModel, seriesModels } = input;- const { fontSize } = theme.cartesian.label;+ const { leftAxisModel, rightAxisModel } = input;+ const { fontSize } = renderingContext.theme.cartesian.label;const axisNameFontSize = fontSize;const seriesLabelFontSize = fontSize;@@ -562,66 +665,14 @@ export const getChartPadding = (padding.right += yAxisNameTotalWidth;}- const maxOverflow = chartWidth / 8; // don't allow overflow greater than 12.5% of the chart width- let firstTickOverflow: number;- let lastTickOverflow: number;-- // We handle non-categorical scatter plots differently, because echarts places- // the tick labels on the very edge of the x-axis for scatter plots only.- const isScatterPlot =- seriesModels?.some((seriesModel) => {- const seriesSettings = settings.series?.(- seriesModel.legacySeriesSettingsObjectKey,- );- return seriesSettings?.display === "scatter";- }) ?? false;- if (isScatterPlot && xAxisModel.axisType !== "category") {- firstTickOverflow = Math.min(- Math.max(- ticksDimensions.firstXTickWidth / 2 -- padding.left +- TICK_OVERFLOW_BUFFER,- 0,- ),- maxOverflow,- );- lastTickOverflow = Math.min(- Math.max(- ticksDimensions.lastXTickWidth / 2 -- padding.right +- TICK_OVERFLOW_BUFFER,- 0,- ),- maxOverflow,- );- } else {- const currentBoundaryWidth = chartWidth - padding.left - padding.right;- const dimensionWidth = getDimensionWidth(input, currentBoundaryWidth);-- firstTickOverflow = Math.min(- Math.max(- ticksDimensions.firstXTickWidth / 2 -- dimensionWidth / 2 -- padding.left +- TICK_OVERFLOW_BUFFER,- 0,- ),- maxOverflow,- );- lastTickOverflow = 0;- if (axisEnabledSetting !== "rotate-45") {- lastTickOverflow = Math.min(- Math.max(- ticksDimensions.lastXTickWidth / 2 -- dimensionWidth / 2 -- padding.right +- TICK_OVERFLOW_BUFFER,- 0,- ),- maxOverflow,- );- }- }+ const { firstTickOverflow, lastTickOverflow } = getTicksOverflow(+ input,+ settings,+ ticksDimensions,+ axisEnabledSetting,+ chartWidth,+ padding,+ );padding.left += firstTickOverflow;padding.right += lastTickOverflow;