繁体   English   中英

无法在C#图表中更改自定义x轴标签的文本方向

[英]Unable to change text orientation of custom x axis labels in C# chart

我正在创建StackedColumn图表,但无法更改始终垂直放置的自定义x轴标签的方向。 我下面的代码在包含名为“图表”的Chart对象的Form的Load事件中。

被注释掉的行是我在研究时发现的尝试修复。 这些更改使标签消失或没有任何效果:

  1. 切换图表区域的x轴的IsLabelAutoFit属性。
  2. 更改LabelAutoFitStyle属性(也已通过#1测试)。
  3. 更改IntervalType和Interval属性(也已通过#1测试)。
  4. 切换LabelStyle.Enabled属性(也已通过#1测试)。
  5. 更改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);

我没有足够的声誉来发布图像,但是我在这里上传了一个:

http://imgur.com/JJLyHN8

x轴应显示“产品A”和“产品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。 您可以在此处了解有关执行此操作的正确方法的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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