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

Show custom legends

1 Answer 106 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Raheel
Top achievements
Rank 1
Raheel asked on 16 Feb 2011, 08:38 PM

Hello ,


 

Scenario:

 


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

        {

            get;

            set;

        }

        private BrushCollection _palette;

        public BrushCollection Palette

        {

            get

            {

                return this._palette;

            }

            set

            {

                this._palette = value;

            }

        }

        public TestParentClass()

        {

 

                this.Palette = new BrushCollection()

                {

                    new SolidColorBrush(Colors.Green),

                    new SolidColorBrush(Colors.Yellow),

                    new SolidColorBrush(Colors.Red)

                };

          

        }

 

        public void LoadChartData()

        {

 

            try

            {

                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.Children.Add(customTitle);

                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);

                grid.Children.Add(legend);

 

                //Chart Area

                ChartArea viewChart = new ChartArea();

                foreach (Brush item in this.Palette)

                {

                    testChart.PaletteBrushes.Add(item);

                }

 

                testChart.PaletteBrushesUseSolidColors = true;

                testChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;

 

               

                Grid.SetRow(viewChart, 1);

                Grid.SetColumn(viewChart, 0);

                grid.Children.Add(viewChart);

 

                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 });

 

 

                }

 

 

                viewChart.DataSeries.Add(Chartseries);

 

               

 

            }

            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.

 

 

Thanks,

 

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 22 Feb 2011, 09:45 AM
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!
Tags
Chart
Asked by
Raheel
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or