Skip to content

Commit 2f0d78d

Browse files
committed
fix(echarts): keep compact timeseries grids within bounds
1 parent 560e5bf commit 2f0d78d

5 files changed

Lines changed: 146 additions & 83 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,29 @@ test('uses the post-control body height for compact custom-legend visibility', a
323323
});
324324
});
325325

326+
test('keeps a no-legend grid within a very small post-control body', async () => {
327+
mockOffsetHeight = 40;
328+
render(
329+
<EchartsTimeseries
330+
{...defaultProps}
331+
height={50}
332+
echartOptions={{
333+
grid: { bottom: 37, containLabel: false, top: 12 },
334+
}}
335+
formData={{ ...defaultFormData, zoomable: true }}
336+
/>,
337+
);
338+
339+
await waitFor(() => {
340+
expect(getLatestHeight()).toBe(10);
341+
expect(getLatestEchartProps().echartOptions.grid).toEqual({
342+
bottom: 0,
343+
containLabel: false,
344+
top: 9,
345+
});
346+
});
347+
});
348+
326349
test('falls back to window resize listener when ResizeObserver is unavailable', async () => {
327350
(globalThis as { ResizeObserver?: typeof ResizeObserver }).ResizeObserver =
328351
undefined;

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

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ import type GlobalModel from 'echarts/types/src/model/Global';
4141
import type ComponentModel from 'echarts/types/src/model/Component';
4242
import { EchartsHandler, EventHandlers } from '../types';
4343
import Echart from '../components/Echart';
44+
import {
45+
getViableTimeseriesEchartOptions,
46+
resolveTimeseriesGridOffset,
47+
} from './transformers';
4448
import {
4549
rebaseSeriesData,
4650
snapToNearestX,
@@ -61,19 +65,6 @@ const TIMER_DURATION = 300;
6165
const MAX_CUSTOM_LEGEND_HEIGHT = 160;
6266
const MAX_CUSTOM_LEGEND_HEIGHT_RATIO = 0.3;
6367
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-
}
7768

7869
// Bound the legend after accounting for the fixed ECharts grid reservations,
7970
// leaving enough coordinate space for the plot itself to remain usable.
@@ -87,8 +78,8 @@ export const getTimeseriesLegendMaxHeight = (
8778
Math.max(
8879
Math.floor(
8980
chartBodyHeight -
90-
resolveGridOffset(grid.top, chartBodyHeight) -
91-
resolveGridOffset(grid.bottom, chartBodyHeight) -
81+
resolveTimeseriesGridOffset(grid.top, chartBodyHeight) -
82+
resolveTimeseriesGridOffset(grid.bottom, chartBodyHeight) -
9283
MIN_TIMESERIES_PLOT_HEIGHT,
9384
),
9485
0,
@@ -834,59 +825,15 @@ export default function EchartsTimeseries({
834825
customLegend !== undefined &&
835826
chartBodyHeight > TIMESERIES_CONSTANTS.compactChartHeight &&
836827
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-
]);
828+
const chartEchartOptions = useMemo(
829+
() =>
830+
getViableTimeseriesEchartOptions(
831+
echartOptions,
832+
chartBodyHeight,
833+
formData.zoomable,
834+
),
835+
[chartBodyHeight, echartOptions, formData.zoomable],
836+
);
890837
const renderEchart = ({
891838
chartHeight,
892839
chartWidth,

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ import { defaultGrid, defaultYAxis } from '../defaults';
111111
import {
112112
getBaselineSeriesForStream,
113113
getPadding,
114+
getViableTimeseriesEchartOptions,
114115
transformEventAnnotation,
115116
transformFormulaAnnotation,
116117
transformIntervalAnnotation,
@@ -1672,15 +1673,20 @@ export default function transformProps(
16721673
const mergedEchartOptions = customEchartOptions
16731674
? mergeCustomEChartOptions(echartOptions, customEchartOptions)
16741675
: echartOptions;
1675-
const mergedSeries = mergedEchartOptions.series;
1676+
const viableEchartOptions = getViableTimeseriesEchartOptions(
1677+
mergedEchartOptions,
1678+
height,
1679+
zoomable,
1680+
);
1681+
const mergedSeries = viableEchartOptions.series;
16761682
const finalSeries = Array.isArray(mergedSeries)
16771683
? (mergedSeries as SeriesOption[])
16781684
: mergedSeries && typeof mergedSeries === 'object'
16791685
? [mergedSeries as SeriesOption]
16801686
: renderedSeries;
1681-
const mergedGrid = Array.isArray(mergedEchartOptions.grid)
1682-
? mergedEchartOptions.grid[0]
1683-
: mergedEchartOptions.grid;
1687+
const mergedGrid = Array.isArray(viableEchartOptions.grid)
1688+
? viableEchartOptions.grid[0]
1689+
: viableEchartOptions.grid;
16841690
const finalGrid =
16851691
mergedGrid && typeof mergedGrid === 'object' ? mergedGrid : padding;
16861692
const customLegend = usesCustomLegend
@@ -1707,13 +1713,13 @@ export default function transformProps(
17071713
: undefined;
17081714
const finalEchartOptions = usesCustomLegend
17091715
? {
1710-
...mergedEchartOptions,
1716+
...viableEchartOptions,
17111717
legend: {
1712-
...(mergedEchartOptions.legend as Record<string, unknown>),
1718+
...(viableEchartOptions.legend as Record<string, unknown>),
17131719
show: false,
17141720
},
17151721
}
1716-
: mergedEchartOptions;
1722+
: viableEchartOptions;
17171723

17181724
return {
17191725
customLegend,

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,3 +834,76 @@ export function getPadding(
834834
isHorizontal,
835835
);
836836
}
837+
838+
const MIN_ECHARTS_GRID_HEIGHT = 1;
839+
840+
export function resolveTimeseriesGridOffset(
841+
offset: unknown,
842+
chartHeight: number,
843+
) {
844+
if (typeof offset === 'number') {
845+
return Number.isFinite(offset) ? Math.max(offset, 0) : 0;
846+
}
847+
if (typeof offset !== 'string') {
848+
return 0;
849+
}
850+
851+
const percentage = offset.match(/^\s*(-?\d+(?:\.\d+)?)%\s*$/);
852+
const pixels = percentage
853+
? (Number(percentage[1]) / 100) * chartHeight
854+
: Number(offset);
855+
return Number.isFinite(pixels) ? Math.max(pixels, 0) : 0;
856+
}
857+
858+
export function getViableTimeseriesEchartOptions<Options extends object>(
859+
options: Options,
860+
chartHeight: number,
861+
zoomable: boolean,
862+
): Options {
863+
const optionWithGrid = options as Options & { grid?: unknown };
864+
const gridOption = Array.isArray(optionWithGrid.grid)
865+
? optionWithGrid.grid[0]
866+
: optionWithGrid.grid;
867+
if (!gridOption || typeof gridOption !== 'object') {
868+
return options;
869+
}
870+
871+
const grid = gridOption as Record<string, unknown>;
872+
const rawTop = resolveTimeseriesGridOffset(grid.top, chartHeight);
873+
const rawBottom = resolveTimeseriesGridOffset(grid.bottom, chartHeight);
874+
const isCompact = chartHeight <= TIMESERIES_CONSTANTS.compactChartHeight;
875+
const requestedTop = isCompact ? Math.min(rawTop, 12) : rawTop;
876+
const requestedBottom =
877+
isCompact && !zoomable ? Math.min(rawBottom, 5) : rawBottom;
878+
// Cap both reservations so even a tiny canvas retains a coordinate region.
879+
const reservationBudget = Math.max(chartHeight - MIN_ECHARTS_GRID_HEIGHT, 0);
880+
const top = Math.min(requestedTop, reservationBudget);
881+
const bottom = Math.min(
882+
requestedBottom,
883+
Math.max(reservationBudget - top, 0),
884+
);
885+
const mustDisableContainLabel =
886+
isCompact || requestedTop + requestedBottom > reservationBudget;
887+
888+
if (
889+
top === rawTop &&
890+
bottom === rawBottom &&
891+
(!mustDisableContainLabel || grid.containLabel === false)
892+
) {
893+
return options;
894+
}
895+
896+
const viableGrid = {
897+
...grid,
898+
bottom,
899+
...(mustDisableContainLabel ? { containLabel: false } : {}),
900+
top,
901+
};
902+
903+
return {
904+
...options,
905+
grid: Array.isArray(optionWithGrid.grid)
906+
? [viableGrid, ...optionWithGrid.grid.slice(1)]
907+
: viableGrid,
908+
} as Options;
909+
}

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,11 +1306,15 @@ test('keeps the custom legend absent when a compact chart hides the Plain legend
13061306
});
13071307

13081308
test.each([
1309-
[99, 7],
1310-
[100, 8],
1309+
[10, 9, 0.25, 9, 0],
1310+
[13, 12, 0.25, 12, 0],
1311+
[20, 12, 0.25, 12, 7],
1312+
[30, 12, 1, 12, 17],
1313+
[99, 12, 7, 12, 80],
1314+
[100, 12, 8, 12, 80],
13111315
])(
1312-
'keeps the zoomable ECharts grid viable and hides the Plain legend at %ipx',
1313-
(height, expectedGridHeight) => {
1316+
'keeps the hidden-legend zoomable ECharts grid within a %ipx canvas',
1317+
(height, expectedGridY, expectedGridHeight, expectedTop, expectedBottom) => {
13141318
const getContext = jest
13151319
.spyOn(HTMLCanvasElement.prototype, 'getContext')
13161320
.mockReturnValue({
@@ -1341,7 +1345,9 @@ test.each([
13411345
chart as unknown as {
13421346
getModel: () => {
13431347
getComponent: (component: string) => {
1344-
coordinateSystem: { getRect: () => { height: number } };
1348+
coordinateSystem: {
1349+
getRect: () => { height: number; y: number };
1350+
};
13451351
};
13461352
};
13471353
}
@@ -1350,9 +1356,17 @@ test.each([
13501356
.getComponent('grid');
13511357

13521358
expect(getCustomLegend(transformed)).toBeUndefined();
1353-
expect(gridModel.coordinateSystem.getRect().height).toBe(
1354-
expectedGridHeight,
1355-
);
1359+
expect(transformed.echartOptions.grid).toMatchObject({
1360+
bottom: expectedBottom,
1361+
top: expectedTop,
1362+
});
1363+
const gridRect = gridModel.coordinateSystem.getRect();
1364+
expect(gridRect).toMatchObject({
1365+
height: expectedGridHeight,
1366+
y: expectedGridY,
1367+
});
1368+
expect(gridRect.y).toBeGreaterThanOrEqual(0);
1369+
expect(gridRect.y + gridRect.height).toBeLessThanOrEqual(height);
13561370
} finally {
13571371
chart.dispose();
13581372
getContext.mockRestore();

0 commit comments

Comments
 (0)