无法更改C#图表中自定义x轴标签的文本方向
本文关键字:标签 方向 文本 自定义 | 更新日期: 2023-09-27 18:23:41
我正在创建一个StackedColumn图表,无法更改自定义x轴标签的方向,这些标签始终垂直。下面的代码位于包含名为"Chart"的Chart对象的Form的Load事件中
被注释掉的行是我在研究时发现的尝试修复。这些变化要么使标签消失,要么没有任何影响:
- 切换图表区域x轴的IsLabelAutoFit属性
- 更改LabelAutoFitStyle属性(也使用#1进行了测试)
- 更改IntervalType和Interval属性(也使用#1进行了测试)
- 切换LabelStyle.Enabled属性(也使用#1进行了测试)
- 更改LabelStyle.Angle属性(也使用#1进行了测试)
这是我的代码:
chart.BackColor = Color.FromArgb(211, 223, 240);
chart.BackGradientStyle = GradientStyle.TopBottom;
chart.BorderlineColor = Color.FromArgb(26, 59, 105);
chart.BorderlineDashStyle = ChartDashStyle.Solid;
chart.BorderlineWidth = 2;
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.Name = "Chart1";
chart.TabIndex = 1;
var title = new Title();
title.Alignment = ContentAlignment.TopCenter;
title.ForeColor = Color.FromArgb(26, 59, 105);
title.Font = new Font("Segoe UI", 14.25F, FontStyle.Bold);
title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
title.ShadowOffset = 3;
title.Text = "Sales Report";
chart.Titles.Clear();
chart.Titles.Add(title);
var chartArea = new ChartArea();
chartArea.AxisX.Title = "Product Sold";
chartArea.AxisX.LabelStyle.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisX.IsLabelAutoFit = false;
chartArea.AxisX.LabelStyle.Enabled = true;
//chartArea.AxisX.IntervalType = DateTimeIntervalType.Number;
//chartArea.AxisX.Interval = 1;
//chartArea.AxisX.IsLabelAutoFit = true;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.IncreaseFont;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep45;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep90;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.StaggeredLabels;
//chartArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
//chartArea.AxisX.LabelStyle.Enabled = false;
//chartArea.AxisX.LabelStyle.Angle = 0;
//chartArea.AxisX.LabelStyle.Angle = 30;
chartArea.AxisY.IsLabelAutoFit = false;
chartArea.AxisY.Title = "Number of Closed Sales";
chartArea.AxisY.LabelStyle.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
chartArea.Area3DStyle.Enable3D = true;
chartArea.Area3DStyle.LightStyle = LightStyle.Simplistic;
chartArea.Area3DStyle.Inclination = 15;
chartArea.Area3DStyle.Rotation = 10;
chartArea.Area3DStyle.WallWidth = 0;
chartArea.BackColor = Color.FromArgb(64, 165, 191, 228);
chartArea.BackGradientStyle = GradientStyle.TopBottom;
chartArea.BackSecondaryColor = Color.Transparent;
chartArea.BorderColor = Color.FromArgb(64, 64, 64, 64);
chartArea.BorderDashStyle = ChartDashStyle.Solid;
chartArea.Name = "Default";
chartArea.Position.Auto = true;
chartArea.ShadowColor = Color.Transparent;
chart.ChartAreas.Clear();
chart.ChartAreas.Add(chartArea);
var legend = new Legend();
legend.BackColor = Color.Transparent;
legend.Enabled = true;
legend.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
legend.IsTextAutoFit = false;
legend.Docking = Docking.Top;
legend.IsDockedInsideChartArea = false;
legend.Alignment = StringAlignment.Center;
legend.DockedToChartArea = "Default";
legend.LegendStyle = LegendStyle.Row;
legend.Name = "Default";
chart.Legends.Clear();
chart.Legends.Add(legend);
chart.ChartAreas["Default"].AxisX.CustomLabels.Clear();
var customLabel1 = new CustomLabel(1, 1, "Product A", 0, LabelMarkStyle.None);
var customLabel2 = new CustomLabel(2, 2, "Product B", 0, LabelMarkStyle.None);
chart.ChartAreas["Default"].AxisX.CustomLabels.Add(customLabel1);
chart.ChartAreas["Default"].AxisX.CustomLabels.Add(customLabel2);
chart.Series.Clear();
var newSeries1 = new Series();
newSeries1.BorderColor = Color.FromArgb(180, 26, 59, 105);
newSeries1.ChartArea = "Default";
newSeries1.ChartType = SeriesChartType.StackedColumn;
newSeries1.IsValueShownAsLabel = false;
newSeries1.Color = Color.FromArgb(255, 0, 0);
newSeries1.Legend = "Default";
newSeries1.Name = "Aaron";
var newSeries2 = new Series();
newSeries2.BorderColor = Color.FromArgb(180, 26, 59, 105);
newSeries2.ChartArea = "Default";
newSeries2.ChartType = SeriesChartType.StackedColumn;
newSeries2.IsValueShownAsLabel = false;
newSeries2.Color = Color.FromArgb(0, 255, 0);
newSeries2.Legend = "Default";
newSeries2.Name = "Tom";
var newSeries3 = new Series();
newSeries3.BorderColor = Color.FromArgb(180, 26, 59, 105);
newSeries3.ChartArea = "Default";
newSeries3.ChartType = SeriesChartType.StackedColumn;
newSeries3.IsValueShownAsLabel = false;
newSeries3.Color = Color.FromArgb(0, 255, 255);
newSeries3.Legend = "Default";
newSeries3.Name = "Ethan";
chart.Series.Add(newSeries1);
chart.Series.Add(newSeries2);
chart.Series.Add(newSeries3);
chart.Series["Aaron"].Points.AddXY(1, 6);
chart.Series["Aaron"].Points.AddXY(2, 3);
chart.Series["Tom"].Points.AddXY(1, 2);
chart.Series["Tom"].Points.AddXY(2, 4);
chart.Series["Ethan"].Points.AddXY(1, 1);
chart.Series["Ethan"].Points.AddXY(2, 7);
我没有足够的声誉来发布图片,但我在这里上传了一张:
https://i.stack.imgur.com/gDOF8.jpg
x轴应显示"产品A"answers"产品B"作为两组堆叠列的标签。自定义标签看起来是显示的,但文本是垂直方向的,无法完全读取。
更改此项:
var customLabel1 = new CustomLabel(1, 1, "Product A", 0, LabelMarkStyle.None);
var customLabel2 = new CustomLabel(2, 2, "Product B", 0, LabelMarkStyle.None);
到此:
var customLabel1 = new CustomLabel(0.5, 1.5, "Product A", 0, LabelMarkStyle.None);
var customLabel2 = new CustomLabel(1.5, 2.5, "Product B", 0, LabelMarkStyle.None);
您的示例不起作用,因为点的范围为0;你可以阅读这里解释的正确方法