MS Chart: How can you change the color of each label on the Axis of a Bar Chart?

MaYaN picture MaYaN · Mar 9, 2012 · Viewed 8.8k times · Source

I have a bar chart which shows different categories on the Y axis.

I can change the color of all of them on the axis at the same time by using:

 chart.ChartAreas["MyChart"].AxisY.LabelStyle.ForeColor = "Red";

However it doesn't allow me to set the color for each of them.

Any help will be much appreciated.

Answer

endofzero picture endofzero · Mar 9, 2012

You can try adding custom labels to the chart, and that will allow you to modify each one individually.

private void AddCustomLabelAtYValue(double YValue, string Text, Color ForeColor)
{
    double scale = chart.ChartAreas["MyChart"].AxisY.Maximum - 
        chart.ChartAreas["MyChart"].AxisY.Minimum;
    double offset = scale * 0.5;
    CustomLabel customLabel = new CustomLabel(YValue - offset, 
        YValue + offset, Text, 0, LabelMarkStyle.None);
    customLabel.ForeColor = ForeColor;
    chart.ChartAreas["MyChart"].AxisY.CustomLabels.Add(customLabel);
}