Question

C# chart rotate labels

I have a simple chart, and I'd like the labels on the x-axis to be rotated 45 degrees. What am I doing wrong?

Chart c = new Chart();
c.ChartAreas.Add(new ChartArea());
c.Width = 200;
c.Height = 200;
Series mySeries = new Series();
mySeries.Points.DataBindXY(new string[] { "one", "two", "three" }, new int[] { 1, 2, 3 });
mySeries.LabelAngle = 45; // why doesn't this work?
c.Series.Add(mySeries);

The output is:

img

I'm using the charting stuff from System.Web.UI.DataVisualization.Charting.

 21  40253  21
1 Jan 1970

Solution

 31

The documentation says that Series.LabelAngle sets data point label angle, which (I think) is a label above the chart's column.

To set an angle of axis labels try this one:

var c = Chart1;
c.ChartAreas.Add(new ChartArea());
c.Width = 200;
c.Height = 200;
Series mySeries = new Series();
mySeries.Points.DataBindXY(new string[] { "one", "two", "three" }, new int[] { 1, 2, 3 });
//mySeries.LabelAngle = -45; // why doesn't this work?
c.Series.Add(mySeries);
c.ChartAreas[0].AxisX.LabelStyle.Angle = 45; // this works
2011-05-18

Solution

 19

Here is how I usually rotate my X Axis labels.

 ChartArea area = new ChartArea();
 area.AxisX.IsLabelAutoFit = true;
 area.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30;
 area.AxisX.LabelStyle.Enabled = true;

Results

enter image description here

The key property/line to look at above is the "LabelAutoFitStyle".

2011-05-18