Silverlight 2.0 Pie Chart (Beta)
Posted by Peter McGrattan on May 18, 2008
Edited on 26/June/2008 : Code download and Demos updated to support Silverlight 2 Beta 2
In an attempt to demonstrate some of the powerful 2D graphics support in Silverlight 2.0, I recently developed a re-usable implementation of a pie chart. Having lately read some forum postings asking for a pie chart that works with Silverlight 2.0, I thought I’d publish what I’ve come up with.
Full source code and demos are available below, the following headings show some of the main features as well as examples of pie charts you can create :
1. Basic Silverlight 2.0 Pie Chart :

2. Supports Radial Gradient, Linear Gradient and Video segments :

3. How to create basic Pie Chart in XAML only :
<charts:PieChart x:Name="pieChart1" Center="150.0,150.0" Radius="100.0" Stroke="LightGray" StrokeThickness="2.0" FontSize="10"> <charts:PieSegment Percentage="35" Foreground="Orange" /> <charts:PieSegment Degrees="38" Foreground="Blue" /> <charts:PieSegment Degrees="72" Foreground="Green" /> <charts:PieSegment Degrees="101" Foreground="Red" /> <charts:PieSegment Degrees="23" Foreground="Purple" /> </charts:PieChart>
4. How to create basic Pie Chart in C# only :
PieChart pieChart1 = new PieChart() { Center = new Point(150.0, 150.0), Radius = 100.0, Stroke = new SolidColorBrush(Colors.LightGray), StrokeThickness = 2.0, FontSize = 10.0, Segments = new PieSegmentList() { new PieSegment() { Percentage = 35, Foreground = new SolidColorBrush(Colors.Orange) }, new PieSegment() { Degrees = 38, Foreground = new SolidColorBrush(Colors.Blue) }, new PieSegment() { Degrees = 72, Foreground = new SolidColorBrush(Colors.Green) }, new PieSegment() { Degrees = 101, Foreground = new SolidColorBrush(Colors.Red) }, new PieSegment() { Degrees = 23, Foreground = new SolidColorBrush(Colors.Purple) } } }; // XAML contains <Canvas x:Name="Canvas1" /> Canvas1.Children.Add(pieChart1);
5. Specify segment size in Degrees or as a Percentage :
![]()
6. View changes as you type in Visual Studio 2008 Silverlight 2.0 Designer :

