This topic provides information for configuring the XamPieChart™ in order to use an ItemLegend.
This topic contains the following sections:
This article assumes you have already read the Data Binding topic and have a pie chart already bound to some data.
In order to display a legend next to the XamPieChart an ItemLegend needs to be created and assigned to the Legend property. The LegendLabelMemberPath can then be used to specify which property on your data model it will use to display inside the legend for each pie slice.
Additionally you can use the LegendItemTemplate and LegendItemBadgeTemplate properties and the various font properties on ItemLegend to further customize the look of the legend items. Here is a list of the properties which affect the legend item visuals:
In XAML:
<Grid x:Name="RootLayout"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <ig:XamPieChart x:Name="pieChart" Grid.Column="1" ItemsSource="{Binding Path=EnergyData}" ValueMemberPath="Coal" LabelMemberPath="CountryAbbreviation" Legend="{Binding ElementName=itemLegend1}"> <ig:XamPieChart.LegendItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Margin="1" Visibility="{Binding Series.Visibility}"> <ContentPresenter Content="{Binding}" ContentTemplate="{Binding Series.LegendItemBadgeTemplate}"/> <TextBlock Text="{Binding Path=Item.Country}" Foreground="{Binding Series.Legend.ItemsForeground}" FontStretch="{Binding Series.Legend.ItemsFontStretch}" FontStyle="Italic" FontWeight="Bold" FontSize="16" FontFamily="MS Arial"/> </StackPanel> </DataTemplate> </ig:XamPieChart.LegendItemTemplate> </ig:XamPieChart> <ig:ItemLegend x:Name="itemLegend1" Grid.Column="0"/> </Grid>
In C#:
// Get the DataTemplate from where ever you have it defined. DataTemplate customLegendItemTemplate = this.Resources["CustomLegendItemTemplate"] as DataTemplate; ItemLegend legend = new ItemLegend(); XamPieChart pieChart = new XamPieChart(); pieChart.ItemsSource = EnergyData; pieChart.ValueMemberPath = "Coal"; pieChart.LabelMemberPath = "CountryAbbreviation"; pieChart.LegendItemTemplate = customLegendItemTemplate; pieChart.Legend = legend;
In Visual Basic:
'Get the DataTemplate from where ever you have it defined. Dim customLegendItemTemplate As DataTemplate customLegendItemTemplate = TryCast(Me.FindResource("CustomLegendItemTemplate"), DataTemplate) Dim legend As New ItemLegend() Dim pieChart As New XamPieChart() pieChart.ItemsSource = EnergyData pieChart.ValueMemberPath = "Coal" pieChart.LabelMemberPath = "CountryAbbreviation" pieChart.LegendItemTemplate = customLegendItemTemplate; pieChart.Legend = legend