Reverse Legend Item Order

3 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    5 posts
    Member since:
    Nov 2012

    Posted 25 Nov 2012 Link to this post

    Hi,

    Im using version 2010.1.309.1030 and I need to reverse the legend item order in a stacked bar chart. as I'm on an older version I don;t have access to the ReverseLegendItemsOrder property, so I'm attempting to do the following:

    Chart.DataBound += new EventHandler<ChartDataBoundEventArgs>(Chart_DataBound);

    void Chart_DataBound(object sender, ChartDataBoundEventArgs e)
    {
     var items = new List<ChartLegendItem>(Chart.DefaultView.ChartLegend.ItemsSource.Cast<ChartLegendItem>());
     items.Reverse();
     Chart.DefaultView.ChartLegend.ItemsSource = items;
    }

    However this seems to override the automatic crreation of the legend items, so when I rebind the chart with different data (and a different legend) the legend remains the previous one.

    Thanks

  2. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 29 Nov 2012 Link to this post

    Hi Jonathan,

    In order to use the auto generated items, you have to work with the collection that is already set to the ChartLegend.ItemSource (instead of setting a new collection to it). Basically you need to change your last line with this: 
    (Chart.DefaultView.ChartLegend.ItemsSource as IList<ChartLegendItem>).Clear();
    foreach (var item in items)
    {
    (Chart.DefaultView.ChartLegend.ItemsSource as IList<ChartLegendItem>).Add(item);
    }

    However when I tested this I noticed that if the code is executed in the RadChart.DataBound event handler, initially the chart legend items are not reversed. You can fix that by moving the code in the RadChart.LayoutUpdated event handler. The RadChart.DataBinding event is a appropriate moment to attach to the LayoutUpdated event. Here is an example: 
    public MainPage()
    {
        InitializeComponent();
     
        this.chart.DataBinding +=
            new EventHandler<ChartDataBindingEventArgs>(Chart_DataBinding);
    }
     
    void Chart_DataBinding(object sender, ChartDataBindingEventArgs e)
    {
        chart.LayoutUpdated += chart_LayoutUpdated;
    }
     
    void Chart_LayoutUpdated(object sender, EventArgs e)
    {
        if (chart.DefaultView.ChartLegend.ItemsSource == null)
            return;
     
        var items = new List<ChartLegendItem>(chart.DefaultView.ChartLegend.ItemsSource.Cast<ChartLegendItem>());
     
        if (items.Count == 0)
            return;
     
        items.Reverse();
     
        (chart.DefaultView.ChartLegend.ItemsSource as IList<ChartLegendItem>).Clear();
        foreach (var item in items)
        {
            (chart.DefaultView.ChartLegend.ItemsSource as IList<ChartLegendItem>).Add(item);
        }
     
        this.chart.LayoutUpdated -= chart_LayoutUpdated;
     
    }

    Greetings,
    Petar Kirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Jonathan
    Jonathan avatar
    5 posts
    Member since:
    Nov 2012

    Posted 29 Nov 2012 Link to this post

    That's excellent, it works well. Thanks.
Back to Top