7. Implemented using Dependency Properties and ObservableCollection :
The chart will re-draw at runtime when you specify a new Segments Collection or modify the Center or Radius properties for example.
// Chart will redraw at new size and location pieChart1.Radius = 150.0; pieChart1.Center = new Point() { X = 150.0, Y = 250.0 };
This provides support for databinding, animation of the chart. View the demos and download the code for more information.
8. Efficient drawing and smooth mouse over animation :
Chart is drawn efficiently using framework classes in the System.Windows.Shapes and System.Windows.Media namespaces such as Path, LineSegment, ArcSegment and TranslateTransform. MouseEnter animation is smooth, using a Storyboard in the System.Windows.Media.Animation namespace to animate the rendering of a TranslateTransform.
9. View running demos and download source :
Code Download for Silverlight 2 Beta 2
Let me know any thoughts, requests, questions etc by posting a comment, it will help prioritise my todo list and I’m only too happy to hear if this is useful to you or not.
May 19, 2008 at 1:30 pm
You know how to develop an gantt chart in silvrlight beta?
May 20, 2008 at 1:39 am
Hi Ganttchartseeker, There are several ways to create something simple like this in Silverlight 2.0. A Gantt chart is not dissimilar to a horizontal bar chart. One approach could use a StackPanel, Grid or ListBox control as a list where each item contains a simple rectangle or Usercontrol and possibly customize the Calendar control for the project schedule. This post from the silverlight.net forums may be of interest to you.
May 21, 2008 at 5:03 am
[...] Silverlight 2 Pie Chart: Peter McGrattan has posted a nice control and article that demonstrates how to use a new Silverlight charting control he has written. [...]
May 21, 2008 at 5:35 am
[...] Silverlight 2 Pie Chart: Peter McGrattan has posted a nice control and article that demonstrates how to use a new Silverlight charting control he has written. [...]
May 21, 2008 at 5:50 am
[...] Silverlight 2 Pie Chart: Peter McGrattan has posted a nice control and article that demonstrates how to use a new Silverlight charting control he has written. [...]
May 21, 2008 at 2:32 pm
[...] Peter McGrattan’s Blog [...]
May 22, 2008 at 12:01 am
[...] Источник: блог Питера МакГрэтена. [...]
May 22, 2008 at 10:19 pm
[...] Silverlight 2 Pie Chart: Peter McGrattan贴出了一个很好的控件和相应文章,示范如何使用他编写的一个新Silverlight图表控件。 [...]
May 22, 2008 at 11:28 pm
When I try to run the sample in Win2003/IE6, I get an silverlight error:
ErrorCode:5014, ParseError, illegal xml character, demo.xap, line 1, position 3
May 23, 2008 at 2:36 am
Hi Eric, I’ve tested both demos (basic and video) and building/running the Visual Studio 2008 solution on Win2003 with IE 6 and can confirm everything is working fine for me.
If other Silverlight 2.0 Beta 1 applications do not work on that machine then check you have the ‘Silverlight 2.0 Beta 1 Runtime’ installed properly, if not follow the steps here.
If the runtime is installed correctly, the problem could possibly be related to the locale you are using to view the demos. In Windows this setting is controlled by the Regional Options Control Panel applet and the Language Bar. In Silverlight Beta 1, the installation and control runtime are available in English. Silverlight Beta 1 does not support end to end application localization. This support will be introduced for Beta 2.
If the runtime is installed correctly and your English locale is supported by Silverlight 2.0 Beta 1, then have a look in the comments here and here for evidence that at least one Microsoft team is aware of ‘a known issue that generates this error code’ and that closing and restarting your browser appears to be a workaround.
Bear in mind that Silverlight 2.0 is in it’s first Beta and there will be problems; equally there will be time for problems like this to be resolved. Thanks for your feedback and for trying out the Pie Chart Beta!
May 23, 2008 at 5:57 pm
[...] Diagramas en Silverlight 2: Peter McGrattan ha posteado un control y un artículo en el que nos enseña a usar el control de diagramas para Silverlight que ha hecho. [...]
May 24, 2008 at 2:23 pm
[...] Silverlight 2 Pie Chart: Peter McGrattan has posted a nice control and article that demonstrates how to use a new Silverlight charting control he has written. [...]
May 25, 2008 at 10:04 am
[...] Silverlight 2 Pie Chart: Peter McGrattan has posted a nice control and article that demonstrates how to use a new Silverlight charting control he has written. [...]
May 27, 2008 at 5:54 am
Hi Peter,
Your control is Nice control, Thanks for posting this article.
I have some quries regarding to feteching data from SQL Server 2005 and give input to your Graph. Without using WCF, Can we not use LINQ.
Please help me to provide data from SQL server to SL control or your graph control.
I am waiting your reply
May 31, 2008 at 9:02 am
[...] Silverlight 2 Pie Chart: Peter McGrattan has posted a nice control and article that demonstrates how to use a new Silverlight charting control he has written. [...]
June 5, 2008 at 10:53 am
Hi Dattatray Patil, There are several tutorials available that show how to access data from SQL server 2005 in a Silverlight application as well as how to use LINQ, have a look here for a start.
Once you’ve retrieved your results, create the relevant PieSegment objects and add them to the Segments collection as shown in this post and the examples in the code download.
June 10, 2008 at 11:10 am
[...] Beta1 has obliged me to update links and all the samples previously posted on this blog such as the Pie Chart and Stock List Demo. From this point on all posts will target Silverlight 2 Beta [...]
June 15, 2008 at 3:25 pm
[...] Silverlight 2 Pie Chart: Peter McGrattan has posted a nice control and article that demonstrates how to use a new Silverlight charting control he has written. [...]
June 18, 2008 at 8:29 pm
Hi Peter,
Thank you for posting this article and making the source code available. I’ve been looking for something like this.
Keep up the good work.
Thanks