【问题标题】:MSChart axis CustomLabel angle at RowIndex > 0RowIndex > 0 处的 MSChart 轴 CustomLabel 角度
【发布时间】:2017-03-03 07:56:57
【问题描述】:

使用 VisualStudio WindowsForms 表单。在设计器中创建图表控件。

我正在尝试沿 WITH 默认标签在图表轴上添加一些 customLabels。 为此,我添加了 RowIndex 属性 =1 的 customLabels。因此,我看到了默认标签和我的 customLabels。 现在的问题是,虽然默认标签旋转正确,但我的自定义标签却没有。

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

我看到了什么:

我想看的:

【问题讨论】:

    标签: c# label axis mschart


    【解决方案1】:

    我认为没有办法做到这一点,真的。原因可能是开发人员认为水平标签根本没有足够的空间,一旦您开始将它们放在一行或多行中..

    这里有一个解决方法:让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);
            }
        }
    }
    

    请根据自己的喜好使用Font 和Brush。结果如下:

    请注意,您可能需要通过调整 ChartArea.Position 或 ChartArea.InnerPlotPosition 来在左侧创建更多空间,这两者都在 100% 中,并且像往常一样默认为“auto”。

    对于更多行,您需要将检查更改为 cl.RowIndex < 0 并为 DrawString 找到一个不错的 x 位置值。

    【讨论】:

    • 感谢您的回复。我认为那是最后的紧急情况。我认为我的任务会更容易,只需自定义所有标签并在 rowindex=0 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多