Detect which pie wedge is selected?

Posted by: robkerr on 9 September 2017, 8:21 am EST

  • Posted 9 September 2017, 8:21 am EST

    For my current app I have a requirement to take some action when a wedge of a pie chart is selected. I'm rather at a loss of how to do it...

    Starting with the "Selection.xaml" from the charting sample project, I can see that the chart control "knows" which wedge is selected, and can apply the appropriate style to that wedge.

    What I really need is to *also* know which wedge is selected. I can trap the Tapped event from the chart or (seems more appropriate) from the series, but I'm not able to determine which point (wedge in pie chart terms) was selected to trigger that event.

    How can this be done?

    Thanks!
    Rob
  • Replied 9 September 2017, 8:21 am EST

    Hi Rob,

    You have two options. The Selection.xaml demo is probably not what you need. This demo just shows what happens when you bind C1Chart to a collection view and bind a selected data point to the CurrentItem.

    In your scenario look at these options:

    1. Use a method in C1Chart to get the plot element closest to the mouse. Here is a better explanation. This blog post is for WPF but you can use the same code just use Tapped rather than Mouse events.
    http://our.componentone.com/2010/08/11/adding-more-interaction-to-c1chart-drag-points/

    2. Use PlotElementLoaded event, and attach Tapped event to individual plot element. This event is also demonstrated in the blog post above


    XYDataSeries ds = new XYDataSeries();
    ds.PlotElementLoaded += new EventHandler(ds_PlotElementLoaded);


    void ds_PlotElementLoaded(object sender, EventArgs e)
    {
    var pe = (PlotElement)sender;
    pe.Tapped += pe_Tapped
    }

    void pe_Tapped(object sender, TappedRoutedEventArgs e)
    {
    //Capture plot element clicked on
    PlotElement currentPE = (PlotElement)sender;

    // get data point from plot element
    MyObject o = pe.DataPoint.DataObject as MyObject;
    }


    Let us know if you need more help with your project,

    Regards
    Greg
  • Replied 9 September 2017, 8:21 am EST

    Thanks again, Greg, for the pointer in the right direction. The option #2 was exactly what I was looking for, and I was able to implement it very quickly.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels