2017-03-03 131 views
1

使用VisualStudio WindowsForms窗体。在设计器中创建图表控件。MSChart轴RowIndex> 0时的CustomLabel角度

我想在图表上添加一些customLabels沿着的默认标签。 为此,我使用RowIndex property = 1来添加customLabels。因此我看到默认标签和我的customLabels。 现在的问题是,虽然默认标签正确旋转我的自定义标签不是。

Axis属性LabelStyle.Angle仅影响RowIndex = 0中的标签,即默认标签。 如果我将自定义标签放在RowIndex = 0处 - 所有默认标签都将消失。

我看到:

enter image description here

我希望看到的:

enter image description here

回答

0

我看不出有什么办法做到这一点,真的。其原因可能是,开发商决定目前根本不能横向标签足够的空间,一旦你开始把它们放在一个或多个额外行..

这里是一个解决方法:让那些CustomLabels所有透明他们如你喜欢在xxxPaint事件。

下面是一个例子:

我准备了CustomLabels

CustomLabel cl = new CustomLabel(); 
cl.ForeColor = Color.Transparent; 
cl.RowIndex = 1; 
... 

我编写的画是这样的:

private void chart1_PostPaint(object sender, ChartPaintEventArgs e) 
{ 
    Axis ay = chart1.ChartAreas[0].AxisY; 
    foreach (var cl in ay.CustomLabels) 
    { 
     if (cl.RowIndex == 1) 
     { 
      double vy = (cl.ToPosition + cl.FromPosition)/2d; 
      int y = (int)ay.ValueToPixelPosition(vy); 
      e.ChartGraphics.Graphics.DrawString(cl.Text, 
         cl.Axis.TitleFont, Brushes.DarkRed, 0, y); 
     } 
    } 
} 

不要使用自己喜欢的FontBrush 。下面是结果:

enter image description here

请注意,您可能需要创建更多的空间来通过调整ChartArea.PositionChartArea.InnerPlotPosition,这两者都是在100%,和往常一样,默认为“auto”左。

对于更多行,您需要将支票更改为cl.RowIndex < 0并找到DrawString的x位置值。

+1

感谢您的回复。我认为这是最后的紧急情况。我认为这对我的任务来说更简单,只需将所有标签自定义并在rowindex = 0中。 – Prain

+0

事实上,如果这是一个选择,我想我更喜欢这个。 – TaW