This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 7.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit 3ccf8f4cf07b4f9d09296094aa362a5c5b251193 Author: raams95 <[email protected]> AuthorDate: Mon Nov 16 11:49:16 2026 -0600 fix(echarts): show Mixed chart stack totals on each query's top series (#45257) Co-authored-by: JanakiRamChittineni <[email protected]> Co-authored-by: Evan Rusackas <[email protected]> (cherry picked from commit 7a92154b4cd2f1679721fa4feed59397a502882f) --- .../src/MixedTimeseries/controlPanel.tsx | 4 +- .../src/MixedTimeseries/transformProps.ts | 23 ++++--- .../plugin-chart-echarts/src/utils/series.ts | 20 +++++- .../test/MixedTimeseries/controlPanel.test.ts | 29 ++++++++ .../test/MixedTimeseries/transformProps.test.ts | 80 ++++++++++++++++++++++ .../plugin-chart-echarts/test/utils/series.test.ts | 26 +++++++ 6 files changed, 171 insertions(+), 11 deletions(-) diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/controlPanel.tsx b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/controlPanel.tsx index df342df469b..bf2c7a3feaa 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/controlPanel.tsx +++ b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/controlPanel.tsx @@ -240,8 +240,8 @@ function createCustomizeSection( 'Only show the total value on the stacked chart, and not show on the selected category', ), visibility: ({ controls }: ControlPanelsContainerProps) => - Boolean(controls?.show_value?.value) && - Boolean(controls?.stack?.value), + Boolean(controls?.[`show_value${controlSuffix}`]?.value) && + Boolean(controls?.[`stack${controlSuffix}`]?.value), }, }, ], diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts index 1e62754e157..5763bd7a93a 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts @@ -433,14 +433,6 @@ export default function transformProps( primarySeries.add(seriesOption.id as string); } }; - const showValueIndexesA = extractShowValueIndexes(rawSeriesA, { - stack, - onlyTotal, - }); - const showValueIndexesB = extractShowValueIndexes(rawSeriesB, { - stack, - onlyTotal, - }); annotationLayers .filter((layer: AnnotationLayer) => layer.show) @@ -518,6 +510,15 @@ export default function transformProps( const displayLabelMap: Record<string, string[]> = {}; const displayLabelMapB: Record<string, string[]> = {}; + // Value labels are matched against the ECharts series index, which also + // counts the annotation series and, for Query B, the Query A series + // rendered before them. + const showValueIndexesA = extractShowValueIndexes(rawSeriesA, { + stack, + onlyTotal, + seriesIndexOffset: series.length, + }); + rawSeriesA.forEach(entry => { const entryName = String(entry.name || ''); const seriesName = inverted[entryName] || entryName; @@ -624,6 +625,12 @@ export default function transformProps( } }); + const showValueIndexesB = extractShowValueIndexes(rawSeriesB, { + stack: stackB, + onlyTotal: onlyTotalB, + seriesIndexOffset: series.length, + }); + rawSeriesB.forEach(entry => { const entryName = String(entry.name || ''); const seriesEntry = inverted[entryName] || entryName; diff --git a/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts b/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts index aaa351c594e..2014279cba4 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts @@ -506,6 +506,8 @@ const DEFAULT_STACK_GROUP = '__default__'; * @param opts.legendState Active legend state (hidden series are skipped). * @param opts.seriesStackIds Optional per-series stack-group key (parallel to * `series`). Defaults to a single shared group. + * @param opts.seriesIndexOffset Rendered index of `series[0]`, for charts + * that render other series before these ones. * @returns A `Record<stackGroupKey, number[]>` where each inner array maps * `dataIndex → seriesIndex` of the topmost series in that group. */ @@ -517,10 +519,18 @@ export function extractShowValueIndexes( isHorizontal?: boolean; legendState?: LegendState; seriesStackIds?: string[]; + seriesIndexOffset?: number; }, ): Record<string, number[]> { const result: Record<string, number[]> = Object.create(null); - const { legendState, stack, isHorizontal, onlyTotal, seriesStackIds } = opts; + const { + legendState, + stack, + isHorizontal, + onlyTotal, + seriesStackIds, + seriesIndexOffset = 0, + } = opts; if (!stack) { return result; } @@ -552,6 +562,14 @@ export function extractShowValueIndexes( }); }); + if (seriesIndexOffset) { + Object.values(result).forEach(showValueIndexes => { + showValueIndexes.forEach((seriesIndex, dataIndex) => { + showValueIndexes[dataIndex] = seriesIndex + seriesIndexOffset; + }); + }); + } + return result; } diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/controlPanel.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/controlPanel.test.ts index 7be498a0735..e75115753cf 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/controlPanel.test.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/controlPanel.test.ts @@ -107,3 +107,32 @@ test('should have correct visibility for label_positionB', () => { } as unknown as ControlPanelsContainerProps), ).toBe(false); }); + +test('should show only_totalB based on the Query B value and stack controls', () => { + const onlyTotalBCtrl = getControl( + 'only_totalB', + ) as unknown as VisibilityControl; + expect(onlyTotalBCtrl).toBeDefined(); + + expect( + onlyTotalBCtrl.config.visibility({ + controls: { + show_value: { value: false }, + stack: { value: false }, + show_valueB: { value: true }, + stackB: { value: true }, + }, + } as unknown as ControlPanelsContainerProps), + ).toBe(true); + + expect( + onlyTotalBCtrl.config.visibility({ + controls: { + show_value: { value: true }, + stack: { value: true }, + show_valueB: { value: false }, + stackB: { value: false }, + }, + } as unknown as ControlPanelsContainerProps), + ).toBe(false); +}); diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts index 99ce532afac..1ba931e9d24 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/test/MixedTimeseries/transformProps.test.ts @@ -547,6 +547,86 @@ test('threads labelPosition and labelPositionB to series A and B', () => { expect(seriesB?.label?.position).toBe(LabelPositionEnum.Bottom); }); +function formatFirstValueLabels(series: SeriesOption[]) { + const labels: Record<string, string | number | undefined> = {}; + // ECharts passes each series' position in the rendered series array. + series.forEach((option, seriesIndex) => { + const formatter = (option as SeriesWithLabelFormatter).label?.formatter; + if (formatter) { + labels[String(option.name)] = formatter({ + dataIndex: 0, + seriesIndex, + seriesName: String(option.name), + value: (option.data as [number, number][])[0], + }); + } + }); + return labels; +} + +test('shows the Query B stack total on its topmost series', () => { + const chartProps = createEchartsTimeseriesTestChartProps< + EchartsMixedTimeseriesFormData, + EchartsMixedTimeseriesProps + >({ + ...MIXED_TIMESERIES_CHART_PROPS_DEFAULTS, + defaultQueriesData: queriesData, + formData: { + ...formData, + showQueryIdentifiers: true, + stack: false, + showValueB: true, + onlyTotalB: true, + }, + queriesData, + }); + + const { echartOptions } = transformProps(chartProps); + + expect( + formatFirstValueLabels(echartOptions.series as SeriesOption[]), + ).toMatchObject({ + 'sum__num (Query B), girl': '', + 'sum__num (Query B), boy': '3', + }); +}); + +test('keeps the Query A stack total on its topmost series when annotations are shown', () => { + const formula: FormulaAnnotationLayer = { + name: 'My Formula', + annotationType: AnnotationType.Formula, + value: 'x*2', + style: AnnotationStyle.Solid, + show: true, + showLabel: false, + }; + const chartProps = createEchartsTimeseriesTestChartProps< + EchartsMixedTimeseriesFormData, + EchartsMixedTimeseriesProps + >({ + ...MIXED_TIMESERIES_CHART_PROPS_DEFAULTS, + defaultQueriesData: queriesData, + formData: { + ...formData, + annotationLayers: [formula], + showQueryIdentifiers: true, + seriesType: EchartsTimeseriesSeriesType.Bar, + showValue: true, + onlyTotal: true, + }, + queriesData, + }); + + const { echartOptions } = transformProps(chartProps); + + expect( + formatFirstValueLabels(echartOptions.series as SeriesOption[]), + ).toMatchObject({ + 'sum__num (Query A), girl': '', + 'sum__num (Query A), boy': '3', + }); +}); + describe('legend sorting', () => { const getChartProps = (overrides = {}) => createEchartsTimeseriesTestChartProps< diff --git a/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts b/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts index b2fd06c6038..593b8821738 100644 --- a/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts +++ b/superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts @@ -1024,6 +1024,32 @@ describe('extractShowValueIndexes', () => { }); }); + test('should shift the indexes by the series rendered before them', () => { + expect( + extractShowValueIndexes( + [ + { + id: 'abc', + name: 'abc', + data: [ + ['Jan', 10], + ['Feb', null], + ], + }, + { + id: 'def', + name: 'def', + data: [ + ['Jan', 20], + ['Feb', null], + ], + }, + ], + { stack: true, onlyTotal: true, seriesIndexOffset: 3 }, + ), + ).toEqual({ __default__: [4, undefined] }); + }); + test('should safely handle stack groups with prototype property names like __proto__ or constructor', () => { const result = extractShowValueIndexes( [
