Compare commits

...

3 Commits

Author SHA1 Message Date
darshanr0107
c7b2fe3b95 fix: add adaptive font sizing for complex treemaps
on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
2025-12-19 14:29:37 +05:30
darshanr0107
365c1b1062 chore: add changeset
on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
2025-12-16 13:53:58 +05:30
darshanr0107
fd142d0915 fix: ensure labels are visible in large treemap diagrams
on-behalf-of: @Mermaid-Chart <hello@mermaidchart.com>
2025-12-16 13:48:13 +05:30
3 changed files with 99 additions and 24 deletions

View File

@@ -0,0 +1,5 @@
---
'mermaid': patch
---
fix: Ensure treemap labels render correctly in large nested diagrams

View File

@@ -468,4 +468,65 @@ classDef highlight fill:#f39c12,color:#000,stroke:#e67e22,stroke-width:2px;
);
});
*/
it('14: should render labels in complex treemap with many nested blocks', () => {
imgSnapshotTest(
`treemap-beta
"🔴 High Activity (Top 50%)":::redContainer
"packages/app (1115)": 15:::redleaf
"packages/app/src (844)": 12:::redleaf
"packages/app/src/lib (707)": 11:::redleaf
"packages/app/src/routes (353)": 7:::redleaf
"packages/app/tests (277)": 6:::redleaf
"packages/app/tests/e2e (245)": 6:::redleaf
"packages/app/tests/common (48)": 4:::redleaf
"packages/llm-prompts (29)": 3:::redleaf
"packages/emails (26)": 3:::redleaf
"packages/app/tests/api (23)": 3:::redleaf
"packages/emails/src (18)": 3:::redleaf
"packages/emails/src/emails (17)": 3:::redleaf
"packages/app/static (15)": 3:::redleaf
"packages/llm-prompts/diagram-chat (15)": 3:::redleaf
"packages/app/prisma (11)": 3:::redleaf
"packages/app/prisma/migrations (11)": 3:::redleaf
"packages/llm-prompts/shared (9)": 3:::redleaf
"packages/app/static/icons (8)": 3:::redleaf
"packages/emails/src/components (8)": 3:::redleaf
"packages/app-observability (8)": 3:::redleaf
"packages/mermaid-pre-render-server (8)": 3:::redleaf
"🟠 Medium Activity (35%)":::orangeContainer
"packages/app/tests/mobile (6)": 3:::orangeleaf
"packages/llm-prompts/diagram-chat/tests (6)": 3:::orangeleaf
"packages/eslint-plugin (6)": 3:::orangeleaf
"packages/eslint-plugin/src (5)": 3:::orangeleaf
"packages/eslint-plugin/src/rules (5)": 3:::orangeleaf
"packages/app/static/img (4)": 3:::orangeleaf
"packages/llm-prompts/tests (4)": 3:::orangeleaf
"packages/llm-prompts/common (3)": 3:::orangeleaf
"packages/mermaid-pre-render-server/tests (3)": 3:::orangeleaf
"packages/mermaid-pre-render-server/tests/e2e (3)": 3:::orangeleaf
"packages/app/tests/seed (2)": 3:::orangeleaf
"packages/llm-prompts/shared/patches (2)": 3:::orangeleaf
"packages/mermaid-pre-render-server/src (2)": 3:::orangeleaf
"packages/app/scripts (1)": 3:::orangeleaf
"🟡 Low Activity (15%)":::yellowContainer
"packages/llm-prompts/regenerate-diagram (1)": 3:::yellowleaf
"packages/llm-prompts/repair-diagram (1)": 3:::yellowleaf
"packages/app-buildship (1)": 3:::yellowleaf
"packages/app-buildship/src (1)": 3:::yellowleaf
"packages/app-buildship/src/cleanupAssets (1)": 3:::yellowleaf
"packages/icons (1)": 3:::yellowleaf
classDef root fill:#F5BA71
classDef redContainer fill:#F54927
classDef orangeContainer fill:#DE8118
classDef yellowContainer fill:#F5E514
classDef redleaf fill:#F54927,stroke:none,color:#FFFFFF
classDef orangeleaf fill:#DE8118,stroke:none,color:#FFFFFF
classDef yellowleaf fill:#F5E514,stroke:none,color:#333333
`,
{}
);
});
});

View File

