C# chart rotate labels

2019-01-14 07:05发布

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.

标签: c# charts
3条回答
男人必须洒脱
2楼-- · 2019-01-14 07:35

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
查看更多
Ridiculous、
3楼-- · 2019-01-14 07:37

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".

查看更多
该账号已被封号
4楼-- · 2019-01-14 07:39

I needed these lines to get it to work:

chartarea.AxisX.LabelStyle.Angle = -90;
chartarea.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
chartarea.AxisX.IsLabelAutoFit = false;
查看更多
登录 后发表回答