Skip to content

fix(xychart): label each bar series with its own values when showDataLabel is true - #8521

Open
SwastikTripathi wants to merge 2 commits into
mermaid-js:developfrom
SwastikTripathi:fix/xychart-datalabel-per-series
Open

SwastikTripathi wants to merge 2 commits into
mermaid-js:developfrom
SwastikTripathi:fix/xychart-datalabel-per-series

Conversation

@SwastikTripathi

@SwastikTripathi SwastikTripathi commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

Fixes #8475.

With showDataLabel: true and two or more bar series, every series was
labelled with the first series' values. The root cause was a single
labelData variable computed at the top of draw() from plots[0]:

// before, always reads the first plot
const labelData = db.getXYChartData().plots[0].data.map((data) => data[1]);

That variable was then used unconditionally for every rect shape, so
bar-plot-1, bar-plot-2, … all showed plots[0]'s values.

Fix

labelData is now derived inside the case 'rect': block using the
bar-plot index extracted from shape.groupTexts (bar-plot-N), so each
series reads its own plot's data:

const barPlotGroup = shape.groupTexts.find((t) => t.startsWith('bar-plot-'));
const plotIdx = barPlotGroup ? parseInt(barPlotGroup.replace('bar-plot-', ''), 10) : 0;
const labelData = (allPlots[plotIdx]?.data ?? allPlots[0].data).map((data) => data[1]);

Tests

Two new e2e fixtures added under e2e/diagrams/xychart/pr-check/:

  • should-label-each-bar-series-independently.mmd, vertical orientation, two bar series with distinct values
  • should-label-each-bar-series-independently-horizontal.mmd, same for chartOrientation: horizontal

All existing xychart unit tests (78) pass unchanged.

Summary

Fixes XY chart data labels so each bar series uses its own values when showDataLabel is enabled. The renderer uses the plot index in each bar shape’s bar-plot-N group to select that plot’s data. If the index is missing or the plot is unavailable, it falls back to the first plot.

Adds vertical and horizontal regression fixtures with two bar series. Unit tests also cover a single bar series and a line plot before the bar plots. Label placement when bars overlap remains out of scope.

Test results: The PR description reports that all 78 existing XY chart unit tests pass. The supplied information does not report results for the new unit tests or the end-to-end fixtures.

…Label is true

With showDataLabel enabled, every bar series was labelled with the first
series' values because labelData was computed once from plots[0] at the
top of draw() and then reused for all shapes.

Fix: move labelData inside the rect-shape block and derive it from the
plot whose index matches the shape's bar-plot-N group text.

Closes mermaid-js#8475
@netlify

netlify Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit 9b7e206
🔍 Latest deploy log https://app.netlify.com/projects/mermaid-js/deploys/6af150853aeb5a843a7cee97
😎 Deploy Preview https://deploy-preview-8521--mermaid-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@changeset-bot

changeset-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9b7e206

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mermaid Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the Type: Bug / Error Something isn't working or is incorrect label Oct 23, 2026
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 646799c3-a3ed-4055-a666-d4df7dea36f0
Commits

Reviewing files that changed from the base of the PR and between 6ca81e3 and 9b7e206.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The XY chart renderer now selects data labels from the plot that matches each bar shape. Tests and regression fixtures cover multiple bar series in vertical and horizontal charts.

Changes: XY chart bar-series labels

Layer / File(s) Summary
Plot-indexed labels and regression fixtures
packages/mermaid/src/diagrams/xychart/xychartRenderer.ts, packages/mermaid/src/diagrams/xychart/xychartRenderer.spec.ts, e2e/diagrams/xychart/pr-check/should-label-each-bar-series-independently*.mmd, .changeset/xychart-datalabel-each-bar-series.md
The renderer uses each bar shape’s bar-plot- group to select plot data for labels. It defaults to plot 0 and falls back to the first plot if the indexed plot is unavailable. Tests cover vertical and horizontal bar series, a line series before bars, and a single bar series. Fixtures cover vertical and horizontal charts. The changeset records the patch.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

Suggested reviewers: ashishjain0512

Merge Risk: Minimal · previous review 6ca81

