#8283·mermaid

xychart: Legend text is clipped on wide charts, and chart collapses/vanishes below minimum height

Author: Malix-LabsCreated Sep 20, 2026Updated Sep 28, 2026
LabelsStatus: ApprovedGraph: XYv12.0.1

Description

In xychart, two significant layout bugs occur under common sizing conditions:

  1. Legend Right-Border Clipping on Wide Charts: Series legend labels positioned on the right side overflow the SVG viewBox boundary and are clipped when the chart is configured with a wide width (e.g. $\ge 2000\text{px}$) and rendered inside responsive containers (such as GitHub Markdown / Gist or any layout with max-width: 100%). The clipping gets worse as width increases—at width: 4000, the final letters of even short 3-letter labels ("CPU", "RAM") are sliced almost completely in half.
  2. Chart Vanishing / Layout Crash below 207px Height: When a title is defined, setting height to $\le 206\text{px}$ (at default plotReservedSpacePercent: 50) causes the entire diagram to crash and completely vanish. In contrast, when title is 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:

typescript
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:

typescript
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):
    typescript
    if (heightRequired <= availableHeight) { // Safe guard
      availableHeight -= heightRequired;
      this.showTitle = true;
    }
  • In components/legend.ts (L75):
    typescript
    if (widthRequired <= availableSpace.width && heightRequired <= availableSpace.height) { // Safe guard
      this.boundingRect.width = widthRequired;
    }
  • In components/chartTitle.ts (L41):
    typescript
    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

  1. Legend Cropping:

    mermaid
    %%{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.

  2. Title Height Crash:

    mermaid
    %%{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: 206 to height: 207: at 206 it vanishes completely, at 207 it renders. Remove title "..." 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

  1. Fix chartTitle.ts space check: Change line 41 from:
    typescript
    titleDimension.height <= heightRequired &&
    to:
    typescript
    heightRequired <= availableSpace.height &&
  2. Fix orchestrator.ts legend right margin: Do not absorb 100% of availableWidth into chartWidth. Preserve a safe margin on the right side of the legend so wide SVGs do not clip text when scaled down.