Skip to content

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

UXW-49 fixing log scale axis value (#67668)

Egor Grushin376ff5d1cf03
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked.png
index bbb83d0a0a9..5be4b56e629 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked_Negative.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked_Negative.png
index 8832cc7857e..ab78c2d8a46 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked_Negative.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Bar_Log_Y_Scale_Stacked_Negative.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 91caad55da9..b5e2b7e5c0c 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_Line_Log_Y_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Log_Y_Scale.png
index d63720e4e4f..6019ef4ece7 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 038e522e7ce..7ffb310c823 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_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 a0a98d2b8d9..b2452162b42 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_Symlog_Decimals.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Symlog_Decimals.png
new file mode 100644
index 00000000000..316481f8712
Binary files /dev/null and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Symlog_Decimals.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Log.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Log.png
index 15f79455cef..8d2b21d9373 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Log.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Log.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Two_Bars_Two_Areas_One_Line_Log.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Two_Bars_Two_Areas_One_Line_Log.png
index 6e60de800f8..208695d1908 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Two_Bars_Two_Areas_One_Line_Log.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Two_Bars_Two_Areas_One_Line_Log.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Log_X_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Log_X_Scale.png
index aeade75ba06..25ca2a730db 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Log_X_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ScatterPlot_Log_X_Scale.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 f7ebf472c97..bcd36a9dc7f 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/frontend/src/metabase/static-viz/components/ComboChart/ComboChart.stories.tsx b/frontend/src/metabase/static-viz/components/ComboChart/ComboChart.stories.tsx
index 13621bf0102..8d370dcc3b1 100644
--- a/frontend/src/metabase/static-viz/components/ComboChart/ComboChart.stories.tsx
+++ b/frontend/src/metabase/static-viz/components/ComboChart/ComboChart.stories.tsx
@@ -1064,3 +1064,11 @@ export const VisualizerTimeseriesDifferentUnits = {
renderingContext,
},
};
+
+export const SymlogDecimals = {
+ render: Template,
+ args: {
+ rawSeries: data.symlogDecimals,
+ renderingContext,
+ },
+};
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.ts
index 841a40e61ac..3fc1d494534 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
@@ -103,6 +103,7 @@ import powYScaleCustomYAxisRange from "./pow-y-scale-custom-y-axis-range.json";
import safariNonIanaTimezoneRepro44128 from "./safari-non-iana-timezone-repro-44128.json";
import seriesOrderSettingsDoNotMatchSeriesCount from "./series-order-settings-do-not-match-series-count.json";
import stackedChartCustomYAxisRange from "./stacked-chart-custom-y-axis-range.json";
+import symlogDecimals from "./symlog-decimals.json";
import ticksNativeWeekWithGapLongRange from "./ticks-native-week-with-gap-long-range.json";
import ticksNativeWeekWithGapShortRange from "./ticks-native-week-with-gap-short-range.json";
import timeSeriesTicksCompactFormattingMixedTimezones from "./time-series-ticks-compact-formatting-mixed-timezones.json";
@@ -248,6 +249,7 @@ export const data = {
combinedWithInvalidSettings,
stackedChartCustomYAxisRange,
seriesOrderSettingsDoNotMatchSeriesCount,
+ symlogDecimals,
trendGoalLinesWithScalingPowScaleCustomRange,
barStackedAllLabelsTimeseriesWithGap45717,
offsetBasedTimezone47835,
diff --git a/frontend/src/metabase/static-viz/components/ComboChart/stories-data/symlog-decimals.json b/frontend/src/metabase/static-viz/components/ComboChart/stories-data/symlog-decimals.json
new file mode 100644
index 00000000000..9dd1e4e3722
--- /dev/null
+++ b/frontend/src/metabase/static-viz/components/ComboChart/stories-data/symlog-decimals.json
@@ -0,0 +1,238 @@
+[
+ {
+ "card": {
+ "cache_invalidated_at": null,
+ "description": null,
+ "archived": false,
+ "view_count": 1,
+ "collection_position": null,
+ "table_id": null,
+ "result_metadata": [
+ {
+ "display_name": "X",
+ "field_ref": [
+ "field",
+ "X",
+ {
+ "base-type": "type/Float"
+ }
+ ],
+ "name": "X",
+ "base_type": "type/Float",
+ "effective_type": "type/Float",
+ "semantic_type": null,
+ "fingerprint": {
+ "global": {
+ "distinct-count": 11,
+ "nil%": 0
+ },
+ "type": {
+ "type/Number": {
+ "min": -100,
+ "q1": -1,
+ "q3": 1,
+ "max": 100,
+ "sd": 50,
+ "avg": 0
+ }
+ }
+ }
+ },
+ {
+ "display_name": "Y",
+ "field_ref": [
+ "field",
+ "Y",
+ {
+ "base-type": "type/Float"
+ }
+ ],
+ "name": "Y",
+ "base_type": "type/Float",
+ "effective_type": "type/Float",
+ "semantic_type": null,
+ "fingerprint": {
+ "global": {
+ "distinct-count": 11,
+ "nil%": 0
+ },
+ "type": {
+ "type/Number": {
+ "min": -100,
+ "q1": -1,
+ "q3": 1,
+ "max": 100,
+ "sd": 50,
+ "avg": 0
+ }
+ }
+ }
+ }
+ ],
+ "initially_published_at": null,
+ "can_write": true,
+ "trashed_from_collection_id": null,
+ "database_id": 1,
+ "enable_embedding": false,
+ "collection_id": null,
+ "query_type": "native",
+ "name": "Symlog Decimals Test",
+ "last_query_start": "2025-01-07T00:00:00.000000Z",
+ "dashboard_count": 0,
+ "last_used_at": "2025-01-07T00:00:00.000000Z",
+ "type": "question",
+ "average_query_time": 50,
+ "creator_id": 1,
+ "moderation_reviews": [],
+ "updated_at": "2025-01-07T00:00:00.000000Z",
+ "made_public_by_id": null,
+ "embedding_params": null,
+ "cache_ttl": null,
+ "dataset_query": {
+ "database": 1,
+ "type": "native",
+ "native": {
+ "template-tags": {},
+ "query": "select -100 as x, -100 as y union all select -10, -10 union all select -1, -1 union all select -0.5, -0.5 union all select -0.1, -0.1 union all select 0, 0 union all select 0.1, 0.1 union all select 0.5, 0.5 union all select 1, 1 union all select 10, 10 union all select 100, 100"
+ }
+ },
+ "id": 1,
+ "parameter_mappings": [],
+ "display": "line",
+ "archived_directly": false,
+ "entity_id": "test_symlog_decimals",
+ "collection_preview": true,
+ "visualization_settings": {
+ "graph.x_axis.scale": "log",
+ "graph.y_axis.scale": "log",
+ "graph.show_values": true,
+ "graph.dimensions": ["X"],
+ "graph.metrics": ["Y"]
+ },
+ "collection": null,
+ "metabase_version": "v0.50.0",
+ "parameters": [],
+ "created_at": "2025-01-07T00:00:00.000000Z",
+ "parameter_usage_count": 0,
+ "public_uuid": null,
+ "can_delete": false
+ },
+ "data": {
+ "rows": [
+ [-100, -100],
+ [-10, -10],
+ [-1, -1],
+ [-0.5, -0.5],
+ [-0.1, -0.1],
+ [0, 0],
+ [0.1, 0.1],
+ [0.5, 0.5],
+ [1, 1],
+ [10, 10],
+ [100, 100]
+ ],
+ "cols": [
+ {
+ "display_name": "X",
+ "source": "native",
+ "field_ref": [
+ "field",
+ "X",
+ {
+ "base-type": "type/Float"
+ }
+ ],
+ "name": "X",
+ "base_type": "type/Float",
+ "effective_type": "type/Float"
+ },
+ {
+ "display_name": "Y",
+ "source": "native",
+ "field_ref": [
+ "field",
+ "Y",
+ {
+ "base-type": "type/Float"
+ }
+ ],
+ "name": "Y",
+ "base_type": "type/Float",
+ "effective_type": "type/Float"
+ }
+ ],
+ "native_form": {
+ "params": null,
+ "query": "select -100 as x, -100 as y union all select -10, -10 union all select -1, -1 union all select -0.5, -0.5 union all select -0.1, -0.1 union all select 0, 0 union all select 0.1, 0.1 union all select 0.5, 0.5 union all select 1, 1 union all select 10, 10 union all select 100, 100"
+ },
+ "format-rows?": true,
+ "results_timezone": "UTC",
+ "requested_timezone": "UTC",
+ "results_metadata": {
+ "columns": [
+ {
+ "display_name": "X",
+ "field_ref": [
+ "field",
+ "X",
+ {
+ "base-type": "type/Float"
+ }
+ ],
+ "name": "X",
+ "base_type": "type/Float",
+ "effective_type": "type/Float",
+ "semantic_type": null,
+ "fingerprint": {
+ "global": {
+ "distinct-count": 11,
+ "nil%": 0
+ },
+ "type": {
+ "type/Number": {
+ "min": -100,
+ "q1": -1,
+ "q3": 1,
+ "max": 100,
+ "sd": 50,
+ "avg": 0
+ }
+ }
+ }
+ },
+ {
+ "display_name": "Y",
+ "field_ref": [
+ "field",
+ "Y",
+ {
+ "base-type": "type/Float"
+ }
+ ],
+ "name": "Y",
+ "base_type": "type/Float",
+ "effective_type": "type/Float",
+ "semantic_type": null,
+ "fingerprint": {
+ "global": {
+ "distinct-count": 11,
+ "nil%": 0
+ },
+ "type": {
+ "type/Number": {
+ "min": -100,
+ "q1": -1,
+ "q3": 1,
+ "max": 100,
+ "sd": 50,
+ "avg": 0
+ }
+ }
+ }
+ }
+ ]
+ },
+ "insights": null
+ }
+ }
+]
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 40301192fe5..47e01fa69c3 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/chart-measurements/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/chart-measurements/index.ts
@@ -73,6 +73,30 @@ const getValuesToMeasure = (min: number, max: number): number[] => {
return [...middleValues, min, max];
};
+/**
+ * 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
+ * positions (giving 3.16, 31.6...). This function generates both to ensure we
+ * measure the widest possible tick label.
+ */
+const getLogScaleTickValues = (
+ transformedMin: number,
+ transformedMax: number,
+ fromEChartsAxisValue: (value: number) => number,
+): number[] => {
+ const tickValues: number[] = [];
+
+ const step = 0.5;
+ const startTick = Math.floor(transformedMin * 2) / 2;
+ const endTick = Math.ceil(transformedMax * 2) / 2;
+
+ for (let tick = startTick; tick <= endTick; tick += step) {
+ tickValues.push(fromEChartsAxisValue(tick));
+ }
+
+ return tickValues;
+};
+
const getYAxisTicksWidth = (
axisModel: YAxisModel,
yAxisScaleTransforms: NumericAxisScaleTransforms,
@@ -100,6 +124,17 @@ const getYAxisTicksWidth = (
? getValuesToMeasure(min, max)
: [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"],
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/model/transforms.ts b/frontend/src/metabase/visualizations/echarts/cartesian/model/transforms.ts
index ceeade02e27..c3789b55a03 100644
--- a/frontend/src/metabase/visualizations/echarts/cartesian/model/transforms.ts
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/model/transforms.ts
@@ -11,9 +11,27 @@ export function signedSquareRoot(value: number) {
return getSign(value) * Math.sqrt(Math.abs(value));
}
-export function signedLog(value: number) {
- return getSign(value) * Math.log10(Math.abs(value));
-}
+/**
+ * Symmetric log (symlog) transformation that handles:
+ * - Negative values (symmetric around zero)
+ * - Values between -1 and 1 (linear region)
+ * - Values with |x| >= 1 (logarithmic region)
+ */
+const signedLog = (value: number) => {
+ const absValue = Math.abs(value);
+ if (absValue < 1) {
+ return value;
+ }
+ return getSign(value) * (1 + Math.log10(absValue));
+};
+
+const inverseSignedLog = (value: number) => {
+ const absValue = Math.abs(value);
+ if (absValue < 1) {
+ return value;
+ }
+ return getSign(value) * Math.pow(10, absValue - 1);
+};
export function getAxisTransforms(
scale: NumericScale | undefined,
@@ -38,15 +56,9 @@ export function getAxisTransforms(
return null;
}
- if (value === 0) {
- return value;
- }
-
return signedLog(value);
},
- fromEChartsAxisValue: (value) => {
- return Math.pow(10, Math.abs(value)) * getSign(value);
- },
+ fromEChartsAxisValue: inverseSignedLog,
};
}
diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/model/transforms.unit.spec.ts b/frontend/src/metabase/visualizations/echarts/cartesian/model/transforms.unit.spec.ts
new file mode 100644
index 00000000000..468a8236b4e
--- /dev/null
+++ b/frontend/src/metabase/visualizations/echarts/cartesian/model/transforms.unit.spec.ts
@@ -0,0 +1,29 @@
+import { getAxisTransforms } from "./transforms";
+
+describe("getAxisTransforms", () => {
+ describe("log scale", () => {
+ const { toEChartsAxisValue, fromEChartsAxisValue } =
+ getAxisTransforms("log");
+
+ it("should be linear for |x| < 1 and logarithmic for |x| >= 1", () => {
+ expect(toEChartsAxisValue(0.1)).toBe(0.1);
+ expect(toEChartsAxisValue(0.5)).toBe(0.5);
+ expect(toEChartsAxisValue(0.9)).toBe(0.9);
+
+ expect(toEChartsAxisValue(1)).toBeCloseTo(1, 5);
+ expect(toEChartsAxisValue(10)).toBeCloseTo(2, 5);
+ expect(toEChartsAxisValue(100)).toBeCloseTo(3, 5);
+ });
+
+ it.each([
+ 0, 1, -1, 10, -10, 100, -100, 0.5, -0.5, 0.01, -0.01, 0.001, -0.001,
+ ])(
+ "should have fromEChartsAxisValue as the inverse of toEChartsAxisValue for %d",
+ (value) => {
+ const encoded = toEChartsAxisValue(value) as number;
+ const decoded = fromEChartsAxisValue(encoded);
+ expect(decoded).toBeCloseTo(value, 5);
+ },
+ );
+ });
+});