How to Hide the Series with WPF Chart Legend?
WPF Chart provides the ability to hide series using the ChartLegend. This functionality can be achieved by utilizing the ToggleSeriesVisibility or CheckBoxVisibility properties of the ChartLegend. This article explains in detail how to hide the series in the WPF Chart through legend.
Solution: 1 You can use the ToggleSeriesVisibility property of the chart legend to disable a series in WPF Chart by toggling the legend item.
[Xaml]
<chart:SfChart> … <chart:SfChart.Legend> <chart:ChartLegend ToggleSeriesVisibility="True"> <chart:ChartLegend.Header> <Label FontSize="15" Content="Subjects" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" /> </chart:ChartLegend.Header> </chart:ChartLegend> </chart:SfChart.Legend> <chart:ColumnSeries Label="Biology" ItemsSource="{Binding MathsData}" XBindingPath="StudentName" YBindingPath="Mark" > </chart:ColumnSeries> <chart:ColumnSeries Label="Chemistry" ItemsSource="{Binding PhysicsData}" YBindingPath="Mark" XBindingPath="StudentName" > </chart:ColumnSeries> </chart:SfChart>
[C#]
SfChart chart = new SfChart(); … ChartLegend legend = new ChartLegend() { ToggleSeriesVisibility= true, Header = new Label() { Content = "Subjects" FontWeight = FontWeights.Bold, FontSize = 15 HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, } }; ColumnSeries series1 = new ColumnSeries() { XBindingPath = "StudentName", YBindingPath = "Mark", ItemsSource = (new LegendsViewModel()).MathsData, Label = "Chemistry", }; ColumnSeries series2 = new ColumnSeries() { XBindingPath = "StudentName", YBindingPath = "Mark", ItemsSource = (new LegendsViewModel()).PhysicsData, Label = "Biology", }; chart.Series.Add(series1); chart.Series.Add(series2); chart.Legend = legend; this.Content = chart;
Output
Before toggling the series, it is possible to ensure that both series are visible.
After toggling the series, it is possible to ensure that both series are hidden.
Solution: 2 When the checkbox is deselected, the visibility of the chart legend's checkboxes will be collapsed and as a result, the series associated with it will also be hidden.
[Xaml]
<chart:SfChart> … <chart:SfChart.Legend> <chart:ChartLegend CheckBoxVisibility="Visible"> <chart:ChartLegend.Header> <Label FontSize="15" Content="Subjects" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center" /> </chart:ChartLegend.Header> </chart:ChartLegend> </chart:SfChart.Legend> … </chart:SfChart>
[C#]
SfChart chart = new SfChart(); … ChartLegend legend = new ChartLegend() { CheckBoxVisibility = Visibility.Visible, Header = new Label() { Content = "Subjects" FontWeight = FontWeights.Bold, FontSize = 15 HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, } }; … chart.Legend = legend; this.Content = chart;
Output
Before deselecting the check box, it is possible to ensure that both series are visible.
After deselecting the second check box, the second series is hidden.
See also
- How to control the visibility of all series with a single legend item in wpf charts
- How to create custom legend items in wpf chart
- How to change the series selection when clicking the legend items in wpf chart
Conclusion
I hope you enjoyed learning about how to hide the series with WPF Chart legend.
You can refer to our WPF Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!