Approved
Last Updated: 25 Apr 2019 08:18 by ADMIN
Michael
Created on: 09 Apr 2019 10:14
Category: Chart
Type: Feature Request
1
RadPieChart - Programmatic selection

Hi there,

 

I would like to have support for selecting a slice of the piechart from code-behind. I have have looked into the chart and series properties but found no selectable datapoints property. The chartSelectionBehavior offers a ClearSelection method and the readonly SelectedPoints property, but nothing to add a selected point.

 

3 comments
ADMIN
Stefan Nenchev
Posted on: 25 Apr 2019 08:18
Hello, Michael,

I am glad the solution was helpful. Thank you for the binding suggestion as well, we will consider it once we start working on the feature request.

Regards,
Stefan Nenchev
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Michael
Posted on: 18 Apr 2019 11:37

Hi Stefan,

 

your suggested solution works like a charm. If this would be introduced as a simple binding feature for the chart it will be even more convenient for future developments.

 

Thanks a lot!

ADMIN
Stefan Nenchev
Posted on: 12 Apr 2019 07:35
Hello Michael,

Thank you for submitting the feature request. We will consider adding the feature in Xamarin.Forms. In the meantime, I reviewed the discussion you had with Lance in the support ticket and it seems that there is another approach you can take. Basically, you can create renderers for the different platforms and programmatically select the different pie slices through the respective native control API. I am here providing the approach for Android so that you can easily get the idea:

1. Create a custom PieChart and expose an additional bindable property to control the selected item:

public class PieChartSelectable : RadPieChart
 {
 
     public static readonly BindableProperty SelectedItemIndexProperty =
         BindableProperty.Create("SelectedItemIndex", typeof(int), typeof(PieChartSelectable), -1);
 
     public int SelectedItemIndex
     {
         get { return (int)GetValue(SelectedItemIndexProperty); }
         set { SetValue(SelectedItemIndexProperty, value); }
     }
 
 }

2. Track when this property is changed in the renderer and select the respective slice:

class PieChartSelectableRenderer : PieChartRenderer
{
    public PieChartSelectableRenderer(Context context) : base(context)
    {
    }
 
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (e.PropertyName == nameof(PieChartSelectable.SelectedItemIndex))
        {
            this.SelectDataPoint((this.Element as PieChartSelectable).SelectedItemIndex);
        }
    }
 
    private void SelectDataPoint(int index)
    {
        var dataPoints = (this.Control.Series.Get(0) as PieSeries).DataPoints();
        for (int i = 0; i < dataPoints.Size(); i++)
        {
            DataPoint currentPoint = (DataPoint)dataPoints.Get(i);
            currentPoint.IsSelected = false;
        }
        (dataPoints.Get(index) as DataPoint).IsSelected = true;
    }
}

I have attached a sample for your reference where the functionality is also added for iOS and UWP.

Have a great weekend.

Regards,
Stefan Nenchev
Progress Telerik
Attached Files: