This is an old revision of the document!


Charting

Required

  • WPF Toolkit
  • Assembly reference System.Windows.Controls.DataVisualization.Toolkit.

Create a Pie Chart:

<Window x:Class="WpfChartControl.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <charting:Chart x:Name="chart" Width="350" Height="250">
            <charting:Chart.Series>
                <charting:PieSeries ItemsSource="{Binding}"
                    DependentValuePath="Value"
                    IndependentValuePath="Key"
                    Title="Pet Preference" IsSelectionEnabled="True" />
            </charting:Chart.Series>
        </charting:Chart>
    </Grid>
</Window>

Populate Pie Chart in C#:

chart.DataContext = new KeyValuePair<string, int>[] {
                    new KeyValuePair<string, int>("Dog", 30),
                    new KeyValuePair<string, int>("Cat", 25),
                    new KeyValuePair<string, int>("Rat", 5),
                    new KeyValuePair<string, int>("Hampster", 8),
                    new KeyValuePair<string, int>("Rabbit", 12) };

Source: Chart Controls for WPF ship in the Toolkit