Telerik blogs

Howdy folks! The RadControls for XAML Q3 Beta1 is out and it features an updated version of the RadChart control. We receive dozens of feature requests every day and one of the more substantial ones is the ability to analyze data with RadChart. The feature I will describe today is a great step in that direction. I’m talking of course about data grouping and aggregation.

 

Just like grouping and aggregate functions are one of the major features of our GridView control they can be used with the charts as well (if you have the beta). Basically the feature allows you to group business objects according to some criteria and calculate aggregate functions on those groups. That ability is surprisingly useful and a very powerful way to build charts that let you summarize business data in seconds.

 

Let’s take a look at the data that we want to break down in the RadChart. The table below shows the properties of our business object and their types. The second row is just a sample record showing what kind of records we have.

 

Year (int)

Quarter (string)

Make (string)

Region (string)

Sales (double)

2008

Q1 Toyota Europe 122

 

Now let’s look what that data looks like when we have no grouping or aggregations enabled. The only ItemMapping we have created for this chart is the following:

seriesMapping1.ItemMappings.Add(new ItemMapping("Sales", DataPointMember.YValue));
It simply maps all Sales data to the YValues of the diagram.

 

chart1

 

Now that’s not much help is it? Although the data plotted is mostly unintelligible we can see there is some sort of pattern to it. Let’s explore what we can do with some of that new grouping goodness. Let’s group the data by Year and Make and see if we can better visualize the relations in the data:

 

seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Year"));
seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Make"));

Here’s what we get:

chart2

 

Aha! We’re on to something. Already it is much more clear what’s going on. The year 2008 obviously has much better sales for both manufacturers. Also, we can compare how each company is doing against its competition each year. But we want more! Let’s say we are the CEO of one of the companies and we are interested in the BIG picture. We’ll use some aggregation functions for that. Here is how to do it:

 

seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Make"));
seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Year"));
seriesMapping1.ItemMappings.Add(new ItemMapping("Year", DataPointMember.XCategory));
seriesMapping1.ItemMappings.Add(new ItemMapping("Sales", DataPointMember.YValue, ChartAggregateFunction.Sum));
 

Notice how we are using the new constructor of the ItemMapping class which allows us to tell RadChart that we are not interested in Sales value of each record, but rather the sum of all records in each data group. Also, we are adding an additional ItemMapping to split the data in two categories – one for each year. Here is the final result:

 

chart3

 

Now this is some first-class BI behavior built-in the control. There is absolutely no messing with the data before feeding it to the control. Everything is done internally with the exceptional speed and quality of the RadGridView data engine.

 

But wait. There is more. Let’s try to obtain something much more complex. Say we want to see a break down per region of how both competing companies are doing every year using a stacked series and stack series groups:

seriesMapping1.SeriesDefinition = new StackedBarSeriesDefinition();
seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Make"));
seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Year"));
seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Region"));
seriesMapping1.StackGroupFieldName = "Make";
seriesMapping1.ItemMappings.Add(new ItemMapping("Region", DataPointMember.XCategory));
seriesMapping1.ItemMappings.Add(new ItemMapping("Sales", DataPointMember.YValue, ChartAggregateFunction.Sum));

chart4

 

In the above graph you can see the data broken down by Regions, Companies and Years. For example we will now be able to tell how Toyota is competing to Tata in Europe on a Year-By-Year basis AND for the whole period at the same time. This is possible thanks to the specifics of our Stacked Bar series and the ability to assign a different StackGroup for each company. This is a pretty complex data query by all standards and it is now possible to do in RadChart out-of-the-box with less then 8 lines of code!

 

I sincerely hope you guys find this new grouping and aggregation functionality in RadChart useful. You can play with it right now by downloading the beta!


About the Author

Vladimir Milev

Vladimir Milev is a developer manager.

Comments

Comments are disabled in preview mode.