Skip to content

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

Time series axis label improvements (#70814)

Kevin Kelleher027a2bede14b
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_All_Series_Without_Interpolation.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_All_Series_Without_Interpolation.png
index 5f3dd618bc1..0aba80ce74a 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_All_Series_Without_Interpolation.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_All_Series_Without_Interpolation.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Interpolate_Missing_Values.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Interpolate_Missing_Values.png
index 39f6f21ccd6..68ce266f1a8 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Interpolate_Missing_Values.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Interpolate_Missing_Values.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Between_Extent_And_Chart_Bound.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Between_Extent_And_Chart_Bound.png
index 4195128203a..43ef82803e0 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Between_Extent_And_Chart_Bound.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Between_Extent_And_Chart_Bound.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Disabled.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Disabled.png
index 3d30809f13e..e4030cca966 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Disabled.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Disabled.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Log_Y_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Log_Y_Scale.png
index b5e2b7e5c0c..b97cfab6b14 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Log_Y_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Log_Y_Scale.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Out_Of_Bounds_37848.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Out_Of_Bounds_37848.png
index 621bde78ec4..ed607c794b7 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Out_Of_Bounds_37848.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Out_Of_Bounds_37848.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Very_Small.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Very_Small.png
index 9777a3fbaae..746235812b5 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Very_Small.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Very_Small.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Incorrect_Label_Y_Axis_Split_41285.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Incorrect_Label_Y_Axis_Split_41285.png
index 099fcd5d558..40dd6bcd271 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Incorrect_Label_Y_Axis_Split_41285.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Incorrect_Label_Y_Axis_Split_41285.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Equals_Extents.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Equals_Extents.png
index 1aef0eb73d6..b2fb254ba24 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Equals_Extents.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Equals_Extents.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_High.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_High.png
index da67f93d634..c34aae56373 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_High.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_High.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_Low.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_Low.png
index ed384659f2b..ff8e8869a1f 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_Low.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Custom_Y_Axis_Range_Off_Screen_Low.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale.png
index 6019ef4ece7..fdc50e01f53 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale_Negative.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale_Negative.png
index 7ffb310c823..b1605c5b996 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale_Negative.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale_Negative.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Auto.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Auto.png
index 3d30809f13e..e4030cca966 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Auto.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Auto.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Off.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Off.png
index 3d30809f13e..e4030cca966 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Off.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_Off.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_On.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_On.png
index 91f34b2b896..3f34d51995f 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_On.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Show_Dots_On.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Unpin_From_Zero.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Unpin_From_Zero.png
index 55a07ef53c0..09578adf0cf 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Unpin_From_Zero.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Unpin_From_Zero.png differ
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 b2452162b42..802196d9d27 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_Number_Of_Insights_Error_39608.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Number_Of_Insights_Error_39608.png
index 6a13d89a36f..4c16e455839 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Number_Of_Insights_Error_39608.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Number_Of_Insights_Error_39608.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Bar.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Bar.png
index 0874815cc7e..2ab6b370176 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Bar.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Bar.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Stacked_Bar.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Stacked_Bar.png
index 83968e2a65f..87b7f55c9f5 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Stacked_Bar.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Multi_Series_Stacked_Bar.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Single_Series_Bar.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Single_Series_Bar.png
index 6904229ace4..671ccc58a64 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Single_Series_Bar.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Single_Series_Bar.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Wrong_Y_Axis_Range_37306.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Wrong_Y_Axis_Range_37306.png
index 6eb06b3d85d..d45794337e1 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Wrong_Y_Axis_Range_37306.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Wrong_Y_Axis_Range_37306.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Timeseries_X_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Timeseries_X_Scale.png
index 1023f46bac1..41d797d5e2c 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Timeseries_X_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Timeseries_X_Scale.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels_Timeseries.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels_Timeseries.png
index 88493acc048..2d8bb239ab4 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels_Timeseries.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Data_Labels_Timeseries.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Enourmous_Dataset.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Enourmous_Dataset.png
index b22ef7385ba..ea7de7e640a 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Enourmous_Dataset.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Enourmous_Dataset.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale.png
index bcd36a9dc7f..48de17754e1 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale_Negative.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale_Negative.png
index 09e9576d99a..6bfa2f3991f 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale_Negative.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Log_Y_Scale_Negative.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Native_Time_Series_Quarter.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Native_Time_Series_Quarter.png
index 1aebf5ce8d6..f7980cae0b3 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Native_Time_Series_Quarter.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Native_Time_Series_Quarter.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Negative_Only.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Negative_Only.png
index 13e07677124..f0e34a67a80 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Negative_Only.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Negative_Only.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_No_Total_Timeseries.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_No_Total_Timeseries.png
index 4baf2f40fbe..8ea2e613045 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_No_Total_Timeseries.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_No_Total_Timeseries.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale_Negative_Only.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale_Negative_Only.png
index 09e9576d99a..6bfa2f3991f 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale_Negative_Only.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Pow_Y_Scale_Negative_Only.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale.png
index 38d8cbd8ee6..d8a0aa762c4 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale_Unsorted.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale_Unsorted.png
index 38d8cbd8ee6..d8a0aa762c4 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale_Unsorted.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_WaterfallChart_Timeseries_X_Scale_Unsorted.png differ
diff --git a/e2e/test/scenarios/binning/qb-explicit-joins.cy.spec.js b/e2e/test/scenarios/binning/qb-explicit-joins.cy.spec.js
index 823fcf75cfd..7ef658ef3dc 100644
--- a/e2e/test/scenarios/binning/qb-explicit-joins.cy.spec.js
+++ b/e2e/test/scenarios/binning/qb-explicit-joins.cy.spec.js
@@ -85,12 +85,7 @@ describe("scenarios > binning > from a saved QB question with explicit joins", (
cy.findByText("Quarter").click();
cy.wait("@dataset");
- H.echartsContainer()
- .get("text")
- .should("contain", "Q1 1968")
- .and("contain", "Q1 1978")
- .and("contain", "Q1 1988")
- .and("contain", "Q1 1998");
+ H.echartsContainer().get("text").should("contain", "Q1");
});
it("should work for number", () => {
@@ -218,10 +213,7 @@ describe("scenarios > binning > from a saved QB question with explicit joins", (
H.echartsContainer()
.get("text", { timeout: 1000 })
- .should("contain", "January 1968")
- .and("contain", "January 1978")
- .and("contain", "January 1988")
- .and("contain", "January 1998");
+ .should("contain", "January");
H.cartesianChartCircle();
@@ -234,14 +226,7 @@ describe("scenarios > binning > from a saved QB question with explicit joins", (
// eslint-disable-next-line metabase/no-unscoped-text-selectors -- deprecated usage
cy.findByText("Count by People → Birth Date: Quarter");
- H.echartsContainer()
- .get("text")
- .should("contain", "Q1 1965")
- .and("contain", "Q1 1972")
- .and("contain", "Q1 1979")
- .and("contain", "Q1 1986")
- .and("contain", "Q1 1993")
- .and("contain", "Q1 2000");
+ H.echartsContainer().get("text").should("contain", "Q1");
});
it("should work for number", () => {
diff --git a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
index b12f0a5971d..22e9657ed09 100644
--- a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
+++ b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
@@ -1089,7 +1089,7 @@ describe("scenarios > embedding > dashboard appearance", () => {
H.getIframeBody().within(() => {
cy.findByText(questionDetails.name).should("exist");
- cy.findByText("April 2022").should("exist");
+ cy.findByText("May 2022").should("exist");
// TODO: Enable this once we fix the flakiness https://app.trunk.io/metabase/flaky-tests/test/facb35f0-6d76-5e7d-b21c-40401bbc3ff6?repo=metabase%2Fmetabase
// (metabase#49537)
diff --git a/e2e/test/scenarios/visualizations-tabular/drillthroughs/chart_drill.cy.spec.js b/e2e/test/scenarios/visualizations-tabular/drillthroughs/chart_drill.cy.spec.js
index a6b88e61a91..71c4602d3c9 100644
--- a/e2e/test/scenarios/visualizations-tabular/drillthroughs/chart_drill.cy.spec.js
+++ b/e2e/test/scenarios/visualizations-tabular/drillthroughs/chart_drill.cy.spec.js
@@ -53,7 +53,7 @@ describe("scenarios > visualizations > drillthroughs > chart drill", () => {
);
H.queryBuilderMain().within(() => {
- H.echartsContainer().findByText("June 2022"); // more granular axis labels
+ H.echartsContainer().findByText("June 5, 2022"); // more granular axis labels
// confirm that product category is still broken out
cy.findByLabelText("Legend").within(() => {
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts
index 88baf60fa0d..167da7db629 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/layout/index.ts
@@ -414,6 +414,7 @@ const getTicksDimensions = (
xTicksHeight: 0,
firstXTickWidth: 0,
lastXTickWidth: 0,
+ getXTickWidth: () => 0,
};
if (leftAxisModel) {
@@ -479,6 +480,21 @@ const getTicksDimensions = (
(isTimeSeries && hasTimelineEvents
? CHART_STYLE.timelineEvents.height
: 0);
+
+ ticksDimensions.getXTickWidth = (text: string) => {
+ if (axisEnabledSetting === "rotate-90") {
+ return renderingContext.theme.cartesian.label.fontSize;
+ }
+ const width = renderingContext.measureText(text, {
+ ...CHART_STYLE.axisTicks,
+ size: renderingContext.theme.cartesian.label.fontSize,
+ family: renderingContext.fontFamily,
+ });
+ if (axisEnabledSetting === "rotate-45") {
+ return width / Math.SQRT2;
+ }
+ return width;
+ };
}
return { ticksDimensions, axisEnabledSetting };
@@ -836,6 +852,7 @@ export const getChartLayout = (
bounds,
boundaryWidth,
outerHeight: height,
+ outerWidth: width,
axisEnabledSetting,
stackedBarTicksRotation,
panelGap: 0,
@@ -887,6 +904,7 @@ const computeSplitPanelLayout = (
xTicksHeight: computedTicks.xTicksHeight,
firstXTickWidth: computedTicks.firstXTickWidth,
lastXTickWidth: computedTicks.lastXTickWidth,
+ getXTickWidth: computedTicks.getXTickWidth,
};
const padding = getChartPadding(
@@ -925,6 +943,7 @@ const computeSplitPanelLayout = (
bounds,
boundaryWidth,
outerHeight: height,
+ outerWidth: width,
axisEnabledSetting,
panelHeight,
panelGap,
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/layout/types.ts b/frontend/src/metabase/visualizations/echarts/cartesian/layout/types.ts
index 3d6e39142a9..5c17b11d0fe 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/layout/types.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/layout/types.ts
@@ -9,6 +9,7 @@ export interface TicksDimensions {
xTicksHeight: number;
firstXTickWidth: number;
lastXTickWidth: number;
+ getXTickWidth: (text: string) => number;
}
export interface ChartBoundsCoords {
@@ -26,6 +27,7 @@ export interface ChartLayout {
bounds: ChartBoundsCoords;
boundaryWidth: number;
outerHeight: number;
+ outerWidth: number;
axisEnabledSetting: ComputedVisualizationSettings["graph.x_axis.axis_enabled"];
stackedBarTicksRotation?: TicksRotation;
panelHeight?: number;
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts
index 8bc6c986096..834792833ca 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/axis.ts
@@ -190,7 +190,6 @@ const getCommonDimensionAxisOptions = (
export const buildDimensionAxis = (
chartModel: BaseCartesianChartModel,
- width: number,
settings: ComputedVisualizationSettings,
chartLayout: ChartLayout,
hasTimelineEvents: boolean,
@@ -210,7 +209,6 @@ export const buildDimensionAxis = (
if (isTimeSeriesAxis(xAxisModel)) {
return buildTimeSeriesDimensionAxis(
xAxisModel,
- width,
hasTimelineEvents,
settings,
chartLayout,
@@ -275,14 +273,13 @@ export const buildNumericDimensionAxis = (
export const buildTimeSeriesDimensionAxis = (
xAxisModel: TimeSeriesXAxisModel,
- width: number,
hasTimelineEvents: boolean,
settings: ComputedVisualizationSettings,
chartLayout: ChartLayout,
renderingContext: RenderingContext,
): XAXisOption => {
const { formatter, maxInterval, minInterval, canRender, xDomainPadded } =
- getTicksOptions(xAxisModel, width);
+ getTicksOptions(xAxisModel, chartLayout);
return {
...getCommonDimensionAxisOptions(chartLayout, settings, renderingContext),
@@ -456,7 +453,6 @@ const buildMetricsAxes = (
export const buildAxes = (
chartModel: BaseCartesianChartModel,
- width: number,
chartLayout: ChartLayout,
settings: ComputedVisualizationSettings,
hasTimelineEvents: boolean,
@@ -465,7 +461,6 @@ export const buildAxes = (
return {
xAxis: buildDimensionAxis(
chartModel,
- width,
settings,
chartLayout,
hasTimelineEvents,
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/index.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/index.ts
index cb379100fef..cf5d84511f7 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/index.ts
@@ -274,7 +274,6 @@ export const getCartesianChartOption = (
if (isSplitPanels) {
const baseXAxis = buildDimensionAxis(
chartModel,
- chartWidth,
settings,
chartLayout,
hasTimelineEvents,
@@ -304,7 +303,6 @@ export const getCartesianChartOption = (
const axes = ensureRoomForLabels(
buildAxes(
chartModel,
- chartWidth,
chartLayout,
settings,
hasTimelineEvents,
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/index.unit.spec.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/index.unit.spec.ts
index 41c31419067..521c71b7789 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/index.unit.spec.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/index.unit.spec.ts
@@ -90,7 +90,6 @@ describe("ensureRoomForLabels", () => {
const axes = buildAxes(
chartModel,
- chartWidth,
chartLayout,
mockSettings,
hasTimelineEvents,
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/ticks.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/ticks.ts
index a321f18bd43..371bdb9d312 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/ticks.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/ticks.ts
@@ -3,12 +3,14 @@ import dayjs from "dayjs";
import type { ContinuousDomain } from "metabase/visualizations/shared/types/scale";
+import type { ChartLayout } from "../layout/types";
import type {
TimeSeriesAxisFormatter,
TimeSeriesXAxisModel,
} from "../model/types";
import {
computeTimeseriesTicksInterval,
+ getFormatter,
getLargestInterval,
getTimeSeriesIntervalDuration,
} from "../utils/timeseries";
@@ -27,7 +29,7 @@ const getPadding = (intervalsCount: number) => {
export const getTicksOptions = (
xAxisModel: TimeSeriesXAxisModel,
- chartWidth: number,
+ chartLayout: ChartLayout,
) => {
const { range, toEChartsAxisValue, interval, intervalsCount } = xAxisModel;
@@ -56,18 +58,12 @@ export const getTicksOptions = (
const computedInterval = computeTimeseriesTicksInterval(
xDomain,
interval,
- chartWidth,
- xAxisModel.formatter,
+ chartLayout,
+ formatter,
);
const largestInterval = getLargestInterval([computedInterval, interval]);
- // If the data interval is week but due to available space and the range of the chart
- // we decide to show monthly, yearly or even larger ticks, we should format ticks values as months.
- if (interval.unit === "week" && largestInterval.unit !== "week") {
- formatter = (value) => {
- return xAxisModel.formatter(value, "month");
- };
- }
+ formatter = getFormatter(formatter, interval.unit, largestInterval.unit);
const isWithinRange = (date: Dayjs) => {
return date.isAfter(paddedMin) && date.isBefore(paddedMax);
@@ -82,7 +78,9 @@ export const getTicksOptions = (
if (largestInterval.unit === "week") {
const startOfWeek = range[0].day();
canRender = (date: Dayjs) =>
- isWithinRange(date) && date.day() === startOfWeek;
+ isWithinRange(date) &&
+ date.day() === startOfWeek &&
+ date.week() % largestInterval.count === 0;
const effectiveTicksUnit = "day";
maxInterval = getTimeSeriesIntervalDuration({
count: 1,
@@ -95,7 +93,10 @@ export const getTicksOptions = (
// (like February with 28 days) are shorter than others (31 days).
// Instead, we force ECharts to generate daily ticks and filter to month starts.
if (largestInterval.unit === "month") {
- canRender = (date: Dayjs) => isWithinRange(date) && date.date() === 1;
+ canRender = (date: Dayjs) =>
+ isWithinRange(date) &&
+ date.date() === 1 &&
+ date.month() % largestInterval.count === 0;
maxInterval = getTimeSeriesIntervalDuration({
count: 1,
unit: "day",
@@ -109,7 +110,9 @@ export const getTicksOptions = (
if (!isSingleItem && largestInterval.unit === "quarter") {
const effectiveTicksUnit = "month";
canRender = (date: Dayjs) =>
- isWithinRange(date) && date.startOf("quarter").isSame(date, "month");
+ isWithinRange(date) &&
+ date.startOf("quarter").isSame(date, "month") &&
+ (date.quarter() - 1) % largestInterval.count === 0;
maxInterval = getTimeSeriesIntervalDuration({
count: 1,
unit: effectiveTicksUnit,
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/scatter/option/index.ts b/frontend/src/metabase/visualizations/echarts/cartesian/scatter/option/index.ts
index 2d290c9ae2b..3298852303f 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/scatter/option/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/scatter/option/index.ts
@@ -99,7 +99,6 @@ export function getScatterPlotOption(
if (isSplitPanels) {
const baseXAxis = buildDimensionAxis(
chartModel,
- chartWidth,
settings,
chartLayout,
hasTimelineEvents,
@@ -116,7 +115,6 @@ export function getScatterPlotOption(
} else {
const axes = buildAxes(
chartModel,
- chartWidth,
chartLayout,
settings,
hasTimelineEvents,
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.ts b/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.ts
index 2b24f3fb291..57fccb3e513 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.ts
@@ -4,7 +4,11 @@ import _ from "underscore";
import { parseTimestamp } from "metabase/utils/time-dayjs";
import { isNotNull } from "metabase/utils/types";
-import type { TimeSeriesInterval } from "metabase/visualizations/echarts/cartesian/model/types";
+import type {
+ CartesianChartDateTimeAbsoluteUnit,
+ TimeSeriesAxisFormatter,
+ TimeSeriesInterval,
+} from "metabase/visualizations/echarts/cartesian/model/types";
import {
multipleTimezoneWarning,
unexpectedTimezoneWarning,
@@ -17,6 +21,8 @@ import type {
} from "metabase-types/api";
import type { ShowWarning } from "../../types";
+import type { ChartLayout } from "../layout/types";
+import { getPaddedAxisLabel } from "../option/utils";
export const tryGetDate = (rowValue: RowValue): Dayjs | null => {
if (typeof rowValue === "boolean") {
@@ -36,8 +42,8 @@ export const msToDays = (ms: number) => ms / (24 * 60 * 60 * 1000);
//
// Count and time interval for axis.ticks()
//
-export const TIMESERIES_INTERVALS: (TimeSeriesInterval & {
- testFn: (date: Dayjs) => number;
+export const TICKS_TIMESERIES_INTERVALS: (TimeSeriesInterval & {
+ testFn?: (date: Dayjs) => number;
})[] = [
{ unit: "ms", count: 1, testFn: (_d: Dayjs) => 0 }, // (0) millisecond
{ unit: "second", count: 1, testFn: (d: Dayjs) => d.millisecond() }, // (1) 1 second
@@ -54,15 +60,30 @@ export const TIMESERIES_INTERVALS: (TimeSeriesInterval & {
{ unit: "hour", count: 12, testFn: (d: Dayjs) => d.hour() % 12 }, // (12) 12 hours
{ unit: "day", count: 1, testFn: (d: Dayjs) => d.hour() }, // (13) 1 day
{ unit: "week", count: 1, testFn: (d: Dayjs) => d.day() }, // (14) 1 week
+ { unit: "week", count: 2 },
{ unit: "month", count: 1, testFn: (d: Dayjs) => d.date() }, // (15) 1 month
+ { unit: "month", count: 2 },
{ unit: "quarter", count: 1, testFn: (d: Dayjs) => d.month() % 3 }, // (16) 3 months / 1 quarter
+ { unit: "quarter", count: 2 },
{ unit: "year", count: 1, testFn: (d: Dayjs) => d.month() }, // (17) 1 year
{ unit: "year", count: 2, testFn: (d: Dayjs) => d.year() % 2 }, // (18) 2 year
+ { unit: "year", count: 5 },
{ unit: "year", count: 10, testFn: (d: Dayjs) => d.year() % 10 }, // (19) 10 year
{ unit: "year", count: 50, testFn: (d: Dayjs) => d.year() % 50 }, // (20) 50 year
{ unit: "year", count: 100, testFn: (d: Dayjs) => d.year() % 100 }, // (21) 100 year
];
+// we use some extra intervals in computeTimeseriesTicksInterval - the ones without testFn
+// however, computeTimeseriesDataInterval requires that intervals be multiples of each other
+// so we filter out those extra ones here
+// see #43421
+const DATA_TIMESERIES_INTERVALS: (TimeSeriesInterval & {
+ testFn: (date: Dayjs) => number;
+})[] = TICKS_TIMESERIES_INTERVALS.filter(
+ (i): i is TimeSeriesInterval & { testFn: (date: Dayjs) => number } =>
+ i.testFn != null,
+);
+
// mapping from Metabase "unit" to d3 intervals above
const INTERVAL_INDEX_BY_UNIT: Record<DateTimeAbsoluteUnit, number> = {
minute: 5,
@@ -95,18 +116,18 @@ export function computeTimeseriesDataInterval(
xValues = xValues.filter(isNotNull);
if (unit && INTERVAL_INDEX_BY_UNIT[unit] != null) {
- return TIMESERIES_INTERVALS[INTERVAL_INDEX_BY_UNIT[unit]];
+ return DATA_TIMESERIES_INTERVALS[INTERVAL_INDEX_BY_UNIT[unit]];
}
// Always use 'day' when there's just one value.
if (xValues.length === 1) {
- return TIMESERIES_INTERVALS.find((i) => i.unit === "day");
+ return DATA_TIMESERIES_INTERVALS.find((i) => i.unit === "day");
}
// run each interval's test function on each value
const valueLists = xValues.map((xValue) => {
const parsed = parseTimestamp(xValue);
- return TIMESERIES_INTERVALS.map((interval) => interval.testFn(parsed));
+ return DATA_TIMESERIES_INTERVALS.map((interval) => interval.testFn(parsed));
});
// count the number of different values for each interval
@@ -116,7 +137,9 @@ export function computeTimeseriesDataInterval(
let index = intervalCounts.findIndex((size) => size !== 1);
// special case to check: did we get tripped up by the week interval?
- const weekIndex = TIMESERIES_INTERVALS.findIndex((i) => i.unit === "week");
+ const weekIndex = DATA_TIMESERIES_INTERVALS.findIndex(
+ (i) => i.unit === "week",
+ );
if (index === weekIndex && intervalCounts[weekIndex + 1] === 1) {
index = intervalCounts.findIndex(
(size, index) => size !== 1 && index > weekIndex,
@@ -125,11 +148,11 @@ export function computeTimeseriesDataInterval(
// if we ran off the end of intervals, return the last one
if (index === -1) {
- return TIMESERIES_INTERVALS[TIMESERIES_INTERVALS.length - 1];
+ return DATA_TIMESERIES_INTERVALS[DATA_TIMESERIES_INTERVALS.length - 1];
}
// index currently points to the first item with multiple values, so move it to the previous interval
- return TIMESERIES_INTERVALS[index - 1];
+ return DATA_TIMESERIES_INTERVALS[index - 1];
}
// ------------------------- Computing the TIMESERIES_INTERVALS entry to use for a chart ------------------------- //
@@ -142,45 +165,87 @@ export function getTimeSeriesIntervalDuration(interval: TimeSeriesInterval) {
return dayjs(0).add(interval.count, interval.unit).valueOf();
}
-/// Return the number of ticks we can expect to see over a time range using the TIMESERIES_INTERVALS entry interval.
-/// for example a "5 seconds" interval over a time range of a minute should have an expected tick count of 20.
-function expectedTickCount(
+// Counts interval boundary crossings within the domain
+export function expectedTickCount(
interval: TimeSeriesInterval,
- timeRangeMilliseconds: number,
-) {
- return Math.ceil(
- timeRangeMilliseconds / getTimeSeriesIntervalDuration(interval),
- );
+ xDomain: ContinuousDomain,
+): number {
+ const { unit, count } = interval;
+ const start = dayjs.utc(xDomain[0]);
+ const end = dayjs.utc(xDomain[1]);
+
+ const startTrunc = start.startOf(unit);
+ const endTrunc = end.startOf(unit);
+
+ const diffUnits = endTrunc.diff(startTrunc, unit);
+
+ let startIdx: number;
+ if (unit === "year") {
+ startIdx = startTrunc.year();
+ } else if (unit === "quarter") {
+ startIdx = startTrunc.quarter() - 1;
+ } else if (unit === "month") {
+ startIdx = startTrunc.month();
+ } else if (unit === "week") {
+ startIdx = startTrunc.week();
+ } else if (unit === "day") {
+ startIdx = startTrunc.day();
+ } else if (unit === "hour") {
+ startIdx = startTrunc.hour();
+ } else if (unit === "minute") {
+ startIdx = startTrunc.minute();
+ } else if (unit === "second") {
+ startIdx = startTrunc.second();
+ } else {
+ startIdx = startTrunc.valueOf();
+ }
+
+ const startAligned = Math.ceil(startIdx / count) * count;
+ const endAligned = Math.floor((startIdx + diffUnits) / count) * count;
+
+ const diffAligned = (endAligned - startAligned) / count;
+
+ if (start.valueOf() === startTrunc.valueOf() || startIdx < startAligned) {
+ return diffAligned + 1;
+ }
+ return diffAligned;
}
/// Get the appropriate tick interval option from the TIMESERIES_INTERVALS above based on the xAxis bucketing
/// and the max number of ticks we want to show (itself calculated from chart width).
-function timeseriesTicksInterval(
+export function computeTimeseriesTicksInterval(
+ xDomain: ContinuousDomain,
xInterval: TimeSeriesInterval,
- timeRangeMilliseconds: number,
- maxTickCount: number,
- minTickCount: number = 2,
+ chartLayout: ChartLayout,
+ formatter: TimeSeriesAxisFormatter,
) {
+ const minTickCount = 2;
// first we want to find out where in TIMESERIES_INTERVALS we should start looking for a good match. Find the
// interval with a matching interval and count (e.g. `hour` and `1`) and we'll start there.
- let initialIndex = _.findIndex(TIMESERIES_INTERVALS, ({ unit, count }) => {
- return unit === xInterval.unit && count === xInterval.count;
- });
+ let initialIndex = _.findIndex(
+ TICKS_TIMESERIES_INTERVALS,
+ ({ unit, count }) => {
+ return unit === xInterval.unit && count === xInterval.count;
+ },
+ );
// if we weren't able to find something matching then we'll start from the beginning and try everything
if (initialIndex === -1) {
initialIndex = 0;
}
// Fallback value: the largest tick interval (every 100 years)
- let intervalIndex = TIMESERIES_INTERVALS.length - 1;
+ let intervalIndex = TICKS_TIMESERIES_INTERVALS.length - 1;
// Looking for the first interval which produces less ticks than the maxTicksCount.
// However, if it produces less than minTickCount ticks we prefer taking a smaller previous interval.
- for (let i = initialIndex; i < TIMESERIES_INTERVALS.length; i++) {
- const intervalTicksCount = expectedTickCount(
- TIMESERIES_INTERVALS[i],
- timeRangeMilliseconds,
+ for (let i = initialIndex; i < TICKS_TIMESERIES_INTERVALS.length; i++) {
+ const interval = TICKS_TIMESERIES_INTERVALS[i];
+ const maxTickCount = maxTicksForChartWidth(
+ chartLayout,
+ interval.unit,
+ getFormatter(formatter, xInterval.unit, interval.unit),
);
+ const intervalTicksCount = expectedTickCount(interval, xDomain);
if (intervalTicksCount > maxTickCount) {
continue;
@@ -196,47 +261,91 @@ function timeseriesTicksInterval(
}
}
- return TIMESERIES_INTERVALS[intervalIndex];
+ return TICKS_TIMESERIES_INTERVALS[intervalIndex];
+}
+
+export function getFormatter(
+ formatter: TimeSeriesAxisFormatter,
+ dataUnit: CartesianChartDateTimeAbsoluteUnit,
+ chartUnit: CartesianChartDateTimeAbsoluteUnit,
+) {
+ // If the data interval is week but due to available space and the range of the chart
+ // we decide to show monthly, yearly or even larger ticks, we should format ticks values as months.
+ if (dataUnit === "week" && chartUnit !== "week") {
+ return (value: RowValue) => formatter(value, "month");
+ }
+ return formatter;
}
-/// return the maximum number of ticks to show for a timeseries chart of a given width
function maxTicksForChartWidth(
- chartWidth: number,
- tickFormat: (value: RowValue) => string,
+ chartLayout: ChartLayout,
+ unit: CartesianChartDateTimeAbsoluteUnit,
+ formatter: TimeSeriesAxisFormatter,
) {
- const PIXELS_PER_CHARACTER = 5.5;
- // if there isn't enough buffer, the labels are hidden by ECharts
+ const availableWidth =
+ chartLayout.outerWidth -
+ chartLayout.padding.left -
+ chartLayout.padding.right;
const TICK_BUFFER_PIXELS = 10;
-
- // day of week and month names vary in length, but it's slow to check all of them
- // as an approximation we just use a specific date which was long in my locale
- const formattedValue = tickFormat(new Date(2019, 8, 4).toISOString());
- const pixelsPerTick =
- formattedValue.length * PIXELS_PER_CHARACTER + TICK_BUFFER_PIXELS;
- return Math.floor(chartWidth / pixelsPerTick); // round down so we don't end up with too many ticks
+ const representativeDates = getRepresentativeDates(unit).map((date) =>
+ getPaddedAxisLabel(formatter(date)),
+ );
+ const longestDate = representativeDates.reduce((longest, date) => {
+ return date.length > longest.length ? date : longest;
+ });
+ const longestDateWidth =
+ chartLayout.ticksDimensions.getXTickWidth(longestDate);
+ return Math.floor(availableWidth / (longestDateWidth + TICK_BUFFER_PIXELS));
}
-/// return the range, in milliseconds, of the xDomain. ("Range" in this sense refers to the total "width"" of the
-/// chart in milliseconds.)
-function timeRangeMilliseconds(xDomain: ContinuousDomain) {
- const startTime = xDomain[0]; // these are UNIX timestamps in milliseconds
- const endTime = xDomain[1];
- return endTime - startTime;
-}
+/**
+ * Given a unit, returns an array of "representative" dates.
+ * "representative" means that the date with the longest string representation for the unit is contained in the array.
+ */
+function getRepresentativeDates(unit: CartesianChartDateTimeAbsoluteUnit) {
+ // the length of month names varies by locale
+ let months: number[];
+ if (unit === "year") {
+ months = [0];
+ } else if (unit === "quarter") {
+ months = [0, 3, 6, 9];
+ } else {
+ months = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
+ }
-/// return the appropriate entry in TIMESERIES_INTERVALS for a given chart with domain, interval, and width.
-/// The entry is used to calculate how often a tick should be displayed for this chart (e.g. one tick every 5 minutes)
-export function computeTimeseriesTicksInterval(
- xDomain: ContinuousDomain,
- xInterval: TimeSeriesInterval,
- chartWidth: number,
- tickFormat: (value: RowValue) => string,
-) {
- return timeseriesTicksInterval(
- xInterval,
- timeRangeMilliseconds(xDomain),
- maxTicksForChartWidth(chartWidth, tickFormat),
- );
+ let days: number[];
+ if (
+ unit === "year" ||
+ unit === "quarter" ||
+ unit === "month" ||
+ unit === "week"
+ ) {
+ days = [10]; // use a 2 digit day because it's longer
+ } else {
+ days = [10, 11, 12, 13, 14, 15, 16]; // test each possible weekday name
+ }
+
+ let hours: number[];
+ if (
+ unit === "year" ||
+ unit === "quarter" ||
+ unit === "month" ||
+ unit === "week" ||
+ unit === "day"
+ ) {
+ hours = [10]; // use a 2 digit hour because it's longer
+ } else {
+ hours = [10, 20]; // AM/PM length can vary by locale
+ }
+ const out: string[] = [];
+ for (const month of months) {
+ for (const day of days) {
+ for (const hour of hours) {
+ out.push(new Date(2026, month, day, hour).toISOString());
+ }
+ }
+ }
+ return out;
}
export function getLargestInterval(intervals: TimeSeriesInterval[]) {
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.unit.spec.ts b/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.unit.spec.ts
index 1a5d2f27466..799fca1d11c 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.unit.spec.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/utils/timeseries.unit.spec.ts
@@ -7,6 +7,7 @@ dayjs.extend(utc);
dayjs.extend(timezone);
import { getVisualizationTransformed } from "metabase/visualizations";
+import type { ChartLayout } from "metabase/visualizations/echarts/cartesian/layout/types";
import type {
CartesianChartDateTimeAbsoluteUnit,
TimeSeriesInterval,
@@ -14,6 +15,7 @@ import type {
import {
computeTimeseriesDataInterval,
computeTimeseriesTicksInterval,
+ expectedTickCount,
getTimezoneOrOffset,
normalizeDate,
} from "metabase/visualizations/echarts/cartesian/utils/timeseries";
@@ -30,6 +32,29 @@ import {
createMockSingleSeries,
} from "metabase-types/api/mocks";
+function createMockChartMeasurements(
+ outerWidth: number,
+ xTickWidth: number,
+): ChartLayout {
+ return {
+ boundaryWidth: 0,
+ ticksDimensions: {
+ getXTickWidth: () => xTickWidth,
+ yTicksWidthLeft: 0,
+ yTicksWidthRight: 0,
+ xTicksHeight: 0,
+ firstXTickWidth: 0,
+ lastXTickWidth: 0,
+ },
+ padding: { top: 0, bottom: 0, left: 0, right: 0 },
+ bounds: { top: 0, bottom: 0, left: 0, right: 0 },
+ outerHeight: 0,
+ outerWidth,
+ axisEnabledSetting: true,
+ panelGap: 0,
+ };
+}
+
registerVisualizations();
function computeDefinedTimeseriesDataInterval(
@@ -133,14 +158,190 @@ describe("visualization.lib.timeseries", () => {
});
});
+ describe("expectedTickCount", () => {
+ const testCases: {
+ start: string;
+ end: string;
+ unit: CartesianChartDateTimeAbsoluteUnit;
+ count: number;
+ expected: number;
+ }[] = [
+ {
+ start: "2026-01-10",
+ end: "2026-02-20",
+ unit: "month",
+ count: 1,
+ expected: 1, // 2026-02-01
+ },
+ {
+ start: "2026-01-28",
+ end: "2026-03-02",
+ unit: "month",
+ count: 1,
+ expected: 2, // 2026-02-01, 2026-03-01
+ },
+ {
+ start: "2026-01-01",
+ end: "2026-03-01",
+ unit: "month",
+ count: 1,
+ expected: 3, // 2026-01-01, 2026-02-01, 2026-03-01
+ },
+ {
+ start: "2026-01-10",
+ end: "2026-01-20",
+ unit: "month",
+ count: 1,
+ expected: 0, // no month boundary in range
+ },
+ {
+ start: "2022-01-01",
+ end: "2026-01-01",
+ unit: "year",
+ count: 1,
+ expected: 5, // 2022-01-01, 2023-01-01, 2024-01-01, 2025-01-01, 2026-01-01
+ },
+ {
+ start: "2022-04-01",
+ end: "2026-04-01",
+ unit: "year",
+ count: 1,
+ expected: 4, // 2023-01-01, 2024-01-01, 2025-01-01, 2026-01-01
+ },
+ {
+ start: "2020-01-21",
+ end: "2020-04-05",
+ unit: "month",
+ count: 2,
+ expected: 1, // 2020-03-01
+ },
+ {
+ start: "2022-01-01",
+ end: "2026-01-01",
+ unit: "year",
+ count: 2,
+ expected: 3, // 2022-01-01, 2024-01-01, 2026-01-01
+ },
+ {
+ start: "2023-06-01",
+ end: "2027-06-01",
+ unit: "year",
+ count: 2,
+ expected: 2, // 2024-01-01, 2026-01-01
+ },
+ {
+ start: "2025-01-01",
+ end: "2026-01-01",
+ unit: "quarter",
+ count: 1,
+ expected: 5, // 2025Q1, 2025Q2, 2025Q3, 2025Q4, 2026Q1
+ },
+ {
+ start: "2024-12-15",
+ end: "2025-11-15",
+ unit: "quarter",
+ count: 2,
+ expected: 2, // 2025Q1, 2025Q3
+ },
+ {
+ start: "2025-01-05T00:00:00Z", // Sunday (start of week)
+ end: "2025-02-02T00:00:00Z", // Sunday
+ unit: "week",
+ count: 1,
+ expected: 5, // Jan 5, 12, 19, 26, Feb 2
+ },
+ {
+ start: "2025-01-08T00:00:00Z", // Wednesday
+ end: "2025-01-22T00:00:00Z", // Wednesday
+ unit: "week",
+ count: 1,
+ expected: 2, // Jan 12, Jan 19
+ },
+ {
+ start: "2025-03-15T00:00:00Z",
+ end: "2025-03-20T00:00:00Z",
+ unit: "day",
+ count: 1,
+ expected: 6, // Mar 15, 16, 17, 18, 19, 20
+ },
+ {
+ start: "2025-03-15T12:00:00Z",
+ end: "2025-03-20T12:00:00Z",
+ unit: "day",
+ count: 1,
+ expected: 5, // Mar 16, 17, 18, 19, 20
+ },
+ {
+ start: "2025-03-15T00:00:00Z",
+ end: "2025-03-15T12:00:00Z",
+ unit: "hour",
+ count: 3,
+ expected: 5, // 0:00, 3:00, 6:00, 9:00, 12:00
+ },
+ {
+ start: "2025-03-15T01:00:00Z",
+ end: "2025-03-15T10:00:00Z",
+ unit: "hour",
+ count: 3,
+ expected: 3, // 3:00, 6:00, 9:00
+ },
+ {
+ start: "2025-03-15T10:00:00Z",
+ end: "2025-03-15T11:00:00Z",
+ unit: "minute",
+ count: 15,
+ expected: 5, // 10:00, 10:15, 10:30, 10:45, 11:00
+ },
+ {
+ start: "2025-03-15T10:05:00Z",
+ end: "2025-03-15T11:05:00Z",
+ unit: "minute",
+ count: 15,
+ expected: 4, // 10:15, 10:30, 10:45, 11:00
+ },
+ {
+ start: "2025-03-15T10:00:00Z",
+ end: "2025-03-15T10:02:00Z",
+ unit: "second",
+ count: 30,
+ expected: 5, // :00, :30, 1:00, 1:30, 2:00
+ },
+ {
+ start: "2025-03-15T10:00:10Z",
+ end: "2025-03-15T10:02:10Z",
+ unit: "second",
+ count: 30,
+ expected: 4, // :30, 1:00, 1:30, 2:00
+ },
+ {
+ start: "2025-03-15T10:00:00Z",
+ end: "2025-03-15T10:00:01Z",
+ unit: "ms",
+ count: 1,
+ expected: 1001,
+ },
+ ];
+
+ testCases.forEach(({ start, end, unit, count, expected }) => {
+ it(`should return ${expected} for ${start} to ${end} with ${unit} interval and count ${count}`, () => {
+ expect(
+ expectedTickCount({ unit, count }, [
+ new Date(start).getTime(),
+ new Date(end).getTime(),
+ ]),
+ ).toBe(expected);
+ });
+ });
+ });
+
describe("computeTimeseriesTicksInterval", () => {
- // computeTimeseriesTicksInterval just uses tickFormat to measure the character length of the current formatting style
- const fakeTickFormat = (_value: unknown) => "2020-01-01";
+ const mockFormatter = (value: RowValue) => String(value);
+
type TickInput = {
xDomain: ContinuousDomain;
xInterval: TimeSeriesInterval;
- chartWidth: number;
- tickFormat: (value: RowValue) => string;
+ outerWidth: number;
+ xTickWidth: number;
};
type TickExpected = {
expectedUnit: CartesianChartDateTimeAbsoluteUnit;
@@ -155,12 +356,12 @@ describe("visualization.lib.timeseries", () => {
new Date("2021-01-01").getTime(),
],
xInterval: { unit: "month", count: 1 },
- chartWidth: 1920,
- tickFormat: fakeTickFormat,
+ outerWidth: 1920,
+ xTickWidth: 55,
},
{ expectedUnit: "month", expectedCount: 1 },
],
- // it should be bump to quarters on a narrower chart
+ // 2 month unit should work
[
{
xDomain: [
@@ -168,8 +369,21 @@ describe("visualization.lib.timeseries", () => {
new Date("2021-01-01").getTime(),
],
xInterval: { unit: "month", count: 1 },
- chartWidth: 700,
- tickFormat: fakeTickFormat,
+ outerWidth: 700,
+ xTickWidth: 55,
+ },
+ { expectedUnit: "month", expectedCount: 2 },
+ ],
+ // it should be bumped to quarters on a narrower chart
+ [
+ {
+ xDomain: [
+ new Date("2020-01-01").getTime(),
+ new Date("2021-01-01").getTime(),
+ ],
+ xInterval: { unit: "month", count: 1 },
+ outerWidth: 400,
+ xTickWidth: 55,
},
{ expectedUnit: "quarter", expectedCount: 1 },
],
@@ -178,11 +392,11 @@ describe("visualization.lib.timeseries", () => {
{
xDomain: [
new Date("2020-01-01").getTime(),
- new Date("2021-01-01").getTime(),
+ new Date("2022-01-01").getTime(),
],
xInterval: { unit: "month", count: 1 },
- chartWidth: 300,
- tickFormat: fakeTickFormat,
+ outerWidth: 300,
+ xTickWidth: 55,
},
{ expectedUnit: "year", expectedCount: 1 },
],
@@ -192,23 +406,21 @@ describe("visualization.lib.timeseries", () => {
// {
// xDomain: [new Date("2020-01-01"), new Date("2021-01-01")],
// xInterval: { interval: "month", count: 3 },
- // chartWidth: 1920,
- // tickFormat: fakeTickFormat,
+ // outerWidth: 1920,
+ // xTickWidth: 55,
// },
// { expectedUnit: "month", expectedCount: 3 },
// ],
- // Long date formats should update the interval to have fewer ticks
+ // wide tick labels should update the interval to have fewer ticks
[
{
xDomain: [
new Date("2020-01-01").getTime(),
- new Date("2021-01-01").getTime(),
+ new Date("2022-01-01").getTime(),
],
xInterval: { unit: "month", count: 1 },
- chartWidth: 1920,
- tickFormat: () =>
- // thankfully no date format is actually this long
- "The eighth day of July in the year of our Lord two thousand and nineteen",
+ outerWidth: 1920,
+ xTickWidth: 418,
},
{ expectedUnit: "year", expectedCount: 1 },
],
@@ -216,15 +428,15 @@ describe("visualization.lib.timeseries", () => {
TEST_CASES.forEach(
([
- { xDomain, xInterval, chartWidth, tickFormat },
+ { xDomain, xInterval, outerWidth, xTickWidth },
{ expectedUnit, expectedCount },
]) => {
it(`should return ${expectedCount} ${expectedUnit}`, () => {
const { unit, count } = computeTimeseriesTicksInterval(
xDomain,
xInterval,
- chartWidth,
- tickFormat,
+ createMockChartMeasurements(outerWidth, xTickWidth),
+ mockFormatter,
);
expect(unit).toBe(expectedUnit);
expect(count).toBe(expectedCount);
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/waterfall/option/index.ts b/frontend/src/metabase/visualizations/echarts/cartesian/waterfall/option/index.ts
index b6c861266a3..299a8a84e78 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/waterfall/option/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/waterfall/option/index.ts
@@ -252,7 +252,6 @@ export const getWaterfallChartOption = (
series: seriesOption,
...buildAxes(
chartModel,
- chartWidth,
chartLayout,
settings,
hasTimelineEvents,