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

Exception when adding palettes in code behind

1 Answer 59 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Håkan
Top achievements
Rank 1
Håkan asked on 17 Nov 2014, 11:45 AM
Hi.

I have a pie chart that I need to control the colors on.
Therefore I add palettes in code behind.
It works fine the first time, but when I update the data and the chart is redrawn it crashes when adding the second entry of a palette.
Although the palette gets added correctly, so if I surround it with a try/catch it works, but I would like to solve the problem in the first case.

Here is my code:

this.viewModel.ShiftTypes.Clear();
            this.Chart.Palette.GlobalEntries.Clear();
 
            List<ShiftTypesQuotaGaugeChartData> data = new List<ShiftTypesQuotaGaugeChartData>();
            List<int> shiftTypeIds = planningViewModel.Appointments.Where(a => a.IsVisible).Select(a => a.ShiftTypeId).Distinct().ToList();
            if (shiftTypeIds.Count == 0)
                return;
 
            foreach (int shiftTypeId in shiftTypeIds)
            {
                int shifts = planningViewModel.Appointments.Where(a => a.ShiftTypeId == shiftTypeId).Count();
                if (shifts > 0)
                {
                    ShiftTypeDTO st = this.allShiftTypes.Where(s => s.ShiftTypeId == shiftTypeId).FirstOrDefault();
                    data.Add(new ShiftTypesQuotaGaugeChartData()
                    {
                        Name = st.Name,
                        Value = shifts,
                    });
 
                    try
                    {
                        this.Chart.Palette.GlobalEntries.Add(new PaletteEntry(GraphicsUtility.GetBrushFromString(st.Color), GraphicsUtility.GetBrush(GraphicsUtility.SOEBrushes.ControlBorder)));
                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        // The second time we get an unexpected Index out of bounds here, but it still adds the palette.
                    }
                }
            }
 
            Telerik.Windows.Controls.CollectionExtensions.AddRange(this.viewModel.ShiftTypes, data);

Regards,
Håkan


1 Answer, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 19 Nov 2014, 12:32 PM
Hello Håkan,

I cannot be completely sure about the reason behind the reported issue since I do not have your code. However, it is important to keep in mind that you should first initialize the Palette before filling it with PaletteEntry objects. Please take a look at the attached project and let me know if you have any further questions.

Regards,
Pavel R. Pavlov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
ChartView
Asked by
Håkan
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Share this question
or