Multile Y axis Binding with two Itemsource

2 posts, 0 answers
  1. Sunil
    Sunil avatar
    14 posts
    Member since:
    Sep 2012

    Posted 18 Jan 2011 Link to this post

    Hi Telerik,
    In My chart I have multiple Y axis,
    I am trying to binding with one barseriesdefination and other with LineSeriesDefination each having saparate Itemsource.
    But there is problem of initilization of series.
    If on this senario any solution available please share.
    I attached source Code.
    Thanks in advance.
    public Test()
            {
                InitializeComponent();
      
                  
                this.Loaded += new RoutedEventHandler(GroupMultiYAxes_Loaded);
                this.RadChart.DataBound += new EventHandler<ChartDataBoundEventArgs>(RadChart_DataBound);
                  
            }
      
            void RadChart_DataBound(object sender, ChartDataBoundEventArgs e)
            {
                RadChart.DefaultView.ChartArea.DataSeries[0].Definition.AxisName = "Secondary";
            }
      
            void GroupMultiYAxes_Loaded(object sender, RoutedEventArgs e)
            {
                AxisY additionalAxis = new AxisY();
                additionalAxis.AxisName = "Secondary";
                additionalAxis.AutoRange = false;
                additionalAxis.AddRange(0, 30, 10);
                RadChart.DefaultView.ChartArea.AdditionalYAxes.Add(additionalAxis);
      
                this.SetMappings(RadChart);
      
                RadChart.ItemsSource = new List<MyData2>() {
                    new MyData2() { MyProp1 = "A", Value=5, Cost=10},
                    new MyData2() { MyProp1 = "A", Value=6, Cost=20},
                    new MyData2() { MyProp1 = "B", Value=7, Cost=10},
                    new MyData2() { MyProp1 = "B", Value=8, Cost=20},
                };
            }
      
            private void SetMappings(RadChart chart)
            {
                SeriesMapping seriesMapping = new SeriesMapping();
                seriesMapping.SeriesDefinition = new BarSeriesDefinition();
                seriesMapping.CollectionIndex = 0;
                seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("MyProp1"));
                ItemMapping itemMapping = new ItemMapping();
                itemMapping.DataPointMember = DataPointMember.YValue;
                itemMapping.FieldName = "Value";
                seriesMapping.ItemMappings.Add(itemMapping);
      
                SeriesMapping seriesMapping1 = new SeriesMapping();
                seriesMapping1.SeriesDefinition = new LineSeriesDefinition();
                seriesMapping1.CollectionIndex = 1;
                seriesMapping1.SeriesDefinition.AxisName = "Secondary";
                ItemMapping itemMapping1 = new ItemMapping();
                itemMapping1.DataPointMember = DataPointMember.YValue;
                itemMapping1.FieldName = "Cost";
                seriesMapping.ItemMappings.Add(itemMapping1);
      
                chart.SeriesMappings.Add(seriesMapping);
                chart.SeriesMappings.Add(seriesMapping1);
            }
        }
      
        public class MyData2
        {
            public string MyProp1 { get; set; }
            public int Value { get; set; }
            public int Cost { get; set; }
        }
    }
     
  2. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 21 Jan 2011 Link to this post

    Hi sunil,

    RadChart support for nested collections requires setting of the CollectionIndex property of each SeriesMapping. From your code I see that you are using a normal collection, so you don't have to set the CollectionIndex. Also in your SetMappings I noticed a small typo. With the modifications your code can look like this:

    private void SetMappings(RadChart chart)
    {
        SeriesMapping seriesMapping = new SeriesMapping();
        seriesMapping.SeriesDefinition = new BarSeriesDefinition();
        //seriesMapping.CollectionIndex = 0;
        seriesMapping.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("MyProp1"));
        ItemMapping itemMapping = new ItemMapping();
        itemMapping.DataPointMember = DataPointMember.YValue;
        itemMapping.FieldName = "Value";
        seriesMapping.ItemMappings.Add(itemMapping);
     
        SeriesMapping seriesMapping1 = new SeriesMapping();
        seriesMapping1.SeriesDefinition = new LineSeriesDefinition();
        //seriesMapping1.CollectionIndex = 1;
        seriesMapping1.SeriesDefinition.AxisName = "Secondary";
        ItemMapping itemMapping1 = new ItemMapping();
        itemMapping1.DataPointMember = DataPointMember.YValue;
        itemMapping1.FieldName = "Cost";
        seriesMapping1.ItemMappings.Add(itemMapping1);
     
        chart.SeriesMappings.Add(seriesMapping);
        chart.SeriesMappings.Add(seriesMapping1);
    }

    You are setting the axis for the second mapping in the SetMappings method, so there is no need of doing it again on RadChart DataBound event. You can safely remove the handler for this event and everything will be fine.

    With these small modifications your code is ready to go.

    Hope this helps!

    Greetings,
    Yavor Ivanov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Back to Top