xychart: Legend text is clipped on wide charts, and chart collapses/vanishes below minimum height
Description
In xychart, two significant layout bugs occur under common sizing conditions:
- Legend Right-Border Clipping on Wide Charts: Series legend labels positioned on the right side overflow the SVG
viewBoxboundary and are clipped when the chart is configured with a widewidth(e.g. $\ge 2000\text{px}$) and rendered inside responsive containers (such as GitHub Markdown / Gist or any layout withmax-width: 100%). The clipping gets worse aswidthincreases—atwidth: 4000, the final letters of even short 3-letter labels ("CPU","RAM") are sliced almost completely in half. - Chart Vanishing / Layout Crash below 207px Height: When a
titleis defined, settingheightto $\le 206\text{px}$ (at defaultplotReservedSpacePercent: 50) causes the entire diagram to crash and completely vanish. In contrast, whentitleis omitted, the chart gracefully degrades all the way down to a 3px sparkline!
Root Cause Analysis in Codebase
1. Legend Right-Border Clipping (orchestrator.ts)
In packages/mermaid/src/diagrams/xychart/chartBuilder/orchestrator.ts:
legendSpace = this.componentStore.legend.calculateSpace({
width: availableWidth,
height: chartHeight,
});
availableWidth -= legendSpace.width;
if (availableWidth > 0) {
chartWidth += availableWidth; // <-- Absorbs ALL remaining horizontal margin
availableWidth = 0;
}
...
this.componentStore.legend.setBoundingBoxXY({
x: plotX + chartWidth, // <-- Forces legend bounding box to (chartConfig.width - legendSpace.width)
y: plotY + Math.max((chartHeight - legendSpace.height) / 2, 0),
});Because chartWidth += availableWidth absorbs 100% of the remaining horizontal margin, the legend's bounding box is placed flush against the right edge.
In components/legend.ts:
const widthRequired = this.chartConfig.legendPadding * 2 + markerSize + markerSpacing + textDimension.width;The rightmost text character sits at x = chartConfig.width - legendPadding (where legendPadding defaults to only 10px). When an SVG with a wide canvas (e.g. 2400px–4000px) is scaled down by a responsive container (max-width: 100%), that 10px margin is compressed into negligible sub-pixels. Any minor font metric discrepancy in TextDimensionCalculator pushes text outside the SVG viewBox, slicing off characters. Furthermore, adjusting plotReservedSpacePercent has zero effect because chartWidth += availableWidth re-absorbs all reserved margin.
2. Self-Comparison Bug in chartTitle.ts Crashing Low-Height Charts
Compare how all other components safely check available space vs chartTitle.ts:
- In
components/axis/baseAxis.ts(L36):if (heightRequired <= availableHeight) { // Safe guard availableHeight -= heightRequired; this.showTitle = true; } - In
components/legend.ts(L75):if (widthRequired <= availableSpace.width && heightRequired <= availableSpace.height) { // Safe guard this.boundingRect.width = widthRequired; } - In
components/chartTitle.ts(L41):const heightRequired = titleDimension.height + 2 * this.chartConfig.titlePadding; if ( titleDimension.width <= widthRequired && titleDimension.height <= heightRequired && // <-- BUG: Checks against heightRequired instead of availableSpace.height! this.chartConfig.showTitle && this.chartData.title ) { this.boundingRect.width = widthRequired; this.boundingRect.height = heightRequired; this.showChartTitle = true; }
titleDimension.height <= heightRequired is a tautology ($h \le h + 2p$ is always true). It never checks heightRequired <= availableSpace.height!
Because of this bug, ChartTitle unconditionally claims $\sim 40\text{px}$, forcing availableHeight negative in orchestrator.ts at $\le 206\text{px}$, causing D3 scale calculations to throw an exception and vanish the chart.
When title is omitted, the graceful degradation guards in baseAxis.ts work as intended:
- $\ge 121\text{px}$: X-axis title (
"Elapsed Time") renders. - $60\text{px} - 120\text{px}$: Title drops safely, tick numbers (
0..10) render. - $20\text{px} - 59\text{px}$: Numbers drop safely, tick lines render.
- $3\text{px} - 19\text{px}$: Ticks drop safely, bare sparkline data line renders down to 3px!
Steps to reproduce
Legend Cropping:
%%{init:{"xyChart":{"width":4000}}}%% xychart title "Severe Legend Cropping at 4000px" x-axis "Elapsed Time" 0 --> 10 y-axis "Percentage (%)" 0 --> 100 line "CPU" [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] line "RAM" [20, 30, 40, 50, 60, 70, 80, 90, 95, 90]View in a container with
max-width: 100%: the "U" in"CPU"and "M" in"RAM"are cut in half by the right border.Title Height Crash:
%%{init:{"xyChart":{"width":1200,"height":206}}}%% xychart title "Will Crash and Vanish" x-axis "Elapsed Time" 0 --> 10 y-axis "Percentage (%)" 0 --> 100 line "CPU" [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]Change
height: 206toheight: 207: at 206 it vanishes completely, at 207 it renders. Removetitle "..."and it renders down to 3px.
A live reproduction of all ladder tests is viewable on GitHub Gist: https://gist.github.com/Malix-Labs/5ced0989f2d02e33575a6261ad6d29c6
Setup
- Mermaid version: 10.x / 11.x (tested on GitHub Gists / Markdown)
- Browser and Version: Chrome / Firefox on Linux / macOS
Suggested Fixes
- Fix
chartTitle.tsspace check: Change line 41 from:to:titleDimension.height <= heightRequired &&heightRequired <= availableSpace.height && - Fix
orchestrator.tslegend right margin: Do not absorb 100% ofavailableWidthintochartWidth. Preserve a safe margin on the right side of the legend so wide SVGs do not clip text when scaled down.
Source: mermaid-js/mermaid