2014-09-21 52 views
1

我正在创建StackedColumn图表,并且无法更改始终垂直定向的自定义x轴标签的方向。我的代码位于包含名为“chart”的Chart对象的Form的Load事件中。无法更改C#图表中自定义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”作为两组堆叠柱的标签。自定义标签似乎显示,但文本垂直方向,不能完全读取。

回答

0

更改此:

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

+1

yw。如果你想角度他们;你可以添加这个到你的例子的结尾:chart.ChartAreas [0] .AxisX.LabelStyle.Angle = 45; – sapbucket 2014-09-22 23:10:52