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

Telerik RadcartesanChart Reverse the legend order

4 Answers 123 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Kannan
Top achievements
Rank 1
Kannan asked on 21 Apr 2015, 03:52 PM

Hi, I am using WPF telerik RadCartesanchart and a Radlegend control which is bound to the chart's legend Items property. It is working, but I would like to have the chart in the same way but legends to be displayed in reverse order, Could someone help me on this?

Thank you,

Kannan M

4 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 22 Apr 2015, 09:11 AM
Hi Kannan,

To achieve the desired effect you can create a custom panel that orders its items in reverse order and set it as an ItemPanel of the RadLegend control. Here is an example:
public class MyStackPanel : StackPanel
{
    protected override Size ArrangeOverride(Size finalSize)
    {
        double bottom = 0;
        foreach (UIElement item in this.Children)
        {
            bottom += item.DesiredSize.Height;
        }
 
        foreach (UIElement child in this.Children)
        {
            child.Arrange(new Rect(0, bottom - child.DesiredSize.Height, finalSize.Width, child.DesiredSize.Height));
            bottom -= child.DesiredSize.Height;
        }
 
        return finalSize;
    }
}

<telerik:RadLegend Items="{Binding ElementName=chart, Path=LegendItems}">
    <telerik:RadLegend.ItemsPanel>
        <ItemsPanelTemplate>
            <local:MyStackPanel />
        </ItemsPanelTemplate>
    </telerik:RadLegend.ItemsPanel>
</telerik:RadLegend>
Please note that this panel implementation is merely a suggestion which is not well tested. This is why I recommend you to test it properly before use it in your application.

If you have any further questions, please do not hesitate to contact us again.

Regards,
Martin
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Kannan
Top achievements
Rank 1
answered on 22 Apr 2015, 02:35 PM
Thanks a lot, that approach solved my issue
0
Renier Pretorius
Top achievements
Rank 2
Iron
Iron
Iron
answered on 02 Oct 2015, 06:45 AM

Hi Kannan and others,

 I was looking for the same and came across an alternative method which works easier for me. In the view constructor -

InitializeComponent();
chart.LegendItems.CollectionChanged += (s, e) =>
  {
      if (legend.Items == null) legend.Items = new Telerik.Windows.Controls.Legend.LegendItemCollection();
      legend.Items.Clear();
      var lst = chart.LegendItems.Reverse();
      foreach (var l in lst)
      {
          legend.Items.Add(l);
      }
  }

0
Renier Pretorius
Top achievements
Rank 2
Iron
Iron
Iron
answered on 02 Oct 2015, 06:46 AM
Actually got the idea from this forum
Tags
Chart
Asked by
Kannan
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Kannan
Top achievements
Rank 1
Renier Pretorius
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or