RadChartView legend formatting

2 posts, 0 answers
  1. Mathias
    Mathias avatar
    4 posts
    Member since:
    Apr 2013

    Posted 17 Apr 2015 Link to this post

    Hello,

     I have 2 issues with the attached chart legend:

    1) I would like the legend items to be displayed on 2 adjacent columns (see chart legend expected.png). Is that possible?

    2) I don't know if you noticed but item legend texts are not perfectly left aligned. Do you have a fix for that?

     

    Best,

    Mathias

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 22 Apr 2015 Link to this post

    Hello Mathias,

    Thank you for writing.

    The ChartLegendElement displays the legend items in a StackLayoutElement which can be vertical or horizontal. In order to display the legend items in two columns, you can use two additional vertical StackLayoutElement. Here is a sample code snippet, which result is illustrated on the attached screenshot:
    public Form1()
    {
        InitializeComponent();
     
        Random rand = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8; i++)
        {
            LineSeries lineSeries = new LineSeries();
            sb = new StringBuilder();
            for (int j = 0; j < i + 1; j++)
            {
                sb.Append("S0" + i);
            }
            lineSeries.LegendTitle = sb.ToString();
            lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(-50, 50), "Jan"));
            lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(-50, 50), "Apr"));
            lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(-50, 50), "Jul"));
            lineSeries.DataPoints.Add(new CategoricalDataPoint(rand.Next(-50, 50), "Oct"));
            this.radChartView1.Series.Add(lineSeries);
        }
     
        this.radChartView1.ShowLegend = true;
        this.radChartView1.ChartElement.LegendPosition = LegendPosition.Bottom;
    }
     
    class MyChart : RadChartView
    {
        protected override RadChartElement CreateChartElement()
        {
            return new MyChartElement();
        }
    }
     
    class MyChartElement : RadChartElement
    {
        protected override ChartLegendElement CreateChartLegendElement()
        {
            return new MyLegendElement(this);
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadChartElement);
            }
        }
    }
     
    class MyLegendElement : ChartLegendElement
    {
        StackLayoutElement leftPanel = new StackLayoutElement();
        StackLayoutElement rightPanel = new StackLayoutElement();
     
        public MyLegendElement(RadChartElement chartElement) : base(chartElement)
        {
        }
     
        protected override void CreateChildElements()
        {
            base.CreateChildElements();
            this.StackElement.Orientation = System.Windows.Forms.Orientation.Horizontal;          
        }
     
        protected override void OnLegendInfosCollectionChanged(Telerik.WinControls.Data.NotifyCollectionChangedEventArgs e, bool providerChange)
        {
            if (!this.StackElement.Children.Contains(leftPanel))
            {
                this.StackElement.Children.Add(leftPanel);
                leftPanel.Orientation = System.Windows.Forms.Orientation.Vertical;
            }
            if (!this.StackElement.Children.Contains(rightPanel))
            {
                this.StackElement.Children.Add(rightPanel);
                rightPanel.Orientation = System.Windows.Forms.Orientation.Vertical;
            }
     
            leftPanel.Children.Clear();
            rightPanel.Children.Clear();
            for (int i = 0; i < this.Provider.LegendInfos.Count; i++)
            {
                LegendItemElement element = this.OnVisualItemCreating(this.Provider.LegendInfos[i]);
                     
                if (i < this.Provider.LegendInfos.Count / 2)
                {
                    leftPanel.Children.Add(element);
                }
                else
                {
                    rightPanel.Children.Add(element);
                }
            }
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(ChartLegendElement);
            }
        }
    }

    As to the question about text alignment of the items in the LegendElement, I was unable to reproduce it on my end with the specified version 2015.1 331. I have attached my sample project. Could you please have a look into it and specify how it differs from your real setup? Thus, we would be able to investigate the precise case and assist you further. Thank you in advance.

    I am looking forward to your reply.
     
    Regards,
    Dess
    Telerik
     

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

     
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top