Skip to content

fix(xychart): auto-flip point labels to avoid line collision#2

Closed
DominicBurkart wants to merge 2 commits intoclaude/add-xychart-point-labels-NYGcCfrom
claude/fix-label-line-collision-HNiTa
Closed

fix(xychart): auto-flip point labels to avoid line collision#2
DominicBurkart wants to merge 2 commits intoclaude/add-xychart-point-labels-NYGcCfrom
claude/fix-label-line-collision-HNiTa

Conversation

@DominicBurkart
Copy link
Copy Markdown
Owner

@DominicBurkart DominicBurkart commented Mar 29, 2026

Summary

  • Adds collision detection for point labels on xychart line plots — when a label's bounding box would intersect an adjacent line segment, the label is automatically flipped to the opposite side of the data point (below instead of above for vertical charts, left instead of right for horizontal charts)
  • Fixes the issue visible in PR #1 QA example 6 where the steep descent from PaLM (540) to LLaMA-65B (65) causes the line to pass through the "LLaMA-65B" label

Stacked on #1.

Approach

For each labeled point, the algorithm:

  1. Estimates the label's bounding box using fontSize * label.length * 0.7 (same heuristic as bar label renderer)
  2. Checks if either adjacent line segment (prev→current, current→next) intersects that box
  3. If collision detected, flips the label to the opposite side of the point

Before (PR #1)

Labels always above — line cuts through "LLaMA-65B":

06-real-world

Changes

File Change
linePlot.ts Added lineYAtX(), doesSegmentIntersectBox(), shouldFlipLabelVertical(), shouldFlipLabelHorizontal() helpers; modified label loop to use conditional positioning
xyChart.spec.js 3 new Cypress visual regression tests (steep descent, gentle slope, zigzag)
demos/xychart.html 2 new demo cases for manual QA

Test plan

  • All 4008 vitest unit tests pass (0 failures)
  • Cypress visual regression: steep descent chart — labels should flip below when line would collide
  • Cypress visual regression: gentle slope chart — labels should stay above (no false positives)
  • Cypress visual regression: zigzag pattern — mix of above/below labels
  • Manual QA: open demos/xychart.html and verify the MMLU steep-descent demo

https://claude.ai/code/session_01UCV1QCaRsRtQjRspaVrqUp

Visual QA evidence for the per-point text labels feature,
rendered with Playwright using the forest theme.

https://claude.ai/code/session_01CgvREhkQ3tVYCagq1mHmjV
Copy link
Copy Markdown
Owner Author

DominicBurkart commented Mar 29, 2026

QA Results v2: Label-Line Collision Avoidance


1. Steep Descent — MMLU Real-World Example

PASS — "LLaMA-65B", "Llama 2 34B", "Mistral 7B", "Phi-3-mini" flipped below their points, away from the descending line. "PaLM" stays above.

collision-01-steep-descent


2. Gentle Slope — No Collision (regression check)

PASS — All labels ("Start", "Mid", "End") remain above. No false positives.

collision-02-gentle-slope


3. Zigzag Pattern — Mixed Collision (previously broken, now fixed)

PASS — Valley points ("Low" at P2, "Bottom" at P4) now correctly flip labels below. Peak points ("High" at P1, "Peak" at P3) stay above.

collision-03-zigzag


Summary

# Test Case Labels Position Result
1 Steep descent (MMLU) Flipped below on descent PASS
2 Gentle slope Stayed above (default) PASS
3 Zigzag (v1 bug) Peaks above, valleys below PASS

Automated tests: 4008/4008 vitest unit tests pass, 0 regressions.


Generated by Claude Code

When a line segment has a steep slope, labels placed above a data point
can collide with the line. This adds collision detection that estimates
each label's bounding box and checks for intersection with adjacent line
segments using y-range overlap. When a collision is detected, the label
is flipped to the opposite side of the point (below for vertical charts,
left for horizontal charts).

https://claude.ai/code/session_01UCV1QCaRsRtQjRspaVrqUp
@DominicBurkart DominicBurkart force-pushed the claude/fix-label-line-collision-HNiTa branch from b085228 to 8c36d1d Compare March 29, 2026 11:20
@DominicBurkart
Copy link
Copy Markdown
Owner Author

Superseded by mermaid-js#7551

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants