Skip to content

feat: Add stacked segment support for PieChart sections - #2004

Merged
imaNNeo merged 6 commits into
imaNNeo:mainfrom
jay-k98:feature/stacked-pie-chart
Apr 15, 2026
Merged

imaNNeo merged 6 commits into
imaNNeo:mainfrom
jay-k98:feature/stacked-pie-chart

Conversation

@jay-k98

@jay-k98 jay-k98 commented Sep 22, 2025 •

Copy link
Copy Markdown
Contributor

As described in #2003 a "Stacked Pie Chart" was requested. This Pull Request aims to add this feature.

Solution

The Implementation of [StackedPieChart] is very similar to [PieChart]. The biggest difference is drawing a Section with multiple Segments.

The radius of a segment is calculated like this:

SegmentRadius = SectionRadius * SegmentValue / TotalSectionValues

The sweep angles are then calculated like in [PieChart]

Todo's and Improvements

  • Add drawing strokes
  • Extend the Demo (add icon and more examples)
  • Add tests
  • Extract duplicate logic from [PieChart] and [StackedPieChart]

@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch from 378932b to 6a9aec1 Compare September 22, 2025 12:41
@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch 2 times, most recently from d04f72f to 2ecef63 Compare September 30, 2025 08:21
@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch from 2ecef63 to b88f01f Compare October 6, 2025 11:43
@codecov

codecov Bot commented Oct 26, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.06%. Comparing base (c83aa90) to head (578a084).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2004      +/-   ##
==========================================
+ Coverage   93.00%   93.06%   +0.05%     
==========================================
  Files          50       50              
  Lines        3917     3949      +32     
==========================================
+ Hits         3643     3675      +32     
  Misses        274      274              
