Is it possible to use grouping in nested collections.

4 posts, 0 answers
  1. Alexey Oyun
    Alexey Oyun avatar
    74 posts
    Member since:
    Mar 2010

    Posted 04 Aug 2010 Link to this post

    I want to use nested collections for different series types.
    So that I can get lines and bars in one chart.

    I have attached screenshot of what I got.
    First two charts are using collection of DataPoints
    Rest use collection of collections.
    As you can see 1st and 3rd charts look similar except legend for 3rd chart is given from series mapping and thats fine.
    But 2nd and 4th charts are different.
    What I want is to be able to group nested collections.

    Below is code snipped for this example.

    XAML:
    <my:RadChart x:Name="chartGrouping1" Height="150"/>
    <my:RadChart x:Name="chartGrouping2" Height="150"/>
    <my:RadChart x:Name="chartGrouping21" Height="150"/>
    <my:RadChart x:Name="chartGrouping22" Height="150"/>

    C#:
    private void Chart_Loaded(object sender, RoutedEventArgs e)
        {
            InitGroupingCharts();
            InitNestedGroupingCharts();
        }
     
        private List<DataPoint> GetGroupData()
        {
            List<DataPoint> groupData = new List<DataPoint>();
            groupData.AddRange(GetAppleGroupData());
            groupData.AddRange(GetBananaGroupData());
            return groupData;
        }
     
        private List<DataPoint> GetBananaGroupData()
        {
            List<DataPoint> bananaGroupData = new List<DataPoint>();
            bananaGroupData.Add(new DataPoint { LegendLabel = "Banana", XCategory = "Europe", YValue = 186 });
            bananaGroupData.Add(new DataPoint { LegendLabel = "Banana", XCategory = "Asia", YValue = 187 });
            bananaGroupData.Add(new DataPoint { LegendLabel = "Banana", XCategory = "North America", YValue = 132 });
            return bananaGroupData;
        }
     
        private List<DataPoint> GetAppleGroupData()
        {
            List<DataPoint> appleGroupData = new List<DataPoint>();
            appleGroupData.Add(new DataPoint { LegendLabel = "Apple", XCategory = "Europe", YValue = 145 });
            appleGroupData.Add(new DataPoint { LegendLabel = "Apple", XCategory = "Asia", YValue = 164 });
            appleGroupData.Add(new DataPoint { LegendLabel = "Apple", XCategory = "North America", YValue = 140 });
            return appleGroupData;
        }
     
        private void InitGroupingCharts()
        {
            List<DataPoint> groupData = GetGroupData();
     
            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
     
            seriesMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue, ChartAggregateFunction.Sum));
            seriesMapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
     
            chartGrouping1.SeriesMappings.Clear();
            chartGrouping1.SeriesMappings.Add(seriesMapping);
            chartGrouping1.ItemsSource = null;
            chartGrouping1.ItemsSource = groupData;
     
            seriesMapping.GroupingSettings.GroupDescriptors.Clear();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
     
            seriesMapping.ItemMappings.Clear();
            seriesMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue, ChartAggregateFunction.Sum));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.XCategory));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
            chartGrouping2.SeriesMappings.Clear();
            chartGrouping2.SeriesMappings.Add(seriesMapping);
            chartGrouping2.ItemsSource = null;
            chartGrouping2.ItemsSource = groupData;
        }
     
        private void InitNestedGroupingCharts()
        {
            chartGrouping21.SeriesMappings.Clear();
     
            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.CollectionIndex = 0;
            seriesMapping.LegendLabel = "A";
            seriesMapping.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
     
            seriesMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue, ChartAggregateFunction.Sum));
            seriesMapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
     
            chartGrouping21.SeriesMappings.Add(seriesMapping);
             
            seriesMapping = new SeriesMapping();
            seriesMapping.CollectionIndex = 1;
            seriesMapping.LegendLabel = "B";
            seriesMapping.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
     
            seriesMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue, ChartAggregateFunction.Sum));
            seriesMapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
            chartGrouping21.SeriesMappings.Add(seriesMapping);
     
            chartGrouping21.ItemsSource = null;
            List<List<DataPoint>> groupData = new List<List<DataPoint>>();
            groupData.Add(GetAppleGroupData());
            groupData.Add(GetBananaGroupData());
            chartGrouping21.ItemsSource = groupData;
     
            chartGrouping22.SeriesMappings.Clear();
            SeriesMapping seriesMapping22 = new SeriesMapping();
            seriesMapping22.CollectionIndex = 0;
            seriesMapping22.LegendLabel = "A";
            seriesMapping22.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
     
            seriesMapping22.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
            seriesMapping22.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.XCategory));
            seriesMapping22.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
     
            chartGrouping22.SeriesMappings.Add(seriesMapping22);
     
            seriesMapping22 = new SeriesMapping();
            seriesMapping22.CollectionIndex = 1;
            seriesMapping22.LegendLabel = "B";
            seriesMapping22.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
     
            seriesMapping22.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
            seriesMapping22.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.XCategory));
            seriesMapping22.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
            chartGrouping22.SeriesMappings.Add(seriesMapping22);
     
     
            chartGrouping22.ItemsSource = null;
            chartGrouping22.ItemsSource = groupData;
        }

    Actually grouping one collection is fine until I will start using milti-series charts. Where I will have both line and bar. In this situation I can figure out how to group my data.

    Thanks, Alexey
  2. Alexey Oyun
    Alexey Oyun avatar
    74 posts
    Member since:
    Mar 2010

    Posted 04 Aug 2010 Link to this post

    Here is screenshot
  3. Alexey Oyun
    Alexey Oyun avatar
    74 posts
    Member since:
    Mar 2010

    Posted 05 Aug 2010 Link to this post

    I had bug in previous code post, so here is update.
    XAML:
    <my:RadChart x:Name="chartGrouping1" Height="150"/>
    <my:RadChart x:Name="chartGrouping2" Height="150"/>
    <my:RadChart x:Name="chartGrouping21" Height="150"/>
    <my:RadChart x:Name="chartGrouping22" Height="150"/>
    C#:
    public partial class Chart
    {
        public Chart()
        {
            InitializeComponent();
            Loaded += Chart_Loaded;
        }
     
        private void Chart_Loaded(object sender, RoutedEventArgs e)
        {
            InitGroupingCharts();
            InitNestedGroupingCharts();
        }
     
        private List<DataPoint> GetGroupData()
        {
            List<DataPoint> groupData = new List<DataPoint>();
            groupData.AddRange(GetAppleGroupData());
            groupData.AddRange(GetBananaGroupData());
            return groupData;
        }
     
        private List<DataPoint> GetBananaGroupData()
        {
            List<DataPoint> bananaGroupData = new List<DataPoint>();
            bananaGroupData.Add(new DataPoint { LegendLabel = "Banana", XCategory = "Europe", YValue = 186 });
            bananaGroupData.Add(new DataPoint { LegendLabel = "Banana", XCategory = "Asia", YValue = 187 });
            bananaGroupData.Add(new DataPoint { LegendLabel = "Banana", XCategory = "North America", YValue = 132 });
            return bananaGroupData;
        }
     
        private List<DataPoint> GetAppleGroupData()
        {
            List<DataPoint> appleGroupData = new List<DataPoint>();
            appleGroupData.Add(new DataPoint { LegendLabel = "Apple", XCategory = "Europe", YValue = 145 });
            appleGroupData.Add(new DataPoint { LegendLabel = "Apple", XCategory = "Asia", YValue = 164 });
            appleGroupData.Add(new DataPoint { LegendLabel = "Apple", XCategory = "North America", YValue = 140 });
            return appleGroupData;
        }
     
        private void InitGroupingCharts()
        {
            List<DataPoint> groupData = GetGroupData();
     
            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
     
            seriesMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue, ChartAggregateFunction.Sum));
            seriesMapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
     
            chartGrouping1.SeriesMappings.Clear();
            chartGrouping1.SeriesMappings.Add(seriesMapping);
            chartGrouping1.ItemsSource = null;
            chartGrouping1.ItemsSource = groupData;
     
            seriesMapping.GroupingSettings.GroupDescriptors.Clear();
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
            seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
     
            seriesMapping.ItemMappings.Clear();
            seriesMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue, ChartAggregateFunction.Sum));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.XCategory));
            seriesMapping.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
            chartGrouping2.SeriesMappings.Clear();
            chartGrouping2.SeriesMappings.Add(seriesMapping);
            chartGrouping2.ItemsSource = null;
            chartGrouping2.ItemsSource = groupData;
        }
     
        private void InitNestedGroupingCharts()
        {
            chartGrouping21.SeriesMappings.Clear();
     
            SeriesMapping seriesMapping21 = new SeriesMapping();
            seriesMapping21.CollectionIndex = 0;
            seriesMapping21.LegendLabel = "Apple Series";
            SetNesteGroupMapping21(seriesMapping21);
     
            chartGrouping21.SeriesMappings.Add(seriesMapping21);
             
            seriesMapping21 = new SeriesMapping();
            seriesMapping21.CollectionIndex = 1;
            seriesMapping21.LegendLabel = "Banana Series";
            SetNesteGroupMapping21(seriesMapping21);
            chartGrouping21.SeriesMappings.Add(seriesMapping21);
     
            chartGrouping21.ItemsSource = null;
            List<List<DataPoint>> groupData = new List<List<DataPoint>>();
            groupData.Add(GetAppleGroupData());
            groupData.Add(GetBananaGroupData());
            chartGrouping21.ItemsSource = groupData;
     
            chartGrouping22.SeriesMappings.Clear();
            SeriesMapping seriesMapping22 = new SeriesMapping();
            seriesMapping22.CollectionIndex = 0;
            seriesMapping22.LegendLabel = "Apple Series";
            SetNesteGroupMapping22(seriesMapping22);
     
            chartGrouping22.SeriesMappings.Add(seriesMapping22);
     
            seriesMapping22 = new SeriesMapping();
            seriesMapping22.CollectionIndex = 1;
            seriesMapping22.LegendLabel = "Banana Series";
            SetNesteGroupMapping22(seriesMapping22);
            chartGrouping22.SeriesMappings.Add(seriesMapping22);
     
     
            chartGrouping22.ItemsSource = null;
            chartGrouping22.ItemsSource = groupData;
        }
     
        private void SetNesteGroupMapping21(SeriesMapping seriesMapping21)
        {
            seriesMapping21.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping21.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
            seriesMapping21.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
     
            seriesMapping21.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue, ChartAggregateFunction.Sum));
            seriesMapping21.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            seriesMapping21.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
        }
     
        private void SetNesteGroupMapping22(SeriesMapping seriesMapping22)
        {
            seriesMapping22.SeriesDefinition = new BarSeriesDefinition();
            seriesMapping22.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("XCategory"));
            seriesMapping22.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("LegendLabel"));
            seriesMapping22.GroupingSettings.ShouldCreateSeriesForLastGroup = true;
            //seriesMapping22.GroupingSettings.ShouldFlattenSeries = true;
     
            seriesMapping22.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
            seriesMapping22.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.XCategory));
        }
     
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }

    What is expected.
    4th chart created with nested collection should be shown as 2nd chart but legend should remain as in 3rd chart.
    Legend could be added manually or shown as in 2nd chart.
    My biggest concern is how to get rid of empty space that is reserved for second series in each category.

    Thanks, Alexey.
  4. Ves
    Admin
    Ves avatar
    2926 posts

    Posted 09 Aug 2010 Link to this post

    Hello Alexey Oyun,

    Please, find attached a small example. Unfortunately, it would not be possible to achieve this with nested collections. Your image shows 6 series and you need only three series -- one for each region. However, one series can contain items from a single collection, you cannot have items from several collections in the same series.

    The attached example shows how to configure the chart to achieve the desired result from a single flat list.

    Regards,
    Ves
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top