getting data values from SelectionChanged event

Hi

I'm using SplineSeries in Xamarin Forms for iOS and Android and I'm trying to understand how to get the values from the SelectionChanged event.  When the user clicks on a point in the graph I want to retrieve the x and y data values.  I can get the index of the graph, but can't see the function to get the data values.

Any guidance would be appreciated

Thanks

5 Replies

HM Hemalatha Marikumar Syncfusion Team February 25, 2020 12:47 PM UTC

Hi Rob, 
 
Greetings from Syncfusion. 
 
Query: How to get the X, Y axis value from SelectionChanged event? 
 
We would like to let you know that your requirement “Get the selected segment’s model” has been achieved as per in below code snippet. 
 
Private void Chart_SelectionChanged(object sender, ChartSelectionEventArgs e)
        {
            IList items = e.SelectedSeries.ItemsSource as IList;
 

            Model selectedDatapoint = items[e.SelectedDataPointIndex] as Model;
 
        } 
 
 
Note: Please make sure on monitoring the SelectedDataPointIndex value while getting the model since it returns -1 while selecting the same segment again. 
 
Regards, 
Hemalatha M. 



RO Rob March 31, 2020 02:27 PM UTC

Hi

thanks for your reply.  I spent some time with it and it revealed other problems with my app.  So apologies for the delay in replying.  

Now I've fixed those problems, I've come back to look at this.  It seems that e.SelectedDataPointIndex is always -1.

My XAML looks like this:
 <chart:SfChart x:Name="sfchart" BackgroundColor="#f5f6fa"
                               EnableSeriesSelection="True" SelectionChanging="Chart_SelectionChanging"
                              Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
 <chart:SplineSeries StrokeWidth="5" EnableDataPointSelection="True" ItemsSource="{Binding Data}" XBindingPath="Date" YBindingPath="Mood" Label="Mood"/>

And my codebehind looks like this:
 async void Chart_SelectionChanging(object sender, ChartSelectionChangingEventArgs e)
        {
           TitleLabel.Text = e.SelectedDataPointIndex.ToString();
            if (e.SelectedDataPointIndex > -1)
            {
                IList items = e.SelectedSeries.ItemsSource as IList;
                Model selectedDatapoint = items[e.SelectedDataPointIndex] as Model;
                await Navigation.PushModalAsync(new SelectedDiaryView(selectedDatapoint));
            }
        }

I use that TitleLable.Text to show me how the Variable is changing.  As soon as the Chart_SelectionChanging event fires, e.SelectedDataPointIndex is immediately -1.  Is there something missing in my XAML setup maybe?

Thanks


DD Devakumar Dhanapoosanam Syncfusion Team April 1, 2020 03:07 PM UTC

 
Thanks for your update. 
 
We have analyzed your query and provided code snippet. We would like to suggest that you can resolve the reported issue and achieve your requirement by enabling only the EnableDataPointSelection and removing the SeriesSelection as per in below code snippet.  
 
XAML: 
<chart:SfChart SelectionChanged="SfChart_SelectionChanged"       
               SelectionChanging="SfChart_SelectionChanging"> 
       …. 
        <chart:SplineSeries ItemsSource="{Binding Data}" 
                       XBindingPath="Month" YBindingPath="Target" 
                       EnableDataPointSelection="True"> 
       <chart:SplineSeries.DataMarker> 
           <chart:ChartDataMarker/> 
       </chart:SplineSeries.DataMarker> 
       </chart:SplineSeries> 
</chart:SfChart> 
 
C#: 
private void SfChart_SelectionChanging(object sender, ChartSelectionChangingEventArgs e) 
{ 
            if (e.SelectedDataPointIndex > -1) 
            { 
               ... 
            } 
} 
 
private void SfChart_SelectionChanged(object sender, ChartSelectionEventArgs e) 
{ 
            if (e.SelectedDataPointIndex > -1) 
            { 
                ... 
            } 
} 
 
Please refer the below UG for more details, 
 
 
Note: We would like to let you know that while enabling both SeriesSelection and EnableDataPointSelection high priority will be considered for the SereiesSelection and selected series data point will not get considered. Hence, we suggest the above solution. 
 
Please let us know whether the above solution resolves your requirement or not.  
 
Regards, 
Devakumar D 



RO Rob April 1, 2020 06:22 PM UTC

Hi

That fixed my problem.  Many thanks.  I don't need both EnableDataPointSelection and  SeriesSelection enabled.  The examples I read were for pie charts and I misunderstood how to translate them to the spline chart.  

But I've used your answer to fix my code and it's doing what I need now.  

Cheers


DD Devakumar Dhanapoosanam Syncfusion Team April 2, 2020 05:36 AM UTC

Hi Rob, 
 
Thanks for your update. 
 
We are glad to hear that the provided solution works. 
 
If you need any further assistance, please don't hesitate to contact us. 
 
Regards, 
Devakumar D 


Loader.
Up arrow icon