One DataSource,Many SeriesMappings

4 posts, 1 answers
  1. Kelley Wong
    Kelley Wong avatar
    21 posts
    Member since:
    May 2010

    Posted 08 Mar 2011 Link to this post

    I have a datasource like this(the PCName is variational)

    Year             PCName               Price
    2009             Dell                       100
    2009             Lenovo                  95.4
    2009             ASUS                    85
    2008             Dell                      130
    2008            Lenovo                 75.4
    2008            ASUS                     81.5
    2010            Dell                       95.0
    2010            Lenovo                 95.4
    2010            ASUS                    97

    ChartControl.SeriesMappings.Clear();
    SeriesMapping BarMapping = new SeriesMapping();
    BarSeriesDefinition BarDefinition = new BarSeriesDefinition();
    BarDefinition.ShowItemLabels = true;
    BarDefinition.ShowItemToolTips = true;
    BarDefinition.LabelSettings.ShowZeroValueLabels = false;
    BarLabelSettings BarLabelSettings = new BarLabelSettings(LabelDisplayMode.MidPoint);
    BarLabelSettings.ShowZeroValueLabels = false;
    BarDefinition.LabelSettings = BarLabelSettings;
    BarMapping.SeriesDefinition = BarDefinition;
    BarMapping.ItemMappings.Add(new ItemMapping("Price", DataPointMember.YValue));
    BarMapping.ItemMappings.Add(new ItemMapping("Year", DataPointMember.XCategory));
    BarMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("PCName"));
    ChartControl.SeriesMappings.Add(BarMapping);

    i set the SeriesMappings or chart control code behind above,and it works fine!

    But now i want to set different SeriesMapping ,  first year data use BarSeriesDefinition ,and second year data use  LineSeriesDefinition ,third year another ,forth another

    can i do it code behind use the same datasource

  2. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 10 Mar 2011 Link to this post

    Hello Kelley Wong,

    You can set multiple SeriesMappings that use the same data from the ItemsSource. Each series mapping will create a new series for your chart. You can take a look on Grouping and Aggregation online demo here that uses similar approach.

    Best regards,
    Yavor Ivanov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. Kelley Wong
    Kelley Wong avatar
    21 posts
    Member since:
    May 2010

    Posted 10 Mar 2011 Link to this post

    in clearly ,i want to set the 2009 data use BarSeriesDefinition ,and 2010 data use LineSeriesDefinition and 2008 data use another series in one chart
  4. Answer
    Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 15 Mar 2011 Link to this post

    Hi Kelley Wong,

    You can try filtering your data by the year field and displaying your data with PCname as XCategory. The code for creating the series mappings can look like this:

    // Bar series (2009)
    SeriesMapping BarMapping = new SeriesMapping() { LegendLabel = "2009" };
    BarMapping.SeriesDefinition = new BarSeriesDefinition()
    {
        ShowItemLabels = true,
        ShowItemToolTips = true,
        LabelSettings = new BarLabelSettings(LabelDisplayMode.MidPoint) { ShowZeroValueLabels = false }
    };
    CreateItemMappings(BarMapping.ItemMappings);
    FilterData(BarMapping, 2009);
     
    collection.Add(BarMapping);
     
    // line series (2010)
    SeriesMapping LineMapping = new SeriesMapping() { LegendLabel = "2010" };
    LineMapping.SeriesDefinition = new LineSeriesDefinition()
    {
        ShowItemLabels = true,
        ShowItemToolTips = true
    };
    CreateItemMappings(LineMapping.ItemMappings);
    FilterData(LineMapping, 2010);
     
    collection.Add(LineMapping);
     
    // line series (2008)
    SeriesMapping LineMapping2 = new SeriesMapping() { LegendLabel = "2008" };
    LineMapping2.SeriesDefinition = new LineSeriesDefinition()
    {
        ShowItemLabels = true,
        ShowItemToolTips = true
    };
    CreateItemMappings(LineMapping2.ItemMappings);
    FilterData(LineMapping2, 2008);
     
    collection.Add(LineMapping2);

    Filtering data can be achieved by using RadChart build-in filtering functionality. Here is the sample code:
    private void FilterData(SeriesMapping mapping, int year)
    {
        mapping.FilterDescriptors.Add(new ChartFilterDescriptor("Year", typeof(Int32), Telerik.Windows.Data.FilterOperator.IsEqualTo, year));
    }

    Finally here is the code for mapping your data to your series:
    private void CreateItemMappings(ItemMappingCollection collection)
    {
        collection.Add(new ItemMapping("Price", DataPointMember.YValue));
        collection.Add(new ItemMapping("PCName", DataPointMember.XCategory));
    }

    You can find all of the above code in the attached files.

    Hope this helps!


    All the best,
    Yavor Ivanov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top