Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,24 @@ export default function transformProps(
rebaseToPercentChange(forecastRebasedData, xAxisLabel || DTTM_ALIAS)
: forecastRebasedData;
const isHorizontal = orientation === OrientationType.Horizontal;
// rebasedData's keys have already been through rebaseForecastDatum, which
// renames a key to its verboseMap entry when one is configured for that
// metric. extraMetricLabels must be mapped the same way, or a sort-only
// metric with a verbose_name set would silently fail to match here (and in
// extractSeries below, which has the same requirement).
const extraMetricLabels = extractExtraMetrics(chartProps.rawFormData)
.map(getMetricLabel)
.map(label => verboseMap[label] ?? label);
const { totalStackedValues, thresholdValues } = extractDataTotalValues(
rebasedData,
{
stack,
percentageThreshold,
xAxisCol: xAxisLabel,
legendState,
extraMetricLabels,
},
);
const extraMetricLabels = extractExtraMetrics(chartProps.rawFormData).map(
getMetricLabel,
);

const isMultiSeries = groupBy.length || metrics?.length > 1;
const xAxisDataType = dataTypes?.[xAxisLabel] ?? dataTypes?.[xAxisOrig];
Expand Down
12 changes: 10 additions & 2 deletions superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,26 @@ export function extractDataTotalValues(
percentageThreshold: number;
xAxisCol: string;
legendState?: LegendState;
extraMetricLabels?: string[];
},
): {
totalStackedValues: number[];
thresholdValues: number[];
} {
const totalStackedValues: number[] = [];
const thresholdValues: number[] = [];
const { stack, percentageThreshold, xAxisCol, legendState } = opts;
const {
stack,
percentageThreshold,
xAxisCol,
legendState,
extraMetricLabels,
} = opts;
const excludedKeys = new Set([xAxisCol, ...(extraMetricLabels ?? [])]);
if (stack) {
data.forEach(datum => {
const values = Object.keys(datum).reduce((prev, curr) => {
if (curr === xAxisCol) {
if (excludedKeys.has(curr)) {
return prev;
}
if (legendState && !legendState[curr]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,55 @@ describe('Does transformProps transform series correctly', () => {
});
});

test('should exclude a verbose-named sort-only metric from the stacked total (#42881)', () => {
// rebaseForecastDatum renames a data column to its verboseMap entry when
// one is configured, so extraMetricLabels (derived from raw metric
// labels) must be resolved through the same verboseMap to still match —
// otherwise the sort-only metric's value leaks back into the total.
const sortMetricVerboseMap = { sort_metric: 'Sort By Metric' };
const sortFormData: SqlaFormData = {
...formData,
onlyTotal: true,
groupby: [],
metrics: ['San Francisco', 'New York', 'Boston'],
timeseries_limit_metric: 'sort_metric',
x_axis_sort: 'sort_metric',
};
const sortQueriesData: ChartDataResponseResult[] = [
createTestQueryData(
createTestData(
[
{
'San Francisco': 32,
'New York': 0,
Boston: 0,
'Sort By Metric': 2,
},
],
{ intervalMs: 300000000 },
),
),
];
const chartProps = createTestChartProps({
formData: sortFormData,
queriesData: sortQueriesData,
datasource: { verboseMap: sortMetricVerboseMap },
});

const transformedSeries = transformProps(chartProps).echartOptions
.series as seriesType[];

const totalLabels = transformedSeries
.flatMap((series, seriesIndex) =>
series.data.map((value, dataIndex) =>
series.label.formatter({ value, dataIndex, seriesIndex }),
),
)
.filter(label => label !== '');

expect(totalLabels).toEqual(['32']);
});

test('should show labels on values >= percentageThreshold if onlyTotal is false', () => {
const chartProps = createTestChartProps({ formData, queriesData });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { GenericDataType } from '@apache-superset/core/common';
import {
calculateLowerLogTick,
dedupSeries,
extractDataTotalValues,
extractGroupbyLabel,
extractSeries,
extractShowValueIndexes,
Expand Down Expand Up @@ -467,6 +468,40 @@ test('sortAndFilterSeries by name with numbers desc', () => {
]);
});

test('extractDataTotalValues excludes extraMetricLabels from the stacked total (#42701)', () => {
const data: DataRecord[] = [
{ category: '1-3d', A: 32, B: 0, Sort: 2 },
{ category: '4-6d', A: 10, B: 5, Sort: 1 },
];
const withoutExclusion = extractDataTotalValues(data, {
stack: true,
percentageThreshold: 0,
xAxisCol: 'category',
});
// Reproduces the bug: the sort-only metric leaks into the total.
expect(withoutExclusion.totalStackedValues).toEqual([34, 16]);

const withExclusion = extractDataTotalValues(data, {
stack: true,
percentageThreshold: 0,
xAxisCol: 'category',
extraMetricLabels: ['Sort'],
});
expect(withExclusion.totalStackedValues).toEqual([32, 15]);
});

test('extractDataTotalValues still respects legendState alongside extraMetricLabels', () => {
const data: DataRecord[] = [{ category: '1-3d', A: 32, B: 8, Sort: 2 }];
const result = extractDataTotalValues(data, {
stack: true,
percentageThreshold: 0,
xAxisCol: 'category',
extraMetricLabels: ['Sort'],
legendState: { A: true, B: false },
});
expect(result.totalStackedValues).toEqual([32]);
});

describe('extractSeries', () => {
test('should generate a valid ECharts timeseries series object', () => {
const data = [
Expand Down
Loading