-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[charts] Add demo for displaying the sum of all values from the stacked series on top of the stacked bars #22879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+167
−0
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f672ba
[charts] Add demo for displaying the sum of all values from the stack…
sai6855 24b4fa5
remove preview
sai6855 bd69064
Apply suggestions from code review
sai6855 4d0e77a
Potential fix for pull request finding
sai6855 2e0a6f2
fix review
sai6855 b64fd2d
Merge branches 'stack-bar' and 'stack-bar' of https://github.com/sai6…
sai6855 77d9ccf
use gatvaluetopositionmapper
sai6855 2351999
Merge branch 'master' into stack-bar
sai6855 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import * as React from 'react'; | ||
| import { ChartsContainer } from '@mui/x-charts/ChartsContainer'; | ||
| import { BarPlot } from '@mui/x-charts/BarChart'; | ||
| import { ChartsXAxis } from '@mui/x-charts/ChartsXAxis'; | ||
| import { ChartsYAxis } from '@mui/x-charts/ChartsYAxis'; | ||
| import { useBarSeries, useXScale, useYScale } from '@mui/x-charts/hooks'; | ||
| import { styled } from '@mui/material/styles'; | ||
|
|
||
| export default function StackTotalLabels() { | ||
| return ( | ||
| <ChartsContainer | ||
| xAxis={[{ scaleType: 'band', data: ['Q1', 'Q2', 'Q3'] }]} | ||
| series={[ | ||
| { | ||
| type: 'bar', | ||
| id: 'a', | ||
| stack: 'total', | ||
| data: [5, 17, 11], | ||
| barLabel: 'value', | ||
| }, | ||
| { type: 'bar', id: 'b', stack: 'total', data: [4, 8, 6], barLabel: 'value' }, | ||
| ]} | ||
| height={400} | ||
| yAxis={[{ width: 30 }]} | ||
| margin={{ left: 0, right: 10, top: 30 }} | ||
| > | ||
| <BarPlot /> | ||
| <StackTotals /> | ||
| <ChartsXAxis /> | ||
| <ChartsYAxis /> | ||
| </ChartsContainer> | ||
| ); | ||
| } | ||
|
|
||
| function StackTotals() { | ||
| const xScale = useXScale(); | ||
| const yScale = useYScale(); | ||
| const barSeries = useBarSeries(); | ||
|
|
||
| const categories = xScale.domain(); | ||
| const bandWidth = xScale.bandwidth(); | ||
|
|
||
| return ( | ||
| <React.Fragment> | ||
| {categories.map((category, dataIndex) => { | ||
| const total = barSeries.reduce( | ||
| (acc, series) => | ||
| acc + (series.stack === 'total' ? (series.data[dataIndex] ?? 0) : 0), | ||
| 0, | ||
| ); | ||
|
|
||
| const cx = (xScale(category) ?? 0) + bandWidth / 2; | ||
| const cy = yScale(total) ?? 0; | ||
|
|
||
| return ( | ||
| <TotalLabel key={String(category)} x={cx} y={cy - 8}> | ||
| {total} | ||
| </TotalLabel> | ||
| ); | ||
| })} | ||
| </React.Fragment> | ||
| ); | ||
| } | ||
|
|
||
| const TotalLabel = styled('text')(({ theme }) => ({ | ||
| ...theme?.typography?.body2, | ||
| fontWeight: 600, | ||
| stroke: 'none', | ||
| fill: (theme.vars || theme)?.palette?.text?.primary, | ||
| dominantBaseline: 'central', | ||
| })); | ||
|
Comment on lines
+71
to
+79
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import * as React from 'react'; | ||
| import { ChartsContainer } from '@mui/x-charts/ChartsContainer'; | ||
| import { BarPlot } from '@mui/x-charts/BarChart'; | ||
| import { ChartsXAxis } from '@mui/x-charts/ChartsXAxis'; | ||
| import { ChartsYAxis } from '@mui/x-charts/ChartsYAxis'; | ||
| import { useBarSeries, useXScale, useYScale } from '@mui/x-charts/hooks'; | ||
| import { styled } from '@mui/material/styles'; | ||
|
|
||
| export default function StackTotalLabels() { | ||
| return ( | ||
| <ChartsContainer | ||
| xAxis={[{ scaleType: 'band', data: ['Q1', 'Q2', 'Q3'] }]} | ||
| series={[ | ||
| { | ||
| type: 'bar', | ||
| id: 'a', | ||
| stack: 'total', | ||
| data: [5, 17, 11], | ||
| barLabel: 'value', | ||
| }, | ||
| { type: 'bar', id: 'b', stack: 'total', data: [4, 8, 6], barLabel: 'value' }, | ||
| ]} | ||
| height={400} | ||
| yAxis={[{ width: 30 }]} | ||
| margin={{ left: 0, right: 10, top: 30 }} | ||
| > | ||
| <BarPlot /> | ||
| <StackTotals /> | ||
| <ChartsXAxis /> | ||
| <ChartsYAxis /> | ||
| </ChartsContainer> | ||
| ); | ||
| } | ||
|
|
||
| function StackTotals() { | ||
| const xScale = useXScale<'band'>(); | ||
| const yScale = useYScale(); | ||
| const barSeries = useBarSeries(); | ||
|
|
||
| const categories = xScale.domain(); | ||
| const bandWidth = xScale.bandwidth(); | ||
|
|
||
| return ( | ||
| <React.Fragment> | ||
| {categories.map((category, dataIndex) => { | ||
| const total = barSeries.reduce( | ||
| (acc, series) => | ||
| acc + (series.stack === 'total' ? (series.data[dataIndex] ?? 0) : 0), | ||
| 0, | ||
| ); | ||
|
sai6855 marked this conversation as resolved.
|
||
|
|
||
| const cx = (xScale(category) ?? 0) + bandWidth / 2; | ||
| const cy = yScale(total) ?? 0; | ||
|
|
||
| return ( | ||
| <TotalLabel key={String(category)} x={cx} y={cy - 8}> | ||
| {total} | ||
| </TotalLabel> | ||
| ); | ||
| })} | ||
| </React.Fragment> | ||
| ); | ||
| } | ||
|
|
||
| const TotalLabel = styled('text')(({ theme }) => ({ | ||
| ...theme?.typography?.body2, | ||
| fontWeight: 600, | ||
| stroke: 'none', | ||
| fill: (theme.vars || theme)?.palette?.text?.primary, | ||
| dominantBaseline: 'central', | ||
| })); | ||
|
Comment on lines
+71
to
+79
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.