xychart: 在宽幅图表中,图例文本被裁剪,图表在最小高度以下会收缩或消失
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.
内容来源: mermaid-js/mermaid