Show custom legends

2 posts, 0 answers
  1. Raheel
    Raheel avatar
    3 posts
    Member since:
    Sep 2010

    Posted 16 Feb 2011 Link to this post

    Hello ,




    We need to show the bar graph using custom colors, but when we run code, we get only color that comes first in color Palette [refer to below code].


    Below example showing the one of the possible scenario, that we have 3 values bound to listbox and we need to show those values to bar graph.



    XAML :

       <telerik:RadChart x:Name="testChart" Width="350" Height="490" PaletteBrushesUseSolidColors="True">         </telerik:RadChart>




    Code Behind :


    public partial class TestParentClass


            System.Windows.Controls.ListBox ChartListData = new System.Windows.Controls.ListBox();

            string TechID





            private BrushCollection _palette;

            public BrushCollection Palette




                    return this._palette;




                    this._palette = value;



            public TestParentClass()



                    this.Palette = new BrushCollection()


                        new SolidColorBrush(Colors.Green),

                        new SolidColorBrush(Colors.Yellow),

                        new SolidColorBrush(Colors.Red)





            public void LoadChartData()





                    System.Windows.Controls.ListBox ChartListData = new System.Windows.Controls.ListBox();

                    ChartListData.Items.Add(new TestClass(3,"first"));

                    ChartListData.Items.Add(new TestClass(6, "Second"));

                    ChartListData.Items.Add(new TestClass(8, "Third"));



                    Grid grid = new Grid();

                    grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(25, GridUnitType.Auto) });

                    grid.RowDefinitions.Add(new RowDefinition());

                    grid.ColumnDefinitions.Add(new ColumnDefinition());

                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Auto) });



                    testChart.Content = grid;



                    ////Chart Title

                    ChartTitle customTitle = new ChartTitle();

                    customTitle.Content = "Title";


                    grid.Background = new SolidColorBrush(Colors.Black);


                    ////Chart Legend

                    ChartLegend legend = new ChartLegend();

                    legend.UseAutoGeneratedItems = false;

                    legend.Header = string.Empty;

                    legend.Name = "Legends";

                    Grid.SetColumn(legend, 2);

                    Grid.SetRow(legend, 1);



                    //Chart Area

                    ChartArea viewChart = new ChartArea();

                    foreach (Brush item in this.Palette)





                    testChart.PaletteBrushesUseSolidColors = true;

                    testChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;



                    Grid.SetRow(viewChart, 1);

                    Grid.SetColumn(viewChart, 0);



                    DataSeries Chartseries = new DataSeries();

                    Chartseries.Definition = new BarSeriesDefinition();



                    foreach (TestClass Item in ChartListData.Items)


                        Chartseries.Add(new DataPoint() { YValue = Convert.ToInt32(Item.count), XCategory = Item.Legend });











                catch (Exception ex)







        public class TestClass


            public int count;

            public string Legend;


            public TestClass(int c,string s)


                count = c;

                Legend = s;





     The issue being faced is that whenever we enable to show custom colrs on bars


    PaletteBrushesUseSolidColors = true;

    testchart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;



    Then we do not get the custom legeds e.g here we have first,second third etc instead we gte the either [item0,item1...] or [series1,series2..].


    Could you please help us to resolve these issue.





  2. Yavor
    Yavor avatar
    401 posts

    Posted 22 Feb 2011 Link to this post

    Hello Raheel,

    I see that you are defining a custom layout in code behind, but let me add some things. First because you are creating a custom layout you should set the UseDefaultLayout property to false. Next you should associate your chartarea with the legend by setting the Legend property of the ChartArea like this:

    ChartArea viewChart = new ChartArea() { Legend = legend };

    We reproduced the problem that you have mentioned. Indeed the legend items don't get their color from their corresponding bars. I have already forwarded your report to our developers for further review. In the meanwhile you can call testChart.UpdateLogicalChildren() method right after you create your chart area. It seems that this problem is caused by the legend not finding the right chart area as its logical parent. Calling this method will recreate the logical tree and fix the issue you are experiencing.

    In the attached file you can find your code with the fixes mentioned above.

    All the best,
    Yavor Ivanov
    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!
Back to Top