Articles in this section
Category / Section

How to get a notification when the legend items are clicked in UWP Chart?

2 mins read

This article explains how to get a notification when the legend item is clicked in UWP Chart.

You can notify the legend toggling by anyone of the following solutions:

Solution 1

 

You can get a legend item when a LegendItem is clicked using the PointerPressed event of ChartLegend and get the information of the corresponding LegendItem in PointerRoutedEventArgs.

 

<chart:SfChart >
 
        <chart:SfChart.Legend>
            <chart:ChartLegend PointerPressed="ChartLegend_PointerPressed">
            </chart:ChartLegend>
        </chart:SfChart.Legend>
 
        <chart:SplineSeries XBindingPath="Year"
                            Label="India"
                            ItemsSource="{Binding List}"
                            YBindingPath="India"
                            ShowTooltip="True">
        </chart:SplineSeries>
        <chart:SplineSeries 
                            XBindingPath="Year"
                            Label="Germany"
                            ItemsSource="{Binding List}"
                            YBindingPath="Germany"
                            ShowTooltip="True">
        </chart:SplineSeries>
        <chart:SplineSeries 
                            XBindingPath="Year"
                            Label="Japan"
                            ItemsSource="{Binding List}"
                            YBindingPath="Japan"
                            ShowTooltip="True">
        </chart:SplineSeries>
        <chart:SplineSeries 
                            XBindingPath="Year"
                            Label="America"
                            YBindingPath="America"
                            ItemsSource="{Binding List}"
                            ShowTooltip="True">
        </chart:SplineSeries>
    </chart:SfChart>

 

    public sealed partial class MainPage : Page
    {
…….
        private void ChartLegend_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
                        var element = e.OriginalSource as FrameworkElement;
            LegendItem legendItem = element.DataContext as LegendItem;
 
            if (legendItem != null)
            {
                var series = legendItem.Series;
            }
        }
    }

 

Solution 2

 

You can get the notification in view model by binding the Boolean property from the view model to the associated series IsSeriesVisible property. In this way, when the series legend is clicked, IsSeriesVisible property of view model will be notified.

<syncfusion:LineSeries IsSeriesVisible="{Binding IsSeriesVisible, Mode=TwoWay}" ItemsSource="{Binding Data}" XBindingPath="XValue" YBindingPath="YValue" Label="LineSeries" />

 

    public class ViewModel : INotifyPropertyChanged
    {
        private bool isSeriesVisible = true;
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public bool IsSeriesVisible
        {
            get { return isSeriesVisible; }
            set
            {
                isSeriesVisible = value;
                OnPropertyChanged();
            }
        }
 
        private void OnPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

 

See also

 

How to get a notification when the legend items are clicked in WPF Chart

 

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