Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 827
MaYaN Avatar asked Mar 09 '12 16:03

MaYaN


People also ask

How do I change the color of my axis labels?

Set up X-axis and Y-axis labels using set_xlabel and set_ylabel method for creating ax using add_subplot(). To set the color for X-axis and Y-axis, we can use the set_color() method (Set both the edgecolor and the facecolor). To set the ticks color, use tick_params method for axes.

How do you change axis label color in Excel?

Just click to select the axis you will change all labels' font color and size in the chart, and then type a font size into the Font Size box, click the Font color button and specify a font color from the drop down list in the Font group on the Home tab.

How do I change the color of my bar graph bar?

On a chart, select the individual data marker that you want to change. On the Format tab, in the Shape Styles group, click Shape Fill. Do one of the following: To use a different fill color, under Theme Colors or Standard Colors, click the color that you want to use.


2 Answers

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);
}
like image 136
endofzero Avatar answered Oct 09 '22 08:10

endofzero


Ok The only solution I have found is to create a custom label and set the color that way:

this._chart.ChartAreas[0].AxisX.CustomLabels.Add(new CustomLabel(position - 1, position + 1, point.AxisLabel, 0, LabelMarkStyle.None));

this._chart.ChartAreas[0].AxisX.CustomLabels[position - 1].ForeColor = GetColor(point.AxisLabel);
like image 44
MaYaN Avatar answered Oct 09 '22 06:10

MaYaN