Skip to content

Commit 7ad3202

Browse files
committed
fix(echarts): match forecast legend rendering order
1 parent 9a0670e commit 7ad3202

4 files changed

Lines changed: 146 additions & 30 deletions

File tree

‎superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,26 @@ export function getHorizontalLegendItemLayouts(
235235
legendNames.flatMap(name => {
236236
const legendName = String(name);
237237
const matchingSeries = seriesByName.get(legendName);
238-
return matchingSeries
239-
? [
240-
[
241-
legendName,
242-
getHorizontalLegendItemLayout(
243-
matchingSeries,
244-
theme,
245-
iconOverride,
246-
),
247-
],
248-
]
249-
: [];
238+
if (!matchingSeries) {
239+
return [];
240+
}
241+
const layout = getHorizontalLegendItemLayout(
242+
matchingSeries,
243+
theme,
244+
iconOverride,
245+
);
246+
return [
247+
[
248+
legendName,
249+
{
250+
...layout,
251+
itemHeight: Math.max(
252+
layout.itemHeight,
253+
legendName.split('\n').length * theme.fontSizeSM,
254+
),
255+
},
256+
],
257+
];
250258
}),
251259
);
252260
}
@@ -1253,9 +1261,14 @@ export default function transformProps(
12531261
const resolvedLegendNames = usesPrimaryAxisLegend
12541262
? legendData
12551263
: sortedLegendData;
1264+
// Match the exact series ordering ECharts receives. Forecast components
1265+
// share a legend name, and ECharts derives its icon from the first match.
1266+
const renderedSeries = dedupSeries(
1267+
reorderForecastSeries([...series]) as SeriesOption[],
1268+
);
12561269
const horizontalPlainLegendItemLayouts = getHorizontalLegendItemLayouts(
12571270
resolvedLegendNames,
1258-
series,
1271+
renderedSeries,
12591272
theme,
12601273
usesPrimaryAxisLegend ? 'roundRect' : undefined,
12611274
);
@@ -1678,7 +1691,7 @@ export default function transformProps(
16781691
}
16791692
: {}),
16801693
},
1681-
series: dedupSeries(reorderForecastSeries(series) as SeriesOption[]),
1694+
series: renderedSeries,
16821695
toolbox: {
16831696
show: zoomable,
16841697
top: TIMESERIES_CONSTANTS.toolboxTop,

‎superset-frontend/plugins/plugin-chart-echarts/src/utils/series.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,17 @@ function measureLegendTextWidth(text: string, theme: SupersetTheme): number {
113113
return cachedWidth;
114114
}
115115

116-
let width = text.length * theme.fontSizeSM * 0.62;
116+
const lines = text.split('\n');
117+
let width = Math.max(
118+
...lines.map(line => line.length * theme.fontSizeSM * 0.62),
119+
);
117120

118121
if (typeof document !== 'undefined') {
119122
const canvas = document.createElement('canvas');
120123
const context = canvas.getContext('2d');
121124
if (context) {
122125
context.font = `${theme.fontSizeSM}px ${theme.fontFamily}`;
123-
({ width } = context.measureText(text));
126+
width = Math.max(...lines.map(line => context.measureText(line).width));
124127
}
125128
}
126129

‎superset-frontend/plugins/plugin-chart-echarts/test/Timeseries/transformProps.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
} from '../../src/Timeseries/types';
4747
import { StackControlsValue, TIMESERIES_CONSTANTS } from '../../src/constants';
4848
import {
49+
ForecastSeriesEnum,
4950
LegendOrientation,
5051
LegendType,
5152
EchartsTimeseriesChartProps,
@@ -1198,6 +1199,59 @@ test('reserves the complete dense Plain legend at a realistic Explore chart size
11981199
).toBeCloseTo(94.8);
11991200
});
12001201

