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:
Regards,
Håkan
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