Bar labels now use their own series’ values. No code-level blocker was established; run the required E2E suite after adding its gating label.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title clearly and concisely states the primary fix: labeling each XY chart bar series with its own values when showDataLabel is enabled.
Linked Issues check Passed Issue #8475 requires per-series labels, unchanged single-series behavior, and horizontal-chart support. xychartRenderer.ts selects the bar-plot-N group for each labeled bar and reads that plot's d…
Out of Scope Changes check Passed The changes are limited to per-series xychart data-label selection, focused unit coverage, vertical and horizontal regression fixtures, and a changeset. They do not change label placement for overlapp…
Description check Passed The description states the bug (every bar series showed the first series' values when showDataLabel is true), the root cause (a single labelData computed from plots[0]), the fix (per-series labelData …
Full details: Linked Issues check

Explanation

Issue #8475 requires per-series labels, unchanged single-series behavior, and horizontal-chart support. xychartRenderer.ts selects the bar-plot-N group for each labeled bar and reads that plot's data, with a fallback to plot 0. The added Vitest cases cover multiple vertical series, one series, a line plot before a bar plot, and multiple horizontal bar series. The two e2e fixtures also cover vertical and horizontal charts with distinct values.

Full details: Out of Scope Changes check

Explanation

The changes are limited to per-series xychart data-label selection, focused unit coverage, vertical and horizontal regression fixtures, and a changeset. They do not change label placement for overlapping bars, which issue #8475 excludes.

Full details: Description check

Explanation

The description states the bug (every bar series showed the first series' values when showDataLabel is true), the root cause (a single labelData computed from plots[0]), the fix (per-series labelData derived from the bar-plot-N group), and the tests added (two e2e fixtures, vertical and horizontal) and reports that the 78 existing xychart unit tests pass. It links issue #8475. Against the template, the Summary and Design Decisions content is present. The Tasks checklist is absent: it does not state whether the contribution guidelines were read, whether unit tests were added (the raw summary shows a spec file with 66 added lines, which the description does not mention), whether documentation was updated (not applicable to a bug fix, but not addressed), or whether a changeset was created (a .changeset file is in the diff, but the description does not mention it). The description does not mention the new Vitest unit tests in xychartRenderer.spec.ts; it mentions only the e2e fixtures.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f7e9e77d-cba6-4116-97ff-607f12eec6c1
Commits

Reviewing files that changed from the base of the PR and between 2e17927 and 6ca81e3.

Files selected for processing: 2

  • .changeset/xychart-datalabel-each-bar-series.md
  • packages/mermaid/src/diagrams/xychart/xychartRenderer.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@pkg-pr-new

pkg-pr-new Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@mermaid-js/examples

npm i https://pkg.pr.new/@mermaid-js/examples@8521

mermaid

npm i https://pkg.pr.new/mermaid@8521

@mermaid-js/layout-elk

npm i https://pkg.pr.new/@mermaid-js/layout-elk@8521

@mermaid-js/layout-tidy-tree

npm i https://pkg.pr.new/@mermaid-js/layout-tidy-tree@8521

@mermaid-js/mermaid-zenuml

npm i https://pkg.pr.new/@mermaid-js/mermaid-zenuml@8521

@mermaid-js/parser

npm i https://pkg.pr.new/@mermaid-js/parser@8521

@mermaid-js/tiny

npm i https://pkg.pr.new/@mermaid-js/tiny@8521

commit: 9b7e206

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.49%. Comparing base (6d8921f) to head (9b7e206).
⚠️ Report is 295 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #8521      +/-   ##
===========================================
- Coverage    81.15%   76.49%   -4.66%     
===========================================
  Files          623      608      -15     
  Lines        86037    57884   -28153     
  Branches     19003    17867    -1136     
===========================================
- Hits         69822    44279   -25543     
+ Misses       15208    13584    -1624     
+ Partials      1007       21     -986     
Flag Coverage Δ
e2e ?
unit 76.49% <100.00%> (-2.97%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...es/mermaid/src/diagrams/xychart/xychartRenderer.ts 98.41% <100.00%> (+18.95%) ⬆️

... and 661 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@argos-ci

argos-ci Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Argos notifications ↗︎

Awaiting the start of a new Argos build…

@charan-rathore

Copy link
Copy Markdown

Verified this end to end and it holds up. The bar-plot-N group text is built from the plot's own index in chartData.plots (the plot component passes i straight through), so allPlots[plotIdx] matches the right series even in mixed charts.

What I ran:

  • Parsed and rendered a two-bar-series chart with showDataLabel through the real parser and renderer in jsdom. On the base commit the second series is labelled with the first series' values; on this branch each bar shows its own value.
  • Same check with a line plot first and a bar plot second: the bars still get their own labels.
  • The xychart unit specs pass on this branch (78 tests, 5 files).

I did not run the cypress/argos e2e, but the two new fixtures cover both orientations. Nice fix.

@SwastikTripathi

Copy link
Copy Markdown
Contributor Author

Thanks for verifying this end to end, @charan-rathore. Good to know the mixed-chart case also holds. Ready for a review pass from a maintainer whenever you have bandwidth.

@filipsajdak filipsajdak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works and gives the same output as #8489, but this PR has no test that can fail and no changeset, so I am leaving a comment rather than an approval.

What I ran, on develop 63333fd with this branch merged (dev server, headless Chromium, texts read from each bar-plot-N group):

  • The issue's chart (before [380, 210, 140], after [95, 60, 130], showDataLabel: true). develop: both groups read 380, 210, 140. This branch: 380, 210, 140 and 95, 60, 130. Same for xychart horizontal, for a chart with a line series first plus three bar series, and with showDataLabelOutsideBar: true.
  • SVG of the 48 existing xychart fixtures plus the two new ones and four charts of mine, develop against this branch: only the new multi-series cases change (6 of 54). The same set rendered on #8489 is byte-identical to this branch.

Findings:

  1. No unit test. The two new .mmd files under e2e/diagrams/xychart/pr-check/ are rendered and compared by Argos, but nothing asserts the labels, so a regression only shows as a screenshot difference somebody has to accept. #8489 adds a jsdom spec that fails without the fix (2 of its 3 tests do).
  2. No changeset in the diff (git diff upstream/develop... lists three files, none under .changeset/). Fixes that change the rendered output normally ship one.
  3. The code: parseInt(barPlotGroup.replace('bar-plot-', ''), 10) and allPlots[plotIdx]?.data ?? allPlots[0].data are fine. A bar whose label value is missing still goes through labelData[i].toString() and would throw; #8489 skips such bars.

Suggestion if you want to keep this PR: add a changeset and a small spec like the one in #8489 (render two bar series through the parser, db and renderer in jsdom and read the texts of each bar-plot-N).

Compared with #8489: same cause, same fix idea, identical rendered output. I would pick #8489 because of the unit test, the changeset and the missing-label guard; this one would then be the duplicate.

Not checked: CI results for the head commit.

The PR description ends with a "Generated with Claude Code" footer; the project asks that PR descriptions carry no AI attribution (maintainers raised it on #7859), so you may want to drop it.

You also have several venn and commonDb PRs open (#8560, #8561, #8562, #8563); I am reviewing #8560 and #8562 separately.

…abel fix

Add a jsdom spec covering the multi-series label regression: 3 of 4 tests
fail on the original renderer (all bar groups read plots[0] values) and
pass with the fix. Also add the missing patch changeset.
@SwastikTripathi

Copy link
Copy Markdown
Contributor Author

Done in 6ca81e3a4: added xychartRenderer.spec.ts with 4 jsdom cases (3 fail without the fix -- both series read plots[0] labels; horizontal variant included) and .changeset/xychart-datalabel-each-bar-series.md. Verified with the original renderer to confirm 3 tests fail before the fix.

@filipsajdak filipsajdak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving: the two things I asked for in my earlier review are now in the PR (head 6ca81e3).

What I re-ran, with this branch merged onto current develop (a0fd6b8): vitest run packages/mermaid/src/diagrams/xychart/xychartRenderer.spec.ts, 4 tests pass. With xychartRenderer.ts restored from develop, 3 of the 4 fail ("gives each bar series its own labels", "assigns correct labels when a line series precedes two bar series", "gives each horizontal bar series its own labels"), so the spec does guard the fix. The changeset is in the diff.

The rendered output is the same as #8489 in my earlier comparison, and that has not changed; #8489 remains a fine alternative, so only one of the two is needed. I did not rerun the browser renders on this head and did not run a mutation tool.

This branch has not been deployed

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

Labels

Type: Bug / Error Something isn't working or is incorrect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xychart: with showDataLabel, every bar series shows the first series' values

3 participants