I created a function that generates random colors.
Function:
public Color newColor()
{
Random randomGen = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
KnownColor randomColorName = names[randomGen.Next(names.Length)];
Color random = Color.FromKnownColor(randomColorName);
return random;
}
I am using the generated colors for my Chart's datapoints and their legend.
foreach (Series ser in Chart1.Series)
{
int i = 0;
foreach (DataPoint point in ser.Points)
{
Color randomColor = newColor();
Chart1.Series["Series1"].Points[i].Color = randomColor;
point.Color = Chart1.Series["SeriesName"].Points[i].Color;
Chart1.Legends["Legend1"].CustomItems.Add(randomColor, point.AxisLabel);
i++;
}
}
My problem here is, the colors are all the same. Yes, they are randomly generated, but they are all the same. If the generated color is blue, everything is blue, including their legends. I added a breakpoint on my function to enter debug mode, it is perfectly fine! The function returns different colors everytime it is called and all the datapoints colors are different from each other. But without entering debug mode, it displays same colors. What's going on? How can I fix this?