Skip to content

Commit 560e5bf

Browse files
committed
fix(echarts): preserve viable plot space with plain legends
1 parent 5f195cf commit 560e5bf

6 files changed

Lines changed: 375 additions & 15 deletions

File tree

‎superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/EchartsTimeseries.test.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,43 @@ test('observes extra control height changes when ResizeObserver is available', a
286286
expect(disconnectSpy).toHaveBeenCalled();
287287
});
288288

289+
test('uses the post-control body height for compact custom-legend visibility', async () => {
290+
mockOffsetHeight = 40;
291+
const { queryByTestId } = render(
292+
<EchartsTimeseries
293+
{...defaultProps}
294+
height={140}
295+
echartOptions={{
296+
grid: { bottom: 80, containLabel: true, top: 20 },
297+
}}
298+
formData={{ ...defaultFormData, zoomable: true }}
299+
customLegend={
300+
{
301+
grid: { bottom: 80, top: 20 },
302+
items: Array.from({ length: 20 }, (_, index) => ({
303+
color: '#123456',
304+
interactive: true,
305+
name: `Series ${index}`,
306+
selected: true,
307+
})),
308+
orientation: LegendOrientation.Top,
309+
showSelectors: true,
310+
} as never
311+
}
312+
/>,
313+
);
314+
315+
await waitFor(() => {
316+
expect(queryByTestId('timeseries-custom-legend')).not.toBeInTheDocument();
317+
expect(getLatestHeight()).toBe(100);
318+
expect(getLatestEchartProps().echartOptions.grid).toEqual({
319+
bottom: 80,
320+
containLabel: false,
321+
top: 12,
322+
});
323+
});
324+
});
325+
289326
test('falls back to window resize listener when ResizeObserver is unavailable', async () => {
290327
(globalThis as { ResizeObserver?: typeof ResizeObserver }).ResizeObserver =
291328
undefined;

‎superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/EchartsTimeseries.tsx‎

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,53 @@ import {
4646
snapToNearestX,
4747
SeriesDataPoint,
4848
} from './percentChange';
49-
import { OrientationType, TimeseriesChartTransformedProps } from './types';
49+
import {
50+
OrientationType,
51+
TimeseriesChartTransformedProps,
52+
TimeseriesCustomLegend,
53+
} from './types';
5054
import { formatSeriesName } from '../utils/series';
5155
import { getTemporalXAxisDrillByFilter } from '../utils/xAxisDrillByFilter';
5256
import { ExtraControls } from '../components/ExtraControls';
5357
import TimeseriesLegend from './TimeseriesLegend';
58+
import { TIMESERIES_CONSTANTS } from '../constants';
5459

5560
const TIMER_DURATION = 300;
5661
const MAX_CUSTOM_LEGEND_HEIGHT = 160;
5762
const MAX_CUSTOM_LEGEND_HEIGHT_RATIO = 0.3;
63+
const MIN_TIMESERIES_PLOT_HEIGHT = 80;
64+
const MIN_ECHARTS_GRID_HEIGHT = 1;
65+
66+
function resolveGridOffset(offset: number | string, chartBodyHeight: number) {
67+
if (typeof offset === 'number') {
68+
return Number.isFinite(offset) ? Math.max(offset, 0) : 0;
69+
}
70+
71+
const percentage = offset.match(/^\s*(-?\d+(?:\.\d+)?)%\s*$/);
72+
const pixels = percentage
73+
? (Number(percentage[1]) / 100) * chartBodyHeight
74+
: Number(offset);
75+
return Number.isFinite(pixels) ? Math.max(pixels, 0) : 0;
76+
}
5877

59-
// Bound the legend to a minority of the allocated chart body so the plot stays
60-
// visible while the legend's own viewport scrolls independently.
61-
export const getTimeseriesLegendMaxHeight = (chartBodyHeight: number) =>
78+
// Bound the legend after accounting for the fixed ECharts grid reservations,
79+
// leaving enough coordinate space for the plot itself to remain usable.
80+
export const getTimeseriesLegendMaxHeight = (
81+
chartBodyHeight: number,
82+
grid: TimeseriesCustomLegend['grid'],
83+
) =>
6284
Math.min(
6385
MAX_CUSTOM_LEGEND_HEIGHT,
6486
Math.floor(Math.max(chartBodyHeight, 0) * MAX_CUSTOM_LEGEND_HEIGHT_RATIO),
87+
Math.max(
88+
Math.floor(
89+
chartBodyHeight -
90+
resolveGridOffset(grid.top, chartBodyHeight) -
91+
resolveGridOffset(grid.bottom, chartBodyHeight) -
92+
MIN_TIMESERIES_PLOT_HEIGHT,
93+
),
94+
0,
95+
),
6596
);
6697
const getTimestampFromTimeAxisValue = (value: string | number) => {
6798
if (typeof value === 'number') {
@@ -796,6 +827,66 @@ export default function EchartsTimeseries({
796827
);
797828

798829
const chartBodyHeight = Math.max(height - extraControlHeight, 0);
830+
const customLegendMaxHeight = customLegend
831+
? getTimeseriesLegendMaxHeight(chartBodyHeight, customLegend.grid)
832+
: 0;
833+
const shouldRenderCustomLegend =
834+
customLegend !== undefined &&
835+
chartBodyHeight > TIMESERIES_CONSTANTS.compactChartHeight &&
836+
customLegendMaxHeight > 0;
837+
const chartEchartOptions = useMemo(() => {
838+
if (!customLegend || shouldRenderCustomLegend) {
839+
return echartOptions;
840+
}
841+
842+
const gridOption = Array.isArray(echartOptions.grid)
843+
? echartOptions.grid[0]
844+
: echartOptions.grid;
845+
if (!gridOption || typeof gridOption !== 'object') {
846+
return echartOptions;
847+
}
848+
849+
const gridTop = resolveGridOffset(customLegend.grid.top, chartBodyHeight);
850+
const gridBottom = resolveGridOffset(
851+
customLegend.grid.bottom,
852+
chartBodyHeight,
853+
);
854+
const isCompact =
855+
chartBodyHeight <= TIMESERIES_CONSTANTS.compactChartHeight;
856+
if (
857+
!isCompact &&
858+
chartBodyHeight - gridTop - gridBottom >= MIN_ECHARTS_GRID_HEIGHT
859+
) {
860+
return echartOptions;
861+
}
862+
863+
const top = isCompact ? Math.min(gridTop, 12) : gridTop;
864+
const requestedBottom =
865+
isCompact && !formData.zoomable ? Math.min(gridBottom, 5) : gridBottom;
866+
const bottom = Math.min(
867+
requestedBottom,
868+
Math.max(chartBodyHeight - top - MIN_ECHARTS_GRID_HEIGHT, 0),
869+
);
870+
const compactGrid = {
871+
...gridOption,
872+
bottom,
873+
containLabel: false,
874+
top,
875+
};
876+
877+
return {
878+
...echartOptions,
879+
grid: Array.isArray(echartOptions.grid)
880+
? [compactGrid, ...echartOptions.grid.slice(1)]
881+
: compactGrid,
882+
};
883+
}, [
884+
chartBodyHeight,
885+
customLegend,
886+
echartOptions,
887+
formData.zoomable,
888+
shouldRenderCustomLegend,
889+
]);
799890
const renderEchart = ({
800891
chartHeight,
801892
chartWidth,
@@ -808,7 +899,7 @@ export default function EchartsTimeseries({
808899
refs={refs}
809900
height={chartHeight}
810901
width={chartWidth}
811-
echartOptions={echartOptions}
902+
echartOptions={chartEchartOptions}
812903
eventHandlers={eventHandlers}
813904
queryEventHandlers={queryEventHandlers}
814905
zrEventHandlers={zrEventHandlers}
@@ -822,15 +913,15 @@ export default function EchartsTimeseries({
822913
<div ref={extraControlRef}>
823914
<ExtraControls formData={formData} setControlValue={setControlValue} />
824915
</div>
825-
{customLegend ? (
916+
{customLegend && shouldRenderCustomLegend ? (
826917
<WithLegend
827918
height={chartBodyHeight}
828919
position={customLegend.orientation}
829920
width={width}
830921
renderLegend={() => (
831922
<TimeseriesLegend
832923
{...customLegend}
833-
maxHeight={getTimeseriesLegendMaxHeight(chartBodyHeight)}
924+
maxHeight={customLegendMaxHeight}
834925
onAll={() => dispatchLegendAction({ type: 'legendAllSelect' })}
835926
onInverse={() =>
836927
dispatchLegendAction({ type: 'legendInverseSelect' })

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,15 @@ function getLegendSeriesColor(
150150

151151
function buildTimeseriesCustomLegend({
152152
fallbackColor,
153+
grid,
153154
interactive,
154155
legendNames,
155156
legendState,
156157
orientation,
157158
series,
158159
}: {
159160
fallbackColor: string;
161+
grid: TimeseriesCustomLegend['grid'];
160162
interactive: boolean;
161163
legendNames: string[];
162164
legendState?: LegendState;
@@ -195,6 +197,7 @@ function buildTimeseriesCustomLegend({
195197
});
196198

197199
return {
200+
grid,
198201
items,
199202
orientation,
200203
showSelectors: interactive,
@@ -1197,7 +1200,8 @@ export default function transformProps(
11971200
icon: 'roundRect',
11981201
}));
11991202
const isSmallChart = height < TIMESERIES_CONSTANTS.compactChartHeight;
1200-
const isLegendVisible = showLegend && !isSmallChart;
1203+
const usesCompactLayout = height <= TIMESERIES_CONSTANTS.compactChartHeight;
1204+
const isLegendVisible = showLegend && !usesCompactLayout;
12011205
const usesPrimaryAxisLegend = colorByPrimaryAxis && groupBy.length === 0;
12021206
const resolvedLegendData = usesPrimaryAxisLegend
12031207
? colorByPrimaryAxisLegendData
@@ -1283,7 +1287,7 @@ export default function transformProps(
12831287
// Reduce grid padding for small charts to maximize the drawing area.
12841288
// Keep enough top padding so the max label doesn't clip against the cell border.
12851289
// Preserve bottom padding when zoomable, since getPadding() reserves space for the dataZoom slider.
1286-
if (height < TIMESERIES_CONSTANTS.compactChartHeight) {
1290+
if (usesCompactLayout) {
12871291
padding.top = Math.min(padding.top, 12);
12881292
if (!zoomable) {
12891293
padding.bottom = Math.min(padding.bottom, 5);
@@ -1466,6 +1470,9 @@ export default function transformProps(
14661470
grid: {
14671471
...defaultGrid,
14681472
...padding,
1473+
// Compact charts prioritize a viable coordinate system over keeping
1474+
// axis labels inside an already constrained grid rectangle.
1475+
containLabel: !usesCompactLayout,
14691476
},
14701477
xAxis,
14711478
yAxis,
@@ -1665,12 +1672,32 @@ export default function transformProps(
16651672
const mergedEchartOptions = customEchartOptions
16661673
? mergeCustomEChartOptions(echartOptions, customEchartOptions)
16671674
: echartOptions;
1668-
const finalSeries = Array.isArray(mergedEchartOptions.series)
1669-
? (mergedEchartOptions.series as SeriesOption[])
1670-
: renderedSeries;
1675+
const mergedSeries = mergedEchartOptions.series;
1676+
const finalSeries = Array.isArray(mergedSeries)
1677+
? (mergedSeries as SeriesOption[])
1678+
: mergedSeries && typeof mergedSeries === 'object'
1679+
? [mergedSeries as SeriesOption]
1680+
: renderedSeries;
1681+
const mergedGrid = Array.isArray(mergedEchartOptions.grid)
1682+
? mergedEchartOptions.grid[0]
1683+
: mergedEchartOptions.grid;
1684+
const finalGrid =
1685+
mergedGrid && typeof mergedGrid === 'object' ? mergedGrid : padding;
16711686
const customLegend = usesCustomLegend
16721687
? buildTimeseriesCustomLegend({
16731688
fallbackColor: theme.colorTextSecondary,
1689+
grid: {
1690+
bottom:
1691+
typeof finalGrid.bottom === 'number' ||
1692+
typeof finalGrid.bottom === 'string'
1693+
? finalGrid.bottom
1694+
: padding.bottom,
1695+
top:
1696+
typeof finalGrid.top === 'number' ||
1697+
typeof finalGrid.top === 'string'
1698+
? finalGrid.top
1699+
: padding.top,
1700+
},
16741701
interactive: !usesPrimaryAxisLegend,
16751702
legendNames: resolvedLegendNames,
16761703
legendState,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ export type TimeseriesLegendItem = {
131131
};
132132

133133
export type TimeseriesCustomLegend = {
134+
grid: {
135+
bottom: number | string;
136+
top: number | string;
137+
};
134138
items: TimeseriesLegendItem[];
135139
orientation: LegendOrientation.Top | LegendOrientation.Bottom;
136140
showSelectors: boolean;

0 commit comments

Comments
 (0)