:::: MENU ::::

How to use Telerik Metro RadPieChart control

When I first started developing for Windows 8 platform (XAML+C#) more than 2 months ago, I noticed that some of the controls that one would expect to see on a serious platform were missing. Mainly, I was missing charting controls and DatePicker control. I never expected the SDK to include charting controls, because I’ve never encountered those as a part of the official Microsoft SDKs, but controls such as a DatePicker should’ve been included for XAML developers, too (apparently, one is available for HTML devs). Luckily, I discovered Telerik Metro controls. Let’s see how to use the RadPieChart control and bind it to a ViewModel.

As I said, I first discovered Telerik controls while they were in the preview mode. Back then it was only available if you requested it via e-mail and waited a few days to be allowed in the program. Now they released the public beta with a much much better demo app and forum support. In the preview version, I had some trouble using the RadPieChart control to bind it to my ViewModel. The lack of documentation and initially support made it impossible to figure it out. Luckily, their forum is great so I got support after all, and now I’d like to share the solution with you.

Download the Telerik beta controls from their website:

http://www.telerik.com/products/windows-metro/overview.aspx

Install it, after which you’ll have the binaries available for your projects. Add the binaries to the project.

Let’s assume that you have a class that defines your model that implements INotifyPropertyChanged,  called AssetData:

RadPieChart will show percentages of some titles/labels in a classic pie chart. If you don’t know what a pie chart is, read more about it here:

http://en.wikipedia.org/wiki/Pie_chart

Every part of the pie chart has a certain title (or a label). This explains the model, which consists of two properties: percentage and a label. To be able to bind it, a ViewModel needs to have an ObservableCollection of AssetData objects:

We will create some random values of percentages. The MainViewmodel needs to have the following headers included:

Next, open the MainPage.xaml page. Add the following namespace to the list of namespaces:

Add the ViewModel to the page resources:

Set the DataContext of the main grid:

Create the RadPieChart in markup inside the grid and set the default palette:

DefaultPalette is set in the ViewModel to the DefaultLight palette already existing in the Telerik controls. There is also a palette called DefaultDark, or you can define your own palette. Add the PieSeries (series of pie char data elements) in markup, set the radius to some value (for example, 0.95), and bind ItemsSource to Data ObservableCollection

Now we need to set values and labels binding. First the values:

We bind value to Percentage properties of  AssetData object. Now, add LabelDefinitions:

Labels will bind to Label property of AssetData objects, and have a small margin set to stay separated from the pie chart.

Result?

A beautiful PieChart (a bit large, but you can modify RadiusFactor property to set it the way you want!)

image

Thanks Telerik for great support even for the preview version of controls!

Share the knowledge

One Comment

So, what do you think ?