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

Colors not showing in barchart

1 Answer 74 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Rieni De Rijke
Top achievements
Rank 1
Rieni De Rijke asked on 04 Aug 2010, 12:29 PM
We have this code where we can switch from a pie to a bar.
The problem is: when we use the pie, the colors from the palette are used for each datapoint (4 different colors) and the legend shows the 4 datapoints in the serie.
But, switching to the bar, the colors are not used (we only get one color) and the datapoints (Nr1, Nr2...) don't show up in the legend.

        private BrushCollection _palette;

        public BrushCollection Palette

        {

            get { return _palette;}
            set { _palette = value; }

        }

        private void MakePallette()

        {

            Palette = new BrushCollection

                          {

                            new SolidColorBrush(Color.FromArgb(255, 106, 148, 200)),

                            new SolidColorBrush(Color.FromArgb(255, 238, 141, 61)),

                            new SolidColorBrush(Color.FromArgb(255, 145, 178, 80)),                         

                            new SolidColorBrush(Color.FromArgb(255, 255, 155, 152))

                          };

        }

        private void MakeChart()

        {

            RadChart radChart = new RadChart();s

            DataSeries dataSeries = null;

            MakePallette();

            foreach (Brush item in Palette)

            {

                radChart.PaletteBrushes.Add(item);

            }

           

            switch (ChartName) // determine what kind of data to display

            {

                case "A":

                    {

                        radChart.DefaultView.ChartTitle.Content = "All A";

                        switch (ChartNr)    // determine what kind of rendering (bar, pie...etc)

                        {

                            case 1:

                                dataSeries = new DataSeries { Definition = new PieSeriesDefinition() };

                                break;

                            case 2:

                                dataSeries = new DataSeries { Definition = new BarSeriesDefinition() };

                                break;

                        }

                        // Create a DataPoint

                        DataPoint dataPoint = new DataPoint { YValue = 8, Label = "8", LegendLabel = "Nr 1" };

                        dataSeries.Add(dataPoint);

                        

                        dataPoint = new DataPoint { YValue = 3, Label = "3", LegendLabel = "Nr 2" };

                        dataSeries.Add(dataPoint);

                       

                        dataPoint = new DataPoint { YValue = 10, Label = "10", LegendLabel = "Nr 3" };

                        dataSeries.Add(dataPoint);

                       

                        dataPoint = new DataPoint { YValue = 25, Label = "25", LegendLabel = "Nr 4" };

                        dataSeries.Add(dataPoint);

                    }

                    break;

                case "B":

                    {

                        radChart.DefaultView.ChartTitle.Content = "All B";

                        switch (ChartNr)    // determine what kind of rendering (bar, pie...etc)

                        {

                            case 1:

                                dataSeries = new DataSeries { Definition = new PieSeriesDefinition() };

                                break;

                            case 2:

                                dataSeries = new DataSeries { Definition = new BarSeriesDefinition() };

                                break;

                        }

                        DataPoint dataPoint = new DataPoint { YValue = 36, Label = "36", LegendLabel = "Krediteringer"};

                        dataSeries.Add(dataPoint);

                       

                        dataPoint = new DataPoint { YValue = 14, Label = "14", LegendLabel = "Rettelser"};

                        dataSeries.Add(dataPoint);

                        

                        dataPoint = new DataPoint { YValue = 369, Label = "369", LegendLabel = "Enkeltgebyr"};

                        dataSeries.Add(dataPoint);

                    }

                    break;

            }           

            radChart.DefaultView.ChartLegend.Header = string.Empty;

            radChart.DefaultView.ChartArea.DataSeries.Add(dataSeries);

        }

1 Answer, 1 is accepted

Sort by
0
Sia
Telerik team
answered on 05 Aug 2010, 10:17 AM
Hi Rieni De Rijke,

That behavior is expected because when you set a pie series definition - you have one data series and every slice is a data point with specific color. When you set a bar series definition and add data points to one data series, all get the same color.

There are two possible solutions:
1) To add new data series for every data point in order to get different color per bar item.
2) To set the LegendDisplayMode to DataPointLabel for the bar series definition:
dataSeries2.Definition = new BarSeriesDefinition() { LegendDisplayMode = LegendDisplayMode.DataPointLabel };

Please let me know if this fix your problem.

Greetings,
Sia
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Chart
Asked by
Rieni De Rijke
Top achievements
Rank 1
Answers by
Sia
Telerik team
Share this question
or