Skip to content

Commit f0f7e12

Browse files
committed
fix(echarts): skip hidden compact legend margins
1 parent 6ce5777 commit f0f7e12

5 files changed

Lines changed: 58 additions & 38 deletions

File tree

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,24 +1125,11 @@ export default function transformProps(
11251125
name,
11261126
icon: 'roundRect',
11271127
}));
1128-
const nonLegendPadding = getPadding(
1129-
false,
1130-
legendOrientation,
1131-
addYAxisLabelOffset,
1132-
zoomable,
1133-
undefined,
1134-
addXAxisLabelOffset,
1135-
yAxisTitlePosition,
1136-
convertInteger(yAxisTitleMargin),
1137-
convertInteger(xAxisTitleMargin),
1138-
isHorizontal,
1139-
);
1140-
const nonLegendReservedHeight =
1141-
nonLegendPadding.top +
1142-
(isHorizontal ? nonLegendPadding.left : nonLegendPadding.bottom);
1128+
const isSmallChart = height < TIMESERIES_CONSTANTS.compactChartHeight;
1129+
const isLegendVisible = showLegend && !isSmallChart;
11431130
const getLegendLayout = (candidateLegendMargin?: string | number | null) => {
11441131
const padding = getPadding(
1145-
showLegend,
1132+
isLegendVisible,
11461133
legendOrientation,
11471134
addYAxisLabelOffset,
11481135
zoomable,
@@ -1172,9 +1159,9 @@ export default function transformProps(
11721159
? colorByPrimaryAxisLegendData
11731160
: sortedLegendData,
11741161
legendMargin: candidateLegendMargin,
1175-
nonLegendReservedHeight,
11761162
orientation: legendOrientation,
1177-
show: showLegend,
1163+
reserveFullHorizontalPlainLegendMargin: true,
1164+
show: isLegendVisible,
11781165
showSelectors: !(colorByPrimaryAxis && groupBy.length === 0),
11791166
theme,
11801167
type: legendType,
@@ -1189,7 +1176,7 @@ export default function transformProps(
11891176
: initialLegendLayout;
11901177
const { effectiveLegendType } = legendLayout;
11911178
const hasHorizontalPlainLegend =
1192-
showLegend &&
1179+
isLegendVisible &&
11931180
effectiveLegendType === LegendType.Plain &&
11941181
(legendOrientation === LegendOrientation.Top ||
11951182
legendOrientation === LegendOrientation.Bottom);
@@ -1200,7 +1187,7 @@ export default function transformProps(
12001187
? legendMargin
12011188
: legendLayout.effectiveLegendMargin;
12021189
const padding = getPadding(
1203-
showLegend,
1190+
isLegendVisible,
12041191
legendOrientation,
12051192
addYAxisLabelOffset,
12061193
zoomable,
@@ -1334,7 +1321,6 @@ export default function transformProps(
13341321
// >= 100px: full axis with proportional tick count
13351322
// 60-99px: show only min/max boundary labels (splitNumber=1), hide lines/ticks
13361323
// < 60px: hide all axis decorations, show line only
1337-
const isSmallChart = height < TIMESERIES_CONSTANTS.compactChartHeight;
13381324
const isMicroChart = height < TIMESERIES_CONSTANTS.microChartHeight;
13391325
const yAxisSplitNumber = isMicroChart
13401326
? undefined
@@ -1545,7 +1531,7 @@ export default function transformProps(
15451531
effectiveLegendType,
15461532
legendOrientation,
15471533
// Hide legend on compact charts — not enough vertical space
1548-
isSmallChart ? false : showLegend,
1534+
isLegendVisible,
15491535
theme,
15501536
zoomable,
15511537
legendState,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ export function resolveLegendLayout(args: {
8080
chartWidth: number;
8181
legendItems?: LegendDataItem[];
8282
legendMargin?: string | number | null;
83-
// Only Timeseries opts into full required-margin layout until sibling callers
84-
// can provide their own accurate fixed-height reservations.
85-
nonLegendReservedHeight?: number;
8683
orientation: LegendOrientation;
84+
// Only Timeseries opts into full required-margin layout until sibling callers
85+
// explicitly adopt the same behavior.
86+
reserveFullHorizontalPlainLegendMargin?: boolean;
8787
show: boolean;
8888
showSelectors?: boolean;
8989
theme: SupersetTheme;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ function getHorizontalPlainLegendLayout({
244244
availableWidth,
245245
currentMargin,
246246
legendLabels,
247-
nonLegendReservedHeight,
248247
orientation,
248+
reserveFullHorizontalPlainLegendMargin,
249249
showSelectors,
250250
theme,
251251
}: {
252252
availableHeight: number;
253253
availableWidth: number;
254254
currentMargin: number;
255255
legendLabels: string[];
256-
nonLegendReservedHeight?: number;
257256
orientation: LegendOrientation.Top | LegendOrientation.Bottom;
257+
reserveFullHorizontalPlainLegendMargin?: boolean;
258258
showSelectors: boolean;
259259
theme: SupersetTheme;
260260
}): LegendLayoutResult {
@@ -271,7 +271,7 @@ function getHorizontalPlainLegendLayout({
271271
defaultLegendPadding[orientation] +
272272
Math.max(0, rowsForMargin - 1) * LEGEND_HORIZONTAL_ROW_HEIGHT;
273273
const boundedMargin =
274-
nonLegendReservedHeight === undefined && availableHeight > 0
274+
!reserveFullHorizontalPlainLegendMargin && availableHeight > 0
275275
? Math.min(requiredMargin, availableHeight * MAX_LEGEND_MARGIN_RATIO)
276276
: requiredMargin;
277277

@@ -328,8 +328,8 @@ export function getLegendLayoutResult({
328328
chartWidth,
329329
legendItems = [],
330330
legendMargin,
331-
nonLegendReservedHeight,
332331
orientation,
332+
reserveFullHorizontalPlainLegendMargin,
333333
show,
334334
showSelectors = true,
335335
theme,
@@ -343,10 +343,10 @@ export function getLegendLayoutResult({
343343
chartWidth: number;
344344
legendItems?: LegendDataItem[];
345345
legendMargin?: string | number | null;
346-
// Full required-margin layout is opt-in for Timeseries; sibling callers that
347-
// omit their fixed padding retain the legacy ratio cap.
348-
nonLegendReservedHeight?: number;
349346
orientation: LegendOrientation;
347+
// Full required-margin layout is opt-in for Timeseries; sibling callers that
348+
// leave it false retain the legacy ratio cap.
349+
reserveFullHorizontalPlainLegendMargin?: boolean;
350350
show: boolean;
351351
showSelectors?: boolean;
352352
theme: SupersetTheme;
@@ -370,8 +370,8 @@ export function getLegendLayoutResult({
370370
availableWidth: resolvedAvailableWidth,
371371
currentMargin: resolvedLegendMargin,
372372
legendLabels,
373-
nonLegendReservedHeight,
374373
orientation,
374+
reserveFullHorizontalPlainLegendMargin,
375375
showSelectors,
376376
theme,
377377
});

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,40 @@ test('keeps a minimal grid height when a pathological Plain legend exceeds the c
11991199
expect(chartHeight - (Number(grid.top) + Number(grid.bottom))).toBe(1);
12001200
});
12011201

1202+
test('does not reserve dense Plain legend space when compact charts hide the legend', () => {
1203+
const chartHeight = 80;
1204+
const denseLegendValues = Object.fromEntries(
1205+
Array.from({ length: 40 }, (_, index) => [
1206+
`Country ${index + 1}, Product Line`,
1207+
index + 1,
1208+
]),
1209+
);
1210+
const chartProps = createTestChartProps({
1211+
height: chartHeight,
1212+
formData: {
1213+
...formData,
1214+
legendType: LegendType.Plain,
1215+
legendOrientation: LegendOrientation.Top,
1216+
showLegend: true,
1217+
},
1218+
queriesData: [
1219+
createTestQueryData(
1220+
createTestData([denseLegendValues], { intervalMs: 300000000 }),
1221+
),
1222+
],
1223+
});
1224+
1225+
const transformed = transformProps(chartProps);
1226+
const grid = transformed.echartOptions.grid as GridComponentOption;
1227+
1228+
expect((transformed.echartOptions.legend as { show?: boolean }).show).toBe(
1229+
false,
1230+
);
1231+
expect(grid.top).toBe(12);
1232+
expect(grid.bottom).toBe(5);
1233+
expect(chartHeight - (Number(grid.top) + Number(grid.bottom))).toBe(63);
1234+
});
1235+
12021236
test('honors user-selected plain legend type for top orientation when space allows (#39540)', () => {
12031237
// Regression test for issue #39540: switching the legend type control from
12041238
// scroll to plain must reach the rendered ECharts config. Horizontal legends

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ const {
7676
| { name?: string | number | null }
7777
)[];
7878
legendMargin?: string | number | null;
79-
nonLegendReservedHeight?: number;
8079
orientation: LegendOrientation;
80+
reserveFullHorizontalPlainLegendMargin?: boolean;
8181
show: boolean;
8282
showSelectors?: boolean;
8383
theme: typeof theme;
@@ -104,8 +104,8 @@ const {
104104
| { name?: string | number | null }
105105
)[];
106106
legendMargin?: string | number | null;
107-
nonLegendReservedHeight?: number;
108107
orientation: LegendOrientation;
108+
reserveFullHorizontalPlainLegendMargin?: boolean;
109109
show: boolean;
110110
showSelectors?: boolean;
111111
theme: typeof theme;
@@ -1250,7 +1250,7 @@ test('getLegendLayoutResult keeps plain when horizontal plain legends exceed two
12501250
expect(layout.effectiveMargin).toBe(68);
12511251
});
12521252

1253-
test('getLegendLayoutResult reserves every row when fixed padding is supplied', () => {
1253+
test('getLegendLayoutResult reserves every row when full-margin layout is requested', () => {
12541254
const layout = getLegendLayoutResult({
12551255
chartHeight: 400,
12561256
chartWidth: 800,
@@ -1259,8 +1259,8 @@ test('getLegendLayoutResult reserves every row when fixed padding is supplied',
12591259
(_, index) => `Country ${index + 1}, Product`,
12601260
),
12611261
legendMargin: null,
1262-
nonLegendReservedHeight: 0,
12631262
orientation: LegendOrientation.Top,
1263+
reserveFullHorizontalPlainLegendMargin: true,
12641264
show: true,
12651265
theme,
12661266
type: LegendType.Plain,
@@ -1278,8 +1278,8 @@ test('getLegendLayoutResult reserves the full required margin for opted-in overf
12781278
chartWidth: 100,
12791279
legendItems: Array.from({ length: 100 }, (_, index) => `Series ${index}`),
12801280
legendMargin: null,
1281-
nonLegendReservedHeight: 0,
12821281
orientation: LegendOrientation.Top,
1282+
reserveFullHorizontalPlainLegendMargin: true,
12831283
show: true,
12841284
theme,
12851285
type: LegendType.Plain,

0 commit comments

Comments
 (0)