简体   繁体   English

在RowIndex> 0时,MSChart轴CustomLabel角度

[英]MSChart axis CustomLabel angle at RowIndex > 0

Using VisualStudio WindowsForms Form. 使用VisualStudio WindowsForms表单。 Creating Chart control in designer. 在设计器中创建图表控件。

I'm trying to add some customLabels on charts Axis along WITH the default labels. 我想补充的图表轴一些customLabels 随着默认标签。 To do so I add customLabels with RowIndex property =1. 为此,我添加了带有RowIndex属性= 1的customLabels Thus I see default labels AND my customLabels. 因此,我看到默认标签和我的customLabels。 Now the problem is that while the default labels are rotated correctly my custom labels are not. 现在的问题是,当默认标签正确旋转时,我的自定义标签不是。

The Axis property LabelStyle.Angle affects only labels that are in RowIndex = 0, ie default labels. Axis属性LabelStyle.Angle仅影响RowIndex = 0中的标签,即默认标签。 And if I put customLabels at RowIndex=0 - all default labels will disappear. 如果我将customLabels放在RowIndex = 0 - 所有默认标签都将消失。

What I see: 我所看到的:

在此输入图像描述

What I want to see: 我想看到的:

在此输入图像描述

I see no way to do that, really. 我认为没办法,真的。 The reason is probably that the developers decided there simply cannot be enough space for horizontal labels, once you start putting them in one or more extra rows.. 原因可能是开发人员决定,一旦你开始将它们放在一个或多个额外的行中,就没有足够的空间用于水平标签。

Here is a workaround : Make those CustomLabels all transparent and draw them as you like in a xxxPaint event. 这是一个解决方法 :使这些CustomLabels全部透明,并在xxxPaint事件中根据xxxPaint 绘制它们。

Here is an example: 这是一个例子:

I prepared the CustomLabels : 我准备了CustomLabels

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

And I code the drawing like this: 我像这样编码绘图:

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);
        }
    }
}

Do use a Font and Brush of your own liking. 请使用您自己喜欢的FontBrush Here is the result: 结果如下:

在此输入图像描述

Note that you may need to create more space to the left by tweaking the ChartArea.Position or the ChartArea.InnerPlotPosition , both of which are in 100% and, as usual, default to ' auto '. 请注意,您可能需要通过调整ChartArea.PositionChartArea.InnerPlotPosition来创建更多空间,两者都是100%并且像往常一样默认为“ auto ”。

For even more rows you need to change the check to cl.RowIndex < 0 and find a nice x position value for the DrawString . 对于更多行,您需要将检查更改为cl.RowIndex < 0并为DrawString找到一个不错的x位置值。

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

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