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

Reorder chart legend labels

3 Answers 61 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Boyan
Top achievements
Rank 1
Boyan asked on 10 Mar 2011, 08:34 PM
Hi. I have been trying to figure out how to reorder the items listed in the chart legend, but so far to no avail. I'd like to do something along the lines of this:

                List<Telerik.Charting.ChartLabel> legendItems = new List<Telerik.Charting.ChartLabel>();

                legendItems.Add(RadChart1.Legend.Items[0]);
                legendItems.Add(RadChart1.Legend.Items[2]);
                legendItems.Add(RadChart1.Legend.Items[5]);
                legendItems.Add(RadChart1.Legend.Items[1]);
                legendItems.Add(RadChart1.Legend.Items[4]);
                legendItems.Add(RadChart1.Legend.Items[3]);
                legendItems.Add(RadChart1.Legend.Items[6]);

                for (int i = 0; i < 7; i ++)
                    RadChart1.Legend.Items[i] = legendItems[i];

But when I run this the RadChart1.Legend.Items appears empty. I am sure I am missing something here and was hoping you'd be able to lend a hand.

I would essentially like to know how I can reorder the labels that appear in the legend block.

Thank you!

3 Answers, 1 is accepted

Sort by
0
Evgenia
Telerik team
answered on 14 Mar 2011, 04:33 PM
Hi Boyan,

You can subscribe to BeforeLayout event of the RadChart - at that moment the Legend Items Collection is populated and add the reordered Items like this:

protected void RadChart1_BeforeLayout(object sender, System.EventArgs e)
 {
     List<Telerik.Charting.LabelItem> legendItems = new List<Telerik.Charting.LabelItem>();
     legendItems.Add(RadChart1.Legend.Items[2]);
     legendItems.Add(RadChart1.Legend.Items[1]);
     legendItems.Add(RadChart1.Legend.Items[0]);
     for (int i = 0; i < RadChart1.Legend.Items.Count; i++ )
     {
         //Remove the top-most item (which then decrements the item count of the legend)   
         RadChart1.Legend.Items.RemoveAt(0);
         //Insert the new (reordered) legend item at the end of the list   
         RadChart1.Legend.Items.Insert(RadChart1.Legend.Items.Count, legendItems[i]);
     }
 }

Kind regards,
Evgenia
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!
0
Boyan
Top achievements
Rank 1
answered on 17 Mar 2011, 11:06 PM
Thanks, Evgenia!

For some reason 
//Remove the top-most item (which then decrements the item count of the legend)   
         RadChart1.Legend.Items.RemoveAt(0);

did not work for me. It was not decreasing the count of the legend, so i used the following instead, which seemed to work :

RadChart1.Legend.Clear()

for (int i = 0; i < legendItems.Count; i++)
{
RadChart1.Legend.Items(i, legendItems[i]);
}

Thanks again for your help!
0
Evgenia
Telerik team
answered on 18 Mar 2011, 09:55 AM
Hello Boyan,

I'm glad you were able to resolve the issue on your own.
Let us know if you need further assistance!

Greetings,
Evgenia
the Telerik team
Tags
Chart (Obsolete)
Asked by
Boyan
Top achievements
Rank 1
Answers by
Evgenia
Telerik team
Boyan
Top achievements
Rank 1
Share this question
or