Skip to content

[charts] Add demo for displaying the sum of all values from the stacked series on top of the stacked bars#22879

Merged
sai6855 merged 8 commits into
mui:masterfrom
sai6855:stack-bar
Jun 22, 2026
Merged

[charts] Add demo for displaying the sum of all values from the stacked series on top of the stacked bars#22879
sai6855 merged 8 commits into
mui:masterfrom
sai6855:stack-bar

Conversation

@sai6855

@sai6855 sai6855 commented Jun 18, 2026

Copy link
Copy Markdown
Member

preview: https://deploy-preview-22879--material-ui-x.netlify.app/x/react-charts/bars/#stack-total-labels

closes #19458

Since the barLabel slot was introduced, a separate prop no longer seems necessary. Therefore, I went with a demo implementation instead of adding a new prop as described in the issue.

@sai6855 sai6855 added docs Improvements or additions to the documentation. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. scope: charts Changes related to the charts. labels Jun 18, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Jun 18, 2026

Copy link
Copy Markdown

Deploy preview

Bundle size

Bundle Parsed size Gzip size
@mui/x-data-grid 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-pro 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-premium 0B(0.00%) 0B(0.00%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 0B(0.00%) 0B(0.00%)
@mui/x-charts-premium 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)
@mui/x-license 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new bar-charts documentation demo showing how to render “stack total” labels above stacked bars by composing a custom overlay component inside ChartsContainer.

Changes:

  • Added a new StackTotalLabels demo that computes totals via useBarSeries() and positions labels using useXScale()/useYScale().
  • Documented the approach in the Bars docs under a new “Stack total labels” section.
  • Added both TSX source and generated JS demo files.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
docs/data/charts/bars/StackTotalLabels.tsx New TypeScript demo overlay that computes and renders stack total labels.
docs/data/charts/bars/StackTotalLabels.js Generated JavaScript version of the same demo.
docs/data/charts/bars/bars.md Adds a new docs section linking to the demo and explaining why a custom overlay is needed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/data/charts/bars/StackTotalLabels.tsx
Comment thread docs/data/charts/bars/StackTotalLabels.js
Comment thread docs/data/charts/bars/StackTotalLabels.tsx
Comment thread docs/data/charts/bars/StackTotalLabels.js
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: sai chand <60743144+sai6855@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment on lines +65 to +71
const TotalLabel = styled('text')(({ theme }) => ({
...theme?.typography?.body2,
fontWeight: 600,
stroke: 'none',
fill: (theme.vars || theme)?.palette?.text?.primary,
dominantBaseline: 'central',
}));
Comment on lines +65 to +71
const TotalLabel = styled('text')(({ theme }) => ({
...theme?.typography?.body2,
fontWeight: 600,
stroke: 'none',
fill: (theme.vars || theme)?.palette?.text?.primary,
dominantBaseline: 'central',
}));
Comment thread docs/data/charts/bars/bars.md Outdated
sai6855 and others added 5 commits June 18, 2026 16:17
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: sai chand <60743144+sai6855@users.noreply.github.com>
@sai6855 sai6855 marked this pull request as ready for review June 19, 2026 06:09
@JCQuintas

Copy link
Copy Markdown
Member

@alexfauquette I think having the prop would be good? Like this is probably a common use-case.

Something like the following could work.

stackLabel?: 'sum' | ((stack: StackLabelContext) => string | null | undefined)

@alexfauquette alexfauquette left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Look nice. 👍

@alexfauquette

alexfauquette commented Jun 22, 2026

Copy link
Copy Markdown
Member

I think having the prop would be good?

The issue with the props is the positioning. In a simple case with just one stack colomun like in the demo it's ok.

It becomes less obvious when

  • There are negative values: do you display the total, or do you display the positive total on top and negative total at the bottom)
  • There are multiple stacking column: do you display a total per column, or do you aggregate all the data

The demo has the advenatge to be failry easy to read and a good starting point for anyone who want to do a more complex one

@sai6855 sai6855 merged commit 8382a1f into mui:master Jun 22, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to the documentation. scope: charts Changes related to the charts. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[charts] Support for displaying the sum of all values from the stacked series on top of the stacked bars

4 participants