Flag Coverage Δ
flutter 93.06% <100.00%> (+0.05%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@imaNNeo

imaNNeo commented Oct 26, 2025

Copy link
Copy Markdown
Owner

Hi, thanks for your contribution.
I think we can use the current implementation of the PieChart to just add the stacked logic into it.
This way, we reuse a lot of code that we already wrote.

Just like what I did for the stacked bar chart here: #112

Please let me know your thoughts on it.

@jay-k98

jay-k98 commented Oct 26, 2025

Copy link
Copy Markdown
Contributor Author

Hi, yeah thank you for responding! I wanted to hear your opinion about that. I'll implement analogous to #112 and add tests :)

@imaNNeo

imaNNeo commented Oct 26, 2025

Copy link
Copy Markdown
Owner

I appreciate it!
Thanks in advance.

@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch 2 times, most recently from a47be1d to e10651b Compare November 10, 2025 12:50
@jay-k98

jay-k98 commented Nov 10, 2025

Copy link
Copy Markdown
Contributor Author

Hi @imaNNeo i was on vacation but now I'm back and implemented the stacked logic for PieChart. I also updated tests and added a new sample for the example app. Looking forward to your feedback :)

@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch 2 times, most recently from c98c21b to 5bee1b4 Compare November 10, 2025 15:32
@Bazni

Bazni commented Dec 17, 2025

Copy link
Copy Markdown

Hey 👋,
I think I commented at the wrong place the first time, sorry about that.

Thx for the work. I'm looking to do something like this:
image

Do you think I will be able to do it with your StackedPieChart?

@imaNNeo

imaNNeo commented Dec 20, 2025

Copy link
Copy Markdown
Owner

Hi @imaNNeo i was on vacation but now I'm back and implemented the stacked logic for PieChart. I also updated tests and added a new sample for the example app. Looking forward to your feedback :)

Can you please update your branch with the latest changes? Then I can check it again.
(You don't need to update the CHANGELOG.md)

@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch from 75f60a7 to 271d231 Compare January 5, 2026 12:11
@jay-k98

jay-k98 commented Jan 5, 2026

Copy link
Copy Markdown
Contributor Author

Can you please update your branch with the latest changes? Then I can check it again. (You don't need to update the CHANGELOG.md)

Hey, I updated the branch and removed CHANGELOG.md changes.

@jay-k98

jay-k98 commented Jan 5, 2026

Copy link
Copy Markdown
Contributor Author

Hey 👋, I think I commented at the wrong place the first time, sorry about that.

Thx for the work. I'm looking to do something like this: image

Do you think I will be able to do it with your StackedPieChart?

Yes, the green and red part will definetly be possible. You can checkout the branch of this PR and run the example. Pie Chart Sample 4 is pretty close to the screenshot you provided.

About the grid you might need to get creative.. :D But I guess you could put two PieCharts in a Stack and use the "lower" PieChart to create the grid with section and segment outlines as well as labels. Hope this helps :)

@imaNNeo

imaNNeo commented Feb 25, 2026

Copy link
Copy Markdown
Owner

Can you please update your branch?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This pull request implements a "Stacked Pie Chart" feature that allows pie chart sections to contain multiple concentric segments (radial bands), addressing issue #2003. The implementation extends the existing PieChart functionality by adding segment support to PieChartSectionData.

Changes:

  • Introduced PieChartStackSegmentData class to represent individual segments within a pie chart section
  • Extended PieChartSectionData with segments list and segmentsSpace properties to support stacked rendering
  • Refactored pie chart rendering to draw individual segments instead of full sections, using new drawSegments, drawSegment, and generateSegmentPath methods

Reviewed changes

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

Show a summary per file
File Description
lib/src/chart/pie_chart/pie_chart_data.dart Added PieChartStackSegmentData class, extended PieChartSectionData with segments support, added totalRadius calculation, updated lerp and equality methods
lib/src/chart/pie_chart/pie_chart_painter.dart Refactored rendering logic from section-based to segment-based drawing, removed drawSection method, added drawSegments/drawSegment/generateSegmentPath methods, updated all radius calculations to use totalRadius
lib/src/utils/lerp.dart Added lerpPieChartStackSegmentDataList function for segment animation support
test/chart/pie_chart/pie_chart_painter_test.dart Updated tests to match new segment-based API, changed method calls from drawSection to drawSegment, updated path generation tests
test/chart/pie_chart/pie_chart_renderer_test.mocks.dart Updated mocks to reflect new segment methods and removed obsolete drawSection mock
example/lib/presentation/samples/pie/pie_chart_sample4.dart Added new sample demonstrating stacked pie chart with single segment per section
example/lib/presentation/samples/chart_samples.dart Registered PieChartSample4 in the samples list

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

Comment thread lib/src/chart/pie_chart/pie_chart_data.dart Outdated
Comment thread lib/src/chart/pie_chart/pie_chart_data.dart Outdated
Comment thread lib/src/chart/pie_chart/pie_chart_data.dart Outdated
Comment thread lib/src/utils/lerp.dart
@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch from 271d231 to ca2f3a3 Compare March 2, 2026 14:19
@jay-k98

jay-k98 commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

@imaNNeo Thank you for responding. I updated the branch and applied the suggested changes of the copilot code-review.

@jay-k98 jay-k98 changed the title Feature: Add StackedPieChart feat: Add StackedPieChart Mar 2, 2026
@jay-k98 jay-k98 changed the title feat: Add StackedPieChart feat: Add segments to PieChart Mar 3, 2026
@imaNNeo

imaNNeo commented Mar 13, 2026

Copy link
Copy Markdown
Owner

Thanks for the fixes,
I had a closer look now. And there's something that doesn't feel right to me.

We have the PieChartSectionData.radius and also segments (where each segment has PieChartStackSegmentData.radius

Now they're adding to determine the final radius of the pie-section shape.
But what I was assuming is like:
PieChartSectionData.radius -> determines the total radius of the section
Then we can have a list of:
PieChartStackSegmentData -> that each one determines the from/to radius of the stacked segment.

Just like how stacking works in BarChart (Look at BarChartSample4 for example)
image
In BarChart, we define the main bar's toY, then each stack item can have a fromY and toY. We render the main bar behind, then we render the stacked parts on top of that based on the fromY and toY

I would like to have a similar functionality here, so in that way, we can have PieChartSectionData.radius which determines the total radius of the section, and we render it behind, then we will have a list of segments that each one has a fromRadius and toRadius, then we render them on top of the base section.
(Keep in mind to clamp the numbers based on the valid radius that we can have (0 to PieChartSectionData.radius)

So basically, segments is a list that we render on top of the main section!

This way:

  1. We have the consistent logic for our stacked items in different charts
  2. We have more customizability (for example user can add a stack only in the middle of the main section

Sorry for giving this feedback so late, but this is something I realized now!
Please let me know your thoughts on it.

@jay-k98

jay-k98 commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

@imaNNeo I get your point and I like the idea of keeping it consistent. Also as you say it would lead to a more flexible solution, which I also appreciate. I will start refactoring and hit you up when I'm done.

@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch from ca2f3a3 to a3e7227 Compare March 17, 2026 13:34
@jay-k98

jay-k98 commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

@imaNNeo I updated the branch and commited the refactored segment logic. I really like it because it is much cleaner and easier to understand. Thanks a lot for your feedback! Segment spacing can be solved by adding a segment. I provided that trick in the samples.

Let me know if this is what you had in mind :)

@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch from a3e7227 to 839a611 Compare March 17, 2026 17:18
@imaNNeo

imaNNeo commented Apr 10, 2026

Copy link
Copy Markdown
Owner

Nice! Thanks for the changes. I also like the new structure. Just some minor comments to solve before merging it:

Comment thread lib/src/chart/pie_chart/pie_chart_painter.dart Outdated
@imaNNeo

imaNNeo commented Apr 10, 2026 •

Copy link
Copy Markdown
Owner

And please:

  • Add some unit-tests for the added classes and the functionality
  • Add the new property/feature in our documentation in pie_chart.md

After that, we're ready to go!

jay-k98 added 6 commits April 13, 2026 16:43
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
@jay-k98
jay-k98 force-pushed the feature/stacked-pie-chart branch from ff1023d to 578a084 Compare April 13, 2026 14:43
@jay-k98

jay-k98 commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

@imaNNeo Thanks for your feedback. I implemented reusing the path and added some tests for better test coverage as well as documentation. Ready for your approval :)

@imaNNeo

imaNNeo commented Apr 15, 2026

Copy link
Copy Markdown
Owner

Perfect!
Thanks for your contribution!

@imaNNeo imaNNeo changed the title feat: Add segments to PieChart feat: add stacked segment support for PieChart sections Apr 15, 2026
@imaNNeo imaNNeo changed the title feat: add stacked segment support for PieChart sections feat: Add stacked segment support for PieChart sections Apr 15, 2026
@imaNNeo
imaNNeo merged commit c379032 into imaNNeo:main Apr 15, 2026
8 checks passed
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.

4 participants