Single selection in pie chart or donut chart


#1

I want only one data point to be selected at a time in my chart (the previously selected data point should be deselected. How can I achieve this?

Is there a property somewhere that sets this behaviour? Or do I have to manually set the Selected property of all other data points to 0/false when a new one is selected?


#2

As far as I can tell, you have to tell slices to select or deselect. You can see an example with the project file in the example they have in this post. They go through all the slices of the pie and deselect the slice, if it doesn’t match the selected index.

http://www.shinobicontrols.com/blog/posts/2012/09/10/the-making-of-a-legend/

The code segment with selection:

for (int j=0; j < sliceCount; j++) {
            
            SChartLegendSymbol *clickedSymbol = [pieChart.legend.symbols objectAtIndex:j];
            UILabel *clickedLabel = [pieChart.legend.labels objectAtIndex:j];                       
            
            if (j == selectedIndex) {
                [series setSlice:j asSelected:YES];
                [clickedLabel setTextColor:[series.style.flavourColors objectAtIndex:j]];
            } else {
                [series setSlice:j asSelected:NO];
                [clickedLabel setTextColor:[UIColor lightGrayColor]];
            }
}

 


#3

Thank you jghazarian,

This is how I implemented it in my chart delegate class (Xamarin.iOS code):

protected override void OnToggledSelection(ShinobiChart chart, SChartRadialDataPoint selectedDataPoint, SChartRadialSeries series, PointF pixelPoint)
        {
            if (!selectedDataPoint.Selected) return;

            for (var i = 0; i < series.DataSeries.DataPoints.Count; i++)
            {
                if (i != selectedDataPoint.Index)
                {
                    ((SChartDonutSeries) series).SetSliceAsSelected(i, false);
                }
            }
        }

#4

Awesome, thanks for sharing your final implementation. That should help for anyone checking this question.