Articles in this section
Category / Section

How to navigate to other page with data point information of selected data in Xamarin.Forms Chart?

1 min read

Chart provides selection support for data points on touch or by using SelectedDataPointIndex property of ChartSeries to programmatically select a data pooint. You can enable the selection behavior for any series using EnableDataPointSelection property. Chart’s SelectionChanged event will notify to the user when selection changed. This event contains selected index, previous selected index and selected series. Please refer the below code snippets to enable data point selection, hooking the SelectionChanged event and navigating to another page with the selected data point information when selection changed.

 

Code snippets for enabling data point selection:

 

Xaml

 

<chart:PieSeries EnableDataPointSelection="True" />

 

C#

 

pieSeries.EnableDataPointSelection = true;

                       

Code snippets to hook SelectionChanged event:

 

Xaml

 

<chart:SfChart SelectionChanged="Chart_SelectionChanged">
    . . .
</chart:SfChart>

 

C#

 

chart.SelectionChanged += Chart_SelectionChanged;

 

Code snippet to navigate to other page with the information of selected data point:

 

C#

 

private void Chart_SelectionChanged(object sender, ChartSelectionEventArgs e)
{
    if (e.SelectedDataPointIndex > -1)
    {
        var data = e.SelectedSeries.ItemsSource as ObservableCollection<Model>;
        Navigation.PushAsync(new TextPage() { Item = data[e.SelectedDataPointIndex] });
    }
}

 

By default, selected segment is highlighted with a color. You can stop applying the selected segment color by cancelling the selection before navigating to other page in SelectionChanging event. Please refer the below code snippet.

 

C#

 

private void chart_SelectionChanging(object sender, ChartSelectionChangingEventArgs e)
{
    if (e.SelectedDataPointIndex > -1)
    {
        var data = e.SelectedSeries.ItemsSource as ObservableCollection<Model>;
        Navigation.PushAsync(new TextPage() { Item = data[e.SelectedDataPointIndex] });
    }
    e.Cancel = true;
}

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied