How to increase the size of the tapped segment in the pie chart?
In this article, we explain how to increase the size of the tapped segment in a pie chart using the Flutter Cartesian chart.
The SfCircularChart widget has the onPointTap callback in the series and pointRadiusMapper property, which you can use to increase the size of the tapped segment.
Here onPointTap callback gets trigged when tapping on the segment, in this call back you can get the pointIndex, seriesIndex, viewportPointIndex, and dataPoints of the tapped segment. Then pointRadiusMapper is used to map the radius for each point from the data source, allowing you to render each segment with a different radius.
In the following steps explain how to increase the size of the tapped segment in the PieSeries.
Step 1: Initialize the variables chartData to store the data source, pointIndex to store the index value of the tapped segment, and radius which is mapped to the tapped slice. Then assign the values in the initState method.
int? pointIndex; late String radius; late List<SampleData> chartData; @override void initState() { radius = '140'; chartData = <SampleData>[ SampleData('1', 120), SampleData('2', 40), SampleData('3', 10), SampleData('4', 10), SampleData('5', 10), SampleData('6', 6), SampleData('7', 4), SampleData('8', 1), SampleData('9', 60), ]; super.initState(); } class SampleData { SampleData(this.x, this.y); final String x; final num y; }
Step 2: Create the SfCircularChart widget with the PieSeries and assign the chartData to the dataSource property and map the x, y values to xValueMapper, yValueMapper properties respectively.
SfCircularChart( series: <CircularSeries<SampleData, String>>[ PieSeries<SampleData, String>( dataSource: chartData, xValueMapper: (SampleData data, _) => data.x, yValueMapper: (SampleData data, _) => data.y) ], )
Step 3: Declare the onPointTap event in the SfCircularChart, assign the tapped segment index value from the event to the pointIndex variable. And set the radius value as per your requirement, then call setState method to make the changes reflected while tapping on the segment.
After that using the pointRadiusMapper set the radius value to the respective selected point with help of the pointIndex value. Here for the tapped segment we have assigned radius as 160 and 140 for the rest of the segments, you can change this based on your scenario.
SfCircularChart( series: <CircularSeries<SampleData, String>>[ PieSeries<SampleData, String>( onPointTap: (ChartPointDetails details) { pointIndex = details.pointIndex; radius = '160'; setState(() {}); }, pointRadiusMapper: (SampleData data, index) => radius != '' && index == pointIndex ? radius : '140', ], )
Thus, the tapped segment size will increase using the onPointTap callback and pointRadiusMapper properties.
Conclusion
I hope you enjoyed learning about how to increase the size of the tapped segment in the pie chart.
You can refer to our Flutter CartesianChart feature tour page to learn about its other groundbreaking feature representations. You can also explore our Flutter CartesianChart documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!