简体   繁体   中英

Margin between labels and y-axis, if labels on x-axis are shown with angle

I recently had to change the size of the x-axis labels to assure they're legible when printed. Doing so, a margin between the labels and the axis itself appeared, which seems to depend in size on the fontsize and the number of datapoints.

This margin seems to vanish, when removing the angle on the labels (AxisX.LabelStyle.Angle = 0). Unfortunately, this aint a solution, as the angle on the label is a requirement for the project.

Setting IsMarginsVisible didn't solve the issue.

Is there any way to remove this margin?

Screenshots visualizing the issue:

有角度和意外的余量 没有边距,但是没有角度

An MCVE showing the issue is available here: https://www.dropbox.com/s/4y0nwj8bymz6m3q/MSChart%20-%20misplaced%20labels.zip?dl=0

Please note: Some time ago, I ran into a similar issue, if the number of elements becomes huge, which is also still unsolved ( MSChart axis misplaced for huge charts ). I'm available for any questions and suggestions, to avoid becoming this question as unpopular as the other.

PS If you feel the urge to downvote this question, please let me know why so I may try to improve it (as I'm unable to see myself where this question is unclear or missing signs of recherche). Thank you.

Unfortunetly I think this is a bug of all type bar charts. To be clear since the question was downvoted: The margin (alot of whitespace if Position is changed from Auto to 0,0,100,100) appears in the bottom of the chart (Y-Axis for bar-type-of charts) as soon as the X-axis text is angled, via chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 30; as in the provided example or eg. chart1.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30; .

There are a couple of reasons I suspect this odd behaviour of beeing a bug, namely:

  1. If you squeeze the width of the chart in the provided code (same as in screenshots in the question) together the margin keep increasing, whereas correct behaviour would be no change.
  2. You can further verify this by changing the chart type to any type of Column-chart
  3. This behaviour is more importantly only apparent in the Primary X-Axis, not the Secondary.

I have tried CustomLabels and with reversing the X-Axis and instead use Secondary Y-axis, but any axis in the bottom gets it's size calculated incorrectly. Infact even if you use the Secondary Y-Axis the Primary has to be disabled to avoid white-space scaling at the bottom.

Which means the most apparent options are sadly reduced to:

  • Requiring alot of more work you can draw your own labels utlizing the Post_Paint event ( Example )
  • If at all possible, consider a column chart which behaves correctly
  • Or use the Secondary Y-axis at the top instead

Both the latter drastically change the appearance of the chart but do leave you with control over the margin. In conclusion: no, this seems to be a bug, there is no way to remove the margin by setting "correct" Properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM