January 12, 2011

Simple Pie Chart using WPF Toolkit

Here is more detail
Also look at WPF Toolkit Tutorial – Part 1

A simple pie chart example in that there only 2 wedges in the pie chart!
Add a reference to the WPFToolkit data visualisation assembly ("...\Program Files\WPF Toolkit\v3.5.50211.1\System.Windows.Controls.DataVisualization.Toolkit.dll")

Add following xaml to the window where the pie chart will be placed:
<Window ...
  <!-- First define the namespace for charting -->
  xmlns:charting="clr-namespace:
  System.Windows.Controls.DataVisualization.Charting;
  assembly=System.Windows.Controls.DataVisualization.Toolkit"

  <charting:Chart Name="pieChart">
    <charting:PieSeries ItemsSource="{Binding}" 
      IndependentValueBinding="{Binding Path=Description}"
   DependentValueBinding="{Binding Path=Percentage}"
    />
  </charting:Chart>
Set the Pie chart wedges
void AssignPieChartWedges()
{
    System.IO.DriveInfo cdrive = new System.IO.DriveInfo("C");
    double availPercentage = Math.Round(100.0d * 
        (double)cdrive.TotalFreeSpace / (double)cdrive.TotalSize);

    List<DrivePercentage> dpList = new List<DrivePercentage>();
    dpList.Add(new DrivePercentage() 
    { Percentage=availPercentage, Description="Free" });
    dpList.Add(new DrivePercentage() 
    { Percentage=100.0d-availPercentage, Description="Used" });
    pieChart.DataContext = dpList;
}

1 comment:

Harrison said...

Sweet and simple, thanks!

Here is the fill in class.


internal class DrivePercentage
{
public double Percentage
{ get; set; }
public string Description
{ get; set; }
}