How to Create Custom Legend Items in WPF Chart?
This article describes how to define the custom legend items with view model data.
Custom legend items are achieved by adding required LegendItem count to ChartLegend’s Items property with Item, Series, IconHeight, IconWidth, IconVisibility, Interior and ItemTemplate as per the below code snippet
[XAML]
<DataTemplate x:Key="itemTemplate"> <StackPanel Margin="10,0,10,0" Orientation="Horizontal"> <CheckBox Margin="2" Tag="{Binding}" IsChecked="True" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/> <Rectangle Width="{Binding IconWidth}" Height="{Binding IconHeight}" Fill="{Binding Interior}" Margin="2"/> <TextBlock HorizontalAlignment="Center" Margin="5,0,0,0" VerticalAlignment="Center" Text="{Binding Item.XValue}"> </TextBlock> </StackPanel> </DataTemplate>
[C#]
ChartLegend legend = new ChartLegend(); foreach (var series in chart.Series) { var data = series.ItemsSource as ObservableCollection<Model>; for (int i = 0; i < data.Count; i++) { legend.Items.Add(new LegendItem() { Item = data[i].XValue.ToString(), Series = series, IconHeight = 10, IconWidth = 10, IconVisibility = Visibility.Visible, Interior = series1.ColorModel.GetBrush(i), CheckBoxVisibility = Visibility.Visible }); } } legend.ItemTemplate = grid.Resources["itemTemplate"] as DataTemplate; chart.Legend = legend;
private void CheckBox_Checked(object sender, RoutedEventArgs e) { CheckBox box = sender as CheckBox; LegendItem item = box.Tag as LegendItem; UpdateLegend(true, item.Item as Model); } private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { CheckBox box = sender as CheckBox; LegendItem item = box.Tag as LegendItem; UpdateLegend(false, item.Item as Model); } private void UpdateLegend(bool isChecked, Model item) { foreach (PieSeriesExt series in chart.Series) { if (isChecked) toggleIndexes.Remove(viewModel.Data.IndexOf(item)); else toggleIndexes.Add(viewModel.Data.IndexOf(item)); series.GetType().GetField("ToggledLegendIndex", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(series, toggleIndexes); } UpdateArea(); } private void UpdateArea() { MethodInfo info = chart.GetType().GetMethod("UpdateArea", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(bool) }, null); info?.Invoke(chart, new object[] { true }); }
Output
Download complete sample here.
Conclusion
I hope you enjoyed learning about how to create custom legend items in WPF Chart.
You can refer to our WPF Charts page to know about its other groundbreaking feature representations. You can also explore our WPF Chart Documentation to understand how to manipulate data.
For current customers you can check out on our WPF components from the License and Download page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WPF Chart and other WPF components.
If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!