2016-06-09 151 views
1

我正在尝试创建一个带有轴标签的条形图,这些轴标签左对齐并与绘图有特定的距离。 轴标签似乎既没有对齐功能,也没有可能插入填充。填充被忽略。有什么能为它的解决方案:条形图轴标签的左对齐

private void button1_Click(object sender, EventArgs e) 
    { 
     Chart chart1 = new Chart(); 
     chart1.Size = new Size(600, 200); 
     Title mainTitle = new Title("BarChartX"); 
     mainTitle.Alignment = ContentAlignment.TopLeft; 
     mainTitle.Font = new System.Drawing.Font("Arial", 11, FontStyle.Bold); 
     chart1.Titles.Add(mainTitle); 

     chart1.ChartAreas.Add(new ChartArea()); 
     chart1.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Regular); 

     chart1.Series.Add("MySeries1").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar; 
     chart1.Series["MySeries1"].IsVisibleInLegend = false; 
     chart1.Series["MySeries1"].Color = Color.Red; 
     chart1.Series["MySeries1"].Points.AddXY(1, 1); 
     chart1.Series["MySeries1"]["PixelPointWidth"] = "30"; 

     chart1.Series.Add("MySeries2").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar; 
     chart1.Series["MySeries2"].IsVisibleInLegend = false; 
     chart1.Series["MySeries2"].Color = Color.Red; 
     chart1.Series["MySeries2"].Points.AddXY(2, 2); 
     chart1.Series["MySeries2"]["PixelPointWidth"] = "30"; 

     chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0; 

     chart1.ChartAreas[0].AxisX.Interval = 1; 
     chart1.ChartAreas[0].AxisY.Interval = 2; 
     chart1.ChartAreas[0].AxisX.Maximum = 3; 
     chart1.ChartAreas[0].AxisY.Maximum = 6; 

     Font stringFont = new System.Drawing.Font("Arial", 10, FontStyle.Regular);  /// !!!!!!!!!!!! 

     string Label1 = "AxisLabel1".PadRight(12); 
     chart1.ChartAreas[0].AxisX.CustomLabels.Add(0.5, 1.5, Label1); 

     string Label2 = "AxisLabel2"; 
     chart1.ChartAreas[0].AxisX.CustomLabels.Add(1.5, 2.5, Label2); 

     flowLayoutPanel1.Controls.Add(chart1); 

}[enter image description here][1] 

回答

0

虽然与空间填充被忽略/由图表渲染引擎,填充与像非打破空间不同的空格字符修剪确实工作:

enter image description here

char space = (char)0xa0; 
//.. 
string Label2 = "AxisLabel2".PadRight(12, space); 
//.. 
string Label1 = "AxisLabel".PadRight(12, space); 
//.. 

当然,对于填充工作,你需要切换到单间距字体,如Consolas

Font stringFont = new System.Drawing.Font("Consolas", 10, FontStyle.Regular); 

更新:

有可能要比较几个有用white-space人物,最常见的有:

  • (焦炭)0x00a0无间断空间
  • (焦炭)0x2002半角空格
  • (焦炭)0x2003 EM-休息空间
  • (焦炭)0x2008标点符号空间
  • (炭)西班牙语 - 秘鲁薄空间
  • (炭)0x200A头发空间

当然与索拉或其它单间隔的字体的它们都将具有相同的宽度。

OP评论说,他唯一的特殊字符的工作是,奇怪的是在delete字符:

  • (焦炭)0X007F删除字符
+0

感谢您的建议!我实际上无法使其工作: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>> char space =(char)0xa0; chart1.ChartAreas [0] .AxisX.LabelStyle.Font = stringFont; string title1 =“AxisLabel1”; chart1.ChartAreas [0] .AxisX.CustomLabels.Add(0.25,0.75,title1.PadRight(12,space)); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>> 它仍然无视空间。除了字体之外,是否有任何设置需要更改? –

+0

我相信没有其他的设置.. - 你使用什么字体?你应该在windows字符表工具中测试,看看你的字体是否真的包含这个特殊的空格字符!c Consolas的工作原理你可以看到。 – TaW

+0

它与0x7F一起工作,这是非常好的方向,非常感谢! –