This is a migrated thread and some comments may be shown as answers.

Reverse Legend Item Order

2 Answers 78 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 25 Nov 2012, 09:52 AM

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 Answers, 1 is accepted

Sort by
0
Petar Kirov
Telerik team
answered on 29 Nov 2012, 07:42 AM
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.

0
Jonathan
Top achievements
Rank 1
answered on 29 Nov 2012, 07:53 AM
That's excellent, it works well. Thanks.
Tags
Chart
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Petar Kirov
Telerik team
Jonathan
Top achievements
Rank 1
Share this question
or