开发者

C# chart rotate labels

开发者 https://www.devze.com 2023-03-06 15:55 出处:网络
I have a simple chart, and I\'d lik开发者_如何转开发e the labels on the x-axis to be rotated 45 degrees. What am I doing wrong?

I have a simple chart, and I'd lik开发者_如何转开发e 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:

C# chart rotate labels

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


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


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

C# chart rotate labels

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


I needed these lines to get it to work:

chartarea.AxisX.LabelStyle.Angle = -90;
chartarea.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
chartarea.AxisX.IsLabelAutoFit = false;


I know this question is old and answered. I just want to say that Series.LabelAngle controls series' label, not Axis'. If you add these two lines, the label would be shown above the column and is rotated 45 degrees:

mySeries.IsValueShownAsLabel = true;
mySeries.SmartLabelStyle.Enabled = false;

So you have to set AxisX's LabelAngle as Maciej Rogoziński said.


Axis.IsLabelAutoFit default value is true, so it must be set to false in order for LabelStyle.Angle to be applied.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号