1202+
test('reserves Forecast legend rows using the final rendered series order', () => {
1203+
const chartHeight = 600;
1204+
const legendNames = Array.from(
1205+
{ length: 40 },
1206+
(_, index) => `F${String(index).padStart(2, '0')}`,
1207+
);
1208+
const forecastValues = Object.fromEntries(
1209+
legendNames.flatMap((name, index) => [
1210+
[name, index + 1],
1211+
[`${name}${ForecastSeriesEnum.ForecastLower}`, index],
1212+
[`${name}${ForecastSeriesEnum.ForecastUpper}`, index + 2],
1213+
[`${name}${ForecastSeriesEnum.ForecastTrend}`, index + 1.5],
1214+
]),
1215+
);
1216+
const chartProps = createTestChartProps({
1217+
width: 140,
1218+
height: chartHeight,
1219+
formData: {
1220+
...formData,
1221+
forecastEnabled: true,
1222+
sortSeriesAscending: true,
1223+
legendType: LegendType.Plain,
1224+
legendOrientation: LegendOrientation.Top,
1225+
showLegend: true,
1226+
yAxisTitleMargin: 0,
1227+
yAxisTitlePosition: 'Left',
1228+
},
1229+
queriesData: [
1230+
createTestQueryData(
1231+
createTestData([forecastValues], { intervalMs: 300000000 }),
1232+
),
1233+
],
1234+
});
1235+
1236+
const transformed = transformProps(chartProps);
1237+
const renderedSeries = transformed.echartOptions.series as SeriesOption[];
1238+
const grid = transformed.echartOptions.grid as GridComponentOption;
1239+
const firstRenderedForLegend = renderedSeries.find(
1240+
seriesOption => seriesOption.name === legendNames[0],
1241+
);
1242+
1243+
expect(firstRenderedForLegend?.id).toBe(
1244+
`${legendNames[0]}${ForecastSeriesEnum.ForecastLower}`,
1245+
);
1246+
expect(firstRenderedForLegend?.type).toBe('line');
1247+
// All 40 line icons render on separate rows: the 846.8px legend margin is
1248+
// followed by the fixed 20px top grid offset.
1249+
expect(grid.top).toBeCloseTo(866.8);
1250+
expect(grid.bottom).toBe(20);
1251+
expect(transformed.height).toBe(chartHeight);
1252+
expect(transformed.contentHeight).toBeCloseTo(966.8);
1253+
});
1254+
12011255
test('grows the content instead of using a negative offset for a pathological Plain legend', () => {
12021256
const chartHeight = 200;
12031257
const pathologicalLegendValues = Object.fromEntries(

‎superset-frontend/plugins/plugin-chart-echarts/test/utils/series.test.ts‎

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ import type {
5656
} from '../../src/utils/series';
5757
import {
5858
EchartsTimeseriesSeriesType,
59+
ForecastSeriesEnum,
5960
LegendOrientation,
6061
LegendType,
6162
} from '../../src/types';
6263
import { defaultLegendPadding } from '../../src/defaults';
6364
import { NULL_STRING, StackControlsValue } from '../../src/constants';
6465
import { getHorizontalLegendItemLayouts as getTimeseriesHorizontalLegendItemLayouts } from '../../src/Timeseries/transformProps';
66+
import { reorderForecastSeries } from '../../src/utils/forecast';
6567

6668
const {
6769
getHorizontalLegendAvailableWidth,
@@ -1682,30 +1684,74 @@ test('getLegendLayoutResult matches Scatter legend item width', () => {
16821684
});
16831685
});
16841686

1685-
test('getLegendLayoutResult matches Forecast observation legend item width', () => {
1687+
test('getLegendLayoutResult matches the post-reorder Forecast legend series', () => {
16861688
const legendItems = Array.from(
16871689
{ length: 40 },
16881690
(_, index) => `F${String(index).padStart(2, '0')}`,
16891691
);
1692+
const forecastSeries: SeriesOption[] = legendItems.flatMap((name, index) => [
1693+
{
1694+
data: [[index, index + 1]],
1695+
id: name,
1696+
name,
1697+
type: 'scatter' as const,
1698+
},
1699+
...[
1700+
ForecastSeriesEnum.ForecastLower,
1701+
ForecastSeriesEnum.ForecastUpper,
1702+
ForecastSeriesEnum.ForecastTrend,
1703+
].map(forecastType => ({
1704+
data: [[index, index + 2]],
1705+
id: `${name}${forecastType}`,
1706+
name,
1707+
symbol: 'emptyCircle' as const,
1708+
type: 'line' as const,
1709+
})),
1710+
]);
1711+
expect(forecastSeries[0]?.type).toBe('scatter');
16901712
expectTimeseriesLegendLayoutMatchesEcharts({
16911713
chartWidth: 109,
16921714
expectedContentHeight: 432,
16931715
expectedMargin: 438,
16941716
expectedRows: 20,
16951717
legendItems,
1696-
series: legendItems.flatMap((name, index) => [
1697-
{
1698-
data: [[index, index + 1]],
1699-
name,
1700-
type: 'scatter' as const,
1701-
},
1702-
{
1703-
data: [[index, index + 2]],
1704-
name,
1705-
symbol: 'emptyCircle',
1706-
type: 'line' as const,
1707-
},
1708-
]),
1718+
series: forecastSeries,
1719+
});
1720+
1721+
const renderedSeries = reorderForecastSeries(
1722+
forecastSeries,
1723+
) as unknown as SeriesOption[];
1724+
1725+
expect(renderedSeries[0]?.id).toBe(
1726+
`${legendItems[0]}${ForecastSeriesEnum.ForecastLower}`,
1727+
);
1728+
expectTimeseriesLegendLayoutMatchesEcharts({
1729+
chartWidth: 109,
1730+
expectedContentHeight: 840,
1731+
expectedMargin: 846.8,
1732+
expectedRows: 40,
1733+
legendItems,
1734+
series: renderedSeries,
1735+
});
1736+
});
1737+
1738+
test('getLegendLayoutResult matches multiline legend label height', () => {
1739+
const legendItems = Array.from(
1740+
{ length: 40 },
1741+
(_, index) => `P${index}\nLong Publisher ${index}`,
1742+
);
1743+
expectTimeseriesLegendLayoutMatchesEcharts({
1744+
chartWidth: 220,
1745+
expectedContentHeight: 1272,
1746+
expectedMargin: 1268,
1747+
expectedRows: 40,
1748+
legendItems,
1749+
series: legendItems.map((name, index) => ({
1750+
data: [index, index + 1],
1751+
name,
1752+
symbol: 'emptyCircle',
1753+
type: 'line',
1754+
})),
17091755
});
17101756
});
17111757

0 commit comments

Comments
 (0)