Telerik blogs

Seamless operation with diverse data sources is paramount for any data visualization solution. RadChart already supports automatic data binding to IEnumerable sources as seen in this example: http://demos.telerik.com/silverlight/#Chart/DataBinding. With the growing of Silverlight data sources and support for various data services, however, a new scenario is becoming more common: data binding to a collection of collections or nested collections. I am glad to announce that a new feature is coming in the Q2 2009 release which will make this automatic.

Consider the following code snippet:

private void FillWithData()
{
   
var list1 = new List<double>() { 1, 3, 5, 7, 9 }; //Odd Numbers
    var list2 = new List<double>() { 2, 4, 6, 8, 10 }; //Even Numbers
    var combinedList = new List<double>[] { list1, list2 };
    
   
SeriesMapping seriesMapping1 = new SeriesMapping() { LegendLabel = "List1", CollectionIndex = 0 };
   
SeriesMapping seriesMapping2 = new SeriesMapping() { LegendLabel = "List2", CollectionIndex = 1 };
   
seriesMapping1.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.YValue });
   
seriesMapping2.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.YValue });
   
SeriesMappingCollection seriesMappings = new SeriesMappingCollection() { seriesMapping1, seriesMapping2 };

   
RadChart1.SeriesMappings = seriesMappings;
   
RadChart1.ItemsSource = combinedList;
}

This method shows how to set up RadChart’s series mappings to activate the feature. The first three lines are simply code for creating the nested data and inserting it into a parent collection (an array in this case). Next, we create our series mappings as usual, but now we specify a new property - “CollectionIndex” which basically tells RadChart that its ItemsSource is a collection of collections and maps each nested collection to a particular data series. The next two lines are pretty standard and should not be a surprise to RadChart veterans as they simply map the numeric data to the YValues of the series. Finally, we set the mappings and data to the chart and we’re done!

chart_databinding

Simple, isn’t it? Let us know what your most common business scenarios are and what data source you use with RadChart most often. As always we will try to make things easier for you.


About the Author

Vladimir Milev

Vladimir Milev is a developer manager.

Comments

Comments are disabled in preview mode.