【问题标题】:Unable to change text orientation of custom x axis labels in C# chart无法更改 C# 图表中自定义 x 轴标签的文本方向
【发布时间】:2023-04-11 10:44:02
【问题描述】:

我正在创建一个 StackedColumn 图表,但我无法更改自定义 x 轴标签的方向,这些标签始终垂直。我下面的代码在包含名为“图表”的图表对象的表单的加载事件中。

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

  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”作为两组堆叠列的标签。自定义标签似乎已显示,但文本是垂直方向的,无法完全阅读。

【问题讨论】:

    标签: c# charts


    【解决方案1】:

    改变这个:

    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;您可以阅读有关正确方法的信息explained here

    【讨论】:

    • 是的。如果你想调整它们的角度;您可以将其添加到示例的末尾:chart.ChartAreas[0].AxisX.LabelStyle.Angle = 45;
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    相关资源
    最近更新 更多