Search code examples
c#vb.netchartsmschartradar-chart

MS Charts: Different colors on label values


Is their any possible way to get different colors on each X-Axis value of a radar chart?

Already tried custom labels, but it didn't work.

Any help will be much appreciated.

enter image description here


Solution

  • There are neither Properties nor CustomAttributes to achieve this for AxisLabels.

    But CustomLabels will do the job nicely.

    Here is an example that adds a CustumLabel for each DataPoint in a Series and gives it a random color:

    enter image description here

    Set up the data:

    Random rnd = new Random(0);
    List<Color> colors = new List<Color>() { Color.Red, Color.Firebrick, Color.Gold,
        Color.DeepPink, Color.Azure, Color.IndianRed, Color.ForestGreen };
    
    ChartArea ca = chart.ChartAreas[0];
    
    Series s = chart.Series[0];
    
    for (int i = 1; i < 7; i++)
    {
        s.Points.AddXY(i, i+ rnd.Next(20 - i));
    }
    

    Now add CustomLabels:

    foreach (var dp in s.Points)
    {
        CustomLabel cl = new CustomLabel();
        cl.FromPosition = dp.XValue;
        cl.ToPosition = dp.XValue ;
        cl.Text = dp.YValues[0]+ "$";
        cl.ForeColor = colors[rnd.Next(colors.Count)];
    
        ca.AxisX.CustomLabels.Add(cl);
    }
    

    Note that for ChartType Radar this is rather simple; for most other types getting the FromPosition and ToPosition is rather tricky: There you need to calculate (usually) the center between two points..