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
Update auto 'Show Dots' logic (#80071)
Nick Fitzpatrick97ecbeab61ea
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Linear_X_Axis.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Linear_X_Axis.pngindex 57849145e7a..8915494507f 100644Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Linear_X_Axis.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Area_Stack_Linear_X_Axis.png differdiff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Combo_Split_Panels_Mixed_Series_Display_Settings.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Combo_Split_Panels_Mixed_Series_Display_Settings.pngindex 884617cd9e6..38dbba951dd 100644Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Combo_Split_Panels_Mixed_Series_Display_Settings.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Combo_Split_Panels_Mixed_Series_Display_Settings.png differdiff --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.pngindex b97cfab6b14..07a1ed90e56 100644Binary 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 differdiff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Pow_Y_Scale.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Pow_Y_Scale.pngindex 0ecd4528b50..e77bd4c15b0 100644Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Pow_Y_Scale.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Goal_Line_Pow_Y_Scale.png differdiff --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.pngindex fdc50e01f53..d0dba4bf748 100644Binary 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 differdiff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Settings.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Settings.pngindex c7d7266e24d..9b15653bccd 100644Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Settings.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Line_Settings.png differdiff --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.pngindex b797aec5bbe..40c4e0a9a4e 100644Binary 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 differdiff --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.pngindex 4c16e455839..175f89b61ca 100644Binary 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 differdiff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Pow_Y_Scale_Custom_Y_Axis_Range.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Pow_Y_Scale_Custom_Y_Axis_Range.pngindex d5c1db3cdea..5e6c15aae6b 100644Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Pow_Y_Scale_Custom_Y_Axis_Range.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Pow_Y_Scale_Custom_Y_Axis_Range.png differdiff --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.pngindex e78d6ee6ef3..8ea52e67a88 100644Binary 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 differdiff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Power.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Power.pngindex f7f68b4876f..6c785581ea2 100644Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Power.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_ComboChart_Trend_Combo_Power.png differdiff --git a/frontend/src/metabase/visualizations/echarts/cartesian/model/series.ts b/frontend/src/metabase/visualizations/echarts/cartesian/model/series.tsindex 2b30b5e5a30..3e320f4c6c3 100644--- a/frontend/src/metabase/visualizations/echarts/cartesian/model/series.ts+++ b/frontend/src/metabase/visualizations/echarts/cartesian/model/series.ts@@ -492,7 +492,12 @@ export function getComboChartDataDensity(return seriesSettings?.show_series_values;});- let totalNumberOfDots = 0;+ const numberOfDotsBySeriesKey: Record<DataKey, number> = Object.fromEntries(+ seriesWithSymbols.map(({ dataKey }) => [+ dataKey,+ dataset.filter((datum) => datum[dataKey] != null).length,+ ]),+ );let totalNumberOfSeriesLabels = 0;let totalNumberOfStackedLabels = 0;@@ -504,73 +509,68 @@ export function getComboChartDataDensity(size: CHART_STYLE.seriesLabels.size,};- dataset.forEach((datum) => {- totalNumberOfDots += seriesWithSymbols.filter(- (seriesModel) => datum[seriesModel.dataKey] != null,- ).length;-- // if we will not be displaying any labels, we do not have to calculate the- // label statistics- if (- !settings["graph.show_values"] ||- settings["graph.label_value_frequency"] === "all"- ) {- return;- }-- // series labels count + label width sum- seriesWithLabels.forEach((seriesModel) => {- const value = datum[seriesModel.dataKey];+ // if we will not be displaying any labels, we do not have to calculate the+ // label statistics+ const shouldCalculateLabelStats =+ settings["graph.show_values"] &&+ settings["graph.label_value_frequency"] !== "all";- if (value != null) {- totalNumberOfSeriesLabels += 1;+ if (shouldCalculateLabelStats) {+ dataset.forEach((datum) => {+ // series labels count + label width sum+ seriesWithLabels.forEach((seriesModel) => {+ const value = datum[seriesModel.dataKey];- const formatter = seriesLabelsFormatters[seriesModel.dataKey];- sumOfSeriesLabelWidths += formatter- ? renderingContext.measureText(formatter(value), fontStyle)- : 0;- }- });+ if (value != null) {+ totalNumberOfSeriesLabels += 1;- // stacked labels count + stacked label width sum- if (settings["stackable.stack_type"] !== "normalized") {- stackModels.forEach((stackModel) => {- const formatter = stackedLabelsFormatters[stackModel.display];-- const positiveStackTotal = getStackTotalValue(- datum,- stackModel.seriesKeys,- POSITIVE_STACK_TOTAL_DATA_KEY,- );- const negativeStackTotal = getStackTotalValue(- datum,- stackModel.seriesKeys,- NEGATIVE_STACK_TOTAL_DATA_KEY,- );-- if (positiveStackTotal !== null) {- totalNumberOfStackedLabels += 1;-- sumOfStackedSeriesLabelWidths += formatter- ? renderingContext.measureText(- formatter(positiveStackTotal),- fontStyle,- )- : 0;- }- if (negativeStackTotal !== null) {- totalNumberOfStackedLabels += 1;-- sumOfStackedSeriesLabelWidths += formatter- ? renderingContext.measureText(- formatter(negativeStackTotal),- fontStyle,- )+ const formatter = seriesLabelsFormatters[seriesModel.dataKey];+ sumOfSeriesLabelWidths += formatter+ ? renderingContext.measureText(formatter(value), fontStyle): 0;}});- }- });++ // stacked labels count + stacked label width sum+ if (settings["stackable.stack_type"] !== "normalized") {+ stackModels.forEach((stackModel) => {+ const formatter = stackedLabelsFormatters[stackModel.display];++ const positiveStackTotal = getStackTotalValue(+ datum,+ stackModel.seriesKeys,+ POSITIVE_STACK_TOTAL_DATA_KEY,+ );+ const negativeStackTotal = getStackTotalValue(+ datum,+ stackModel.seriesKeys,+ NEGATIVE_STACK_TOTAL_DATA_KEY,+ );++ if (positiveStackTotal !== null) {+ totalNumberOfStackedLabels += 1;++ sumOfStackedSeriesLabelWidths += formatter+ ? renderingContext.measureText(+ formatter(positiveStackTotal),+ fontStyle,+ )+ : 0;+ }+ if (negativeStackTotal !== null) {+ totalNumberOfStackedLabels += 1;++ sumOfStackedSeriesLabelWidths += formatter+ ? renderingContext.measureText(+ formatter(negativeStackTotal),+ fontStyle,+ )+ : 0;+ }+ });+ }+ });+ }const sumOfLabelWidths =sumOfSeriesLabelWidths + sumOfStackedSeriesLabelWidths;@@ -594,7 +594,7 @@ export function getComboChartDataDensity(type,seriesDataKeysWithLabels,stackedDisplayWithLabels,- totalNumberOfDots,+ numberOfDotsBySeriesKey,averageLabelWidth,totalNumberOfLabels,};diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/model/types.ts b/frontend/src/metabase/visualizations/echarts/cartesian/model/types.tsindex 0c9188257d4..ab402829eaf 100644--- a/frontend/src/metabase/visualizations/echarts/cartesian/model/types.ts+++ b/frontend/src/metabase/visualizations/echarts/cartesian/model/types.ts@@ -215,7 +215,7 @@ export type ComboChartDataDensity = BaseChartDataDensity & {type: "combo";seriesDataKeysWithLabels: DataKey[];stackedDisplayWithLabels: StackDisplay[];- totalNumberOfDots: number;+ numberOfDotsBySeriesKey: Record<DataKey, number>;};export type BaseCartesianChartModel = {diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/series.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/series.tsindex 48547b74c65..04ae989ad44 100644--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/series.ts+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/series.ts@@ -732,6 +732,7 @@ const buildEChartsLineAreaSeries = (chartDataDensity,chartWidth,seriesSettings,+ seriesModel.dataKey,);const blurOpacity = hasMultipleSeries ? CHART_STYLE.opacity.blur : 1;@@ -812,12 +813,13 @@ const buildEChartsLineAreaSeries = (};};-function getShowSymbol(+export function getShowSymbol(chartDataDensity: ComboChartDataDensity,chartWidth: number,seriesSettings: SeriesSettings,+ seriesDataKey: DataKey,): boolean {- const { totalNumberOfDots } = chartDataDensity;+ const { numberOfDotsBySeriesKey } = chartDataDensity;const maxNumberOfDots = chartWidth / (2 * CHART_STYLE.symbolSize);if (chartWidth <= 0) {@@ -832,7 +834,14 @@ function getShowSymbol(return true;}- return totalNumberOfDots <= maxNumberOfDots;+ const hasSinglePoint = numberOfDotsBySeriesKey[seriesDataKey] === 1;+ const drawsNullsAsZeros = seriesSettings["line.missing"] === "zero";++ if (hasSinglePoint && !drawsNullsAsZeros) {+ return true;+ }++ return Math.max(...Object.values(numberOfDotsBySeriesKey)) <= maxNumberOfDots;}function getStackedDataLabelFormatter(diff --git a/frontend/src/metabase/visualizations/echarts/cartesian/option/series.unit.spec.ts b/frontend/src/metabase/visualizations/echarts/cartesian/option/series.unit.spec.tsindex 8ad77d9d189..a6d15c7bb05 100644--- a/frontend/src/metabase/visualizations/echarts/cartesian/option/series.unit.spec.ts+++ b/frontend/src/metabase/visualizations/echarts/cartesian/option/series.unit.spec.ts@@ -1,14 +1,17 @@import { X_AXIS_DATA_KEY } from "metabase/visualizations/echarts/cartesian/constants/dataset";-import type { Datum } from "metabase/visualizations/echarts/cartesian/model/types";import type { ComputedVisualizationSettings } from "metabase/visualizations/types";+import type { SeriesSettings } from "metabase-types/api";import {createMockColumn,createMockVisualizationSettings,} from "metabase-types/api/mocks";+import type { ComboChartDataDensity, DataKey, Datum } from "../model/types";+import {formatStackTotalLabel,formatStackValuePercentage,+ getShowSymbol,getStackValuePercentage,} from "./series";@@ -136,3 +139,110 @@ describe("formatStackTotalLabel", () => {},);});++const createDataDensity = (+ numberOfDotsBySeriesKey: Record<DataKey, number>,+): ComboChartDataDensity => ({+ type: "combo",+ seriesDataKeysWithLabels: [],+ stackedDisplayWithLabels: [],+ numberOfDotsBySeriesKey,+ averageLabelWidth: 0,+ totalNumberOfLabels: 0,+});++// With chartWidth 800 and symbolSize 6, Auto mode hides dots once the densest+// series holds more than maxNumberOfDots = 800 / (2 * 6) ≈ 66 points.+const CHART_WIDTH = 800;++const DENSE_SERIES = "dense";+const SPARSE_SERIES = "sparse";+const SINGLE_POINT_SERIES = "single-point";++const AUTO: SeriesSettings = {};+const MARKERS_ON: SeriesSettings = { "line.marker_enabled": true };+const MARKERS_OFF: SeriesSettings = { "line.marker_enabled": false };+const MISSING_AS_ZERO: SeriesSettings = { "line.missing": "zero" };++// The chart reported in metabase#76723+const reportedChartDensity = () =>+ createDataDensity({+ [DENSE_SERIES]: 48,+ [SPARSE_SERIES]: 48,+ [SINGLE_POINT_SERIES]: 1,+ });++const crowdedChartDensity = () =>+ createDataDensity({+ [DENSE_SERIES]: 200,+ [SPARSE_SERIES]: 12,+ [SINGLE_POINT_SERIES]: 1,+ });++describe("getShowSymbol", () => {+ describe("Auto mode (line.marker_enabled unset)", () => {+ it("shows dots when no single series crosses the threshold, even if the chart as a whole does (metabase#76723)", () => {+ expect(+ getShowSymbol(reportedChartDensity(), CHART_WIDTH, AUTO, DENSE_SERIES),+ ).toBe(true);+ });++ it("hides dots for a sparse series when another series in the chart is over the threshold", () => {+ expect(+ getShowSymbol(crowdedChartDensity(), CHART_WIDTH, AUTO, SPARSE_SERIES),+ ).toBe(false);+ });++ it("shows the dot for a single-point series even when another series is over the threshold", () => {+ expect(+ getShowSymbol(+ crowdedChartDensity(),+ CHART_WIDTH,+ AUTO,+ SINGLE_POINT_SERIES,+ ),+ ).toBe(true);+ });++ it("does not exempt a single-point series whose missing values are drawn as zeros, since it has a line", () => {+ expect(+ getShowSymbol(+ crowdedChartDensity(),+ CHART_WIDTH,+ MISSING_AS_ZERO,+ SINGLE_POINT_SERIES,+ ),+ ).toBe(false);+ });+ });++ describe("precedence", () => {+ it("shows dots when markers are On, overriding the density threshold", () => {+ expect(+ getShowSymbol(+ crowdedChartDensity(),+ CHART_WIDTH,+ MARKERS_ON,+ DENSE_SERIES,+ ),+ ).toBe(true);+ });++ it("hides dots when markers are Off, overriding the single-point exemption", () => {+ expect(+ getShowSymbol(+ crowdedChartDensity(),+ CHART_WIDTH,+ MARKERS_OFF,+ SINGLE_POINT_SERIES,+ ),+ ).toBe(false);+ });++ it("hides dots when the chart has no width, overriding the single-point exemption", () => {+ expect(+ getShowSymbol(crowdedChartDensity(), 0, AUTO, SINGLE_POINT_SERIES),+ ).toBe(false);+ });+ });+});