FredPonch
FredPonch

Reputation: 502

Formatting Microsoft Chart Control X Axis labels for specific datapoint

In a winfow application, I've a ms chart with 2 chart areas. The first chart areas contains 4 series (stacked and bar)

I need to change the X axis label color for some specific point, but in VS 2010, I can only change the axislabel text but not the color.

Is there a way to do that?

Upvotes: 2

Views: 7273

Answers (2)

Bill W
Bill W

Reputation: 1488

I know this is too late for the OP, however it may be useful for someone else looking for how to do this.

A custom label allows you to set the color, the problem is that if you add one custom label then all the standard labels disappear so you have to create custom labels for the entire axis then set the color for the one that you want to be different.

This code assumes that you need a label for every X value. If you have a large number of X values you will need to adjust the code.

double offset = 0.5;//Choose an offset that is 1/2 of the range between x values
for (int i = 0; i < chart1.Series[0].Points.Count; i++)
{
    var customLabel = new CustomLabel();
    //NOTE: the custom label will appear at the mid-point between the FromPosition and the ToPosition
    customLabel.FromPosition = chart1.Series[0].Points[i].XValue - offset; //set beginning position (uses axis values)
    customLabel.ToPosition = chart1.Series[0].Points[i].XValue + offset; //set ending position  (uses axis values)
    customLabel.Text = chart1.Series[0].Points[i].XValue.ToString(); //set the text to display, you may want to format this value
    if (i == 3)
    {
    customLabel.ForeColor = Color.Green;//only change the 3rd label to be green, the rest will default to black
    }
    chart1.ChartAreas[0].AxisX.CustomLabels.Add(customLabel);
}

Upvotes: 1

NeverHopeless
NeverHopeless

Reputation: 11233

In the this link: http://msdn.microsoft.com/en-us/library/dd456628.aspx

you will find the use of LabelStyle class to change label of Axes. Use LabelStyle.ForeColor property to change color of label.

Upvotes: 2

Related Questions