@@ -224,6 +224,7 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
.attr('dominant-baseline', 'middle')
.text((d) => (d.depth === 0 ? '' : d.data.name)) // Skip label for root section
.attr('font-weight', 'bold')
.attr('clip-path', (_d, i) => `url(#clip-section-${id}-${i})`) // Apply clip-path to prevent overflow
.attr('style', (d) => {
// Hide the label for the root section
if (d.depth === 0) {
@@ -309,6 +310,17 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
// Draw the leaf nodes
const leafNodes = treemapData.leaves();
const isComplexTreemap = leafNodes.length > 20;
const baseLabelFontSize = isComplexTreemap ? 16 : 38;
const baseValueFontSize = isComplexTreemap ? 14 : 28;
const minLabelFontSize = isComplexTreemap ? 4 : 8;
const minValueFontSize = isComplexTreemap ? 4 : 6;
const labelPadding = isComplexTreemap ? 2 : 4;
const minDisplayThreshold = isComplexTreemap ? 8 : 10;
const spacingBetweenLabelAndValue = isComplexTreemap ? 1 : 2;
const cell = g
.selectAll('.treemapLeafGroup')
.data(leafNodes)
@@ -359,7 +371,7 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
// .style('fill', (d) => colorScaleLabel(d.data.name))
.attr('style', (d) => {
const labelStyles =
'text-anchor: middle; dominant-baseline: middle; font-size: 38px;fill:' +
`text-anchor: middle; dominant-baseline: middle; font-size: ${baseLabelFontSize}px;fill:` +
colorScaleLabel(d.data.name) +
';';
const styles = styles2String({ cssCompiledStyles: d.data.cssCompiledStyles } as Node);
@@ -374,21 +386,16 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
const nodeHeight = d.y1 - d.y0;
const textNode = self.node()!;
const padding = 4;
const availableWidth = nodeWidth - 2 * padding;
const availableHeight = nodeHeight - 2 * padding;
const availableWidth = nodeWidth - 2 * labelPadding;
const availableHeight = nodeHeight - 2 * labelPadding;
if (availableWidth < 10 || availableHeight < 10) {
if (availableWidth < minDisplayThreshold || availableHeight < minDisplayThreshold) {
self.style('display', 'none');
return;
}
let currentLabelFontSize = parseInt(self.style('font-size'), 10);
const minLabelFontSize = 8;
const originalValueRelFontSize = 28; // Original font size of value, for max cap
const valueScaleFactor = 0.6; // Value font size as a factor of label font size
const minValueFontSize = 6;
const spacingBetweenLabelAndValue = 2;
// 1. Adjust label font size to fit width
while (
@@ -402,7 +409,7 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
// 2. Adjust both label and prospective value font size to fit combined height
let prospectiveValueFontSize = Math.max(
minValueFontSize,
Math.min(originalValueRelFontSize, Math.round(currentLabelFontSize * valueScaleFactor))
Math.min(baseValueFontSize, Math.round(currentLabelFontSize * valueScaleFactor))
);
let combinedHeight =
currentLabelFontSize + spacingBetweenLabelAndValue + prospectiveValueFontSize;
@@ -411,7 +418,7 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
currentLabelFontSize--;
prospectiveValueFontSize = Math.max(
minValueFontSize,
Math.min(originalValueRelFontSize, Math.round(currentLabelFontSize * valueScaleFactor))
Math.min(baseValueFontSize, Math.round(currentLabelFontSize * valueScaleFactor))
);
if (
prospectiveValueFontSize < minValueFontSize &&
@@ -432,13 +439,18 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
self.style('font-size', `${currentLabelFontSize}px`);
// 3. Final visibility check for the label
if (
textNode.getComputedTextLength() > availableWidth ||
currentLabelFontSize < minLabelFontSize ||
availableHeight < currentLabelFontSize
) {
self.style('display', 'none');
// If label is hidden, value will be hidden by its own .each() loop
if (isComplexTreemap) {
if (currentLabelFontSize < minLabelFontSize || availableHeight < minLabelFontSize) {
self.style('display', 'none');
}
} else {
if (
textNode.getComputedTextLength() > availableWidth ||
currentLabelFontSize < minLabelFontSize ||
availableHeight < currentLabelFontSize
) {
self.style('display', 'none');
}
}
});
@@ -454,7 +466,7 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
})
.attr('style', (d) => {
const labelStyles =
'text-anchor: middle; dominant-baseline: hanging; font-size: 28px;fill:' +
`text-anchor: middle; dominant-baseline: hanging; font-size: ${baseValueFontSize}px;fill:` +
colorScaleLabel(d.data.name) +
';';
const styles = styles2String({ cssCompiledStyles: d.data.cssCompiledStyles } as Node);
@@ -481,14 +493,11 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
}
const finalLabelFontSize = parseFloat(labelElement.style('font-size'));
const originalValueFontSize = 28; // From initial style setting
const valueScaleFactor = 0.6;
const minValueFontSize = 6;
const spacingBetweenLabelAndValue = 2;
const actualValueFontSize = Math.max(
minValueFontSize,
Math.min(originalValueFontSize, Math.round(finalLabelFontSize * valueScaleFactor))
Math.min(baseValueFontSize, Math.round(finalLabelFontSize * valueScaleFactor))
);
valueTextElement.style('font-size', `${actualValueFontSize}px`);
@@ -500,7 +509,7 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
const nodeTotalHeight = d.y1 - d.y0;
const cellBottomPadding = 4;
const maxValueBottomY = nodeTotalHeight - cellBottomPadding;
const availableWidthForValue = nodeWidth - 2 * 4; // padding for value text
const availableWidthForValue = nodeWidth - 2 * labelPadding;
if (
valueTextElement.node()!.getComputedTextLength() > availableWidthForValue ||