Skip to content

Commit 9b8da16

Browse files
committed
fix(echarts): honor plain legend row breaks
1 parent 7ad3202 commit 9b8da16

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,14 @@ function estimateEchartsHorizontalLegendAdditionalMargin(
245245
itemXOffset: 0,
246246
};
247247
const itemRects = labels.map(label => {
248+
// Plain legends use these exact values as zero-sized row-break groups.
249+
if (label === '' || label === '\n') {
250+
return { height: 0, newline: true, width: 0, x: 0 };
251+
}
248252
const layout = itemLayouts[label] ?? fallbackLayout;
249253
return {
250254
height: layout.itemHeight,
255+
newline: false,
251256
width: measureLegendTextWidth(label, theme) + layout.itemWidthOffset,
252257
x: layout.itemXOffset,
253258
};
@@ -264,14 +269,17 @@ function estimateEchartsHorizontalLegendAdditionalMargin(
264269

265270
// Match zrender's horizontal box layout, including its handling of an
266271
// oversized item and of differing icon bounding-box origins.
267-
if (nextX > availableWidth) {
272+
if (nextX > availableWidth || rect.newline) {
268273
currentX = 0;
269274
nextX = moveX;
270275
currentY += currentLineMaxHeight + ECHARTS_LEGEND_ITEM_GAP;
271276
currentLineMaxHeight = rect.height;
272277
} else {
273278
currentLineMaxHeight = Math.max(currentLineMaxHeight, rect.height);
274279
}
280+
if (rect.newline) {
281+
return;
282+
}
275283
rowOffsets.push(currentY);
276284
currentX = nextX + ECHARTS_LEGEND_ITEM_GAP;
277285
});

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,26 @@ test('getLegendLayoutResult matches multiline legend label height', () => {
17551755
});
17561756
});
17571757

1758+
test.each(['', '\n'])(
1759+
'getLegendLayoutResult matches the ECharts row break for %p',
1760+
rowBreak => {
1761+
const legendItems = ['A', rowBreak, 'B'];
1762+
expectTimeseriesLegendLayoutMatchesEcharts({
1763+
chartWidth: 600,
1764+
expectedContentHeight: 34.8,
1765+
expectedMargin: 41.2,
1766+
expectedRows: 2,
1767+
legendItems,
1768+
series: ['A', 'B'].map((name, index) => ({
1769+
data: [index, index + 1],
1770+
name,
1771+
symbol: 'emptyCircle',
1772+
type: 'line',
1773+
})),
1774+
});
1775+
},
1776+
);
1777+
17581778
test('getLegendLayoutResult uses Line geometry for shown annotation layers', () => {
17591779
const observationNames = Array.from(
17601780
{ length: 38 },

0 commit comments

Comments
 (0)