Reorder chart legend labels

Thread is closed for posting
4 posts, 0 answers
  1. Boyan
    Boyan avatar
    17 posts
    Member since:
    Feb 2011

    Posted 10 Mar 2011 Link to this post

    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!
  2. Evgenia
    Admin
    Evgenia avatar
    1437 posts

    Posted 14 Mar 2011 Link to this post

    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!
  3. Boyan
    Boyan avatar
    17 posts
    Member since:
    Feb 2011

    Posted 17 Mar 2011 Link to this post

    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!
  4. Evgenia
    Admin
    Evgenia avatar
    1437 posts

    Posted 18 Mar 2011 Link to this post

    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
Back to Top