【问题标题】:C# Chart Line Annotation disappears when anchor point is off Axis View当锚点关闭轴视图时,C# Chart Line Annotation 消失
【发布时间】:2016-07-05 18:08:46
【问题描述】:

我确定这个问题有一个我不知道的简单解决方案,但无论如何我都会问。我有这个代码:

HorizontalLineAnnotation h = new HorizontalLineAnnotation();
h.AnchorX = startOfNewGraph;
h.Width = newGraphWidth;
h.AxisX = resultGraph.ChartAreas[0].AxisX;
h.AxisY = resultGraph.ChartAreas[0].AxisY;
h.IsSizeAlwaysRelative = false;
h.ClipToChartArea = resultGraph.ChartAreas[0].Name;
originalHLAAnchors.Add(h.AnchorX);
originalHLAWidths.Add(h.Width);
resultGraph.Annotations.Add(h);

我的问题是,当我的图形视图缩放到其 AnchorX 位于视图之外的位置时,注释消失了。我想保持它打开,这样即使注释的两端都不在视图中,我仍然可以看到它们之间的界限。以下是我试图采取的补救措施:

private void resultGraph_AxisViewChanged(object sender, ViewEventArgs e)
    {

        if (e.Axis == resultGraph.ChartAreas[0].AxisX)
        {
            ResizeHorizontalAnnotations();
        }
    }

private void ResizeHorizontalAnnotations()
    {
        int count = 0;
        for (int i = 0; i < resultGraph.Annotations.Count; i++)
        {
            if (resultGraph.Annotations[i] is HorizontalLineAnnotation)
            {
                if (originalHLAAnchors[count] < resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum)
                {
                    resultGraph.Annotations[i].AnchorX = resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum + 0.0005;
                    if ((originalHLAAnchors[count] + originalHLAWidths[count]) >= resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMaximum)
                    {
                        resultGraph.Annotations[i].Width = resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMaximum - resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
                    }
                    else
                    {
                        resultGraph.Annotations[i].Width -= resultGraph.ChartAreas[0].AxisX.ScaleView.ViewMinimum - originalHLAAnchors[count];
                    }
                }
                else
                {
                    resultGraph.Annotations[i].AnchorX = originalHLAAnchors[count];
                    resultGraph.Annotations[i].Width = originalHLAWidths[count];
                }
                resultGraph.Annotations[i].Visible = true;
               count++;
            }
        }
        resultGraph.UpdateAnnotations();
    }

但是,这段代码似乎也不起作用。我第一次尝试时没有显示注释,所以我在 AnchorX 中添加了 0.005 以查看它是否必须超过视图最小值。这也不起作用。当我检查注释的本地值时,数字是正确的。注释要么没有正确显示,要么根本没有显示。 UpdateAnnotations() 会缩小视图并禁用我的注释。

有什么想法吗?

【问题讨论】:

    标签: c# .net visual-studio charts data-visualization


    【解决方案1】:

    看起来像Annotations,它们直接使用AnchorX 属性或使用AnchorDataPoint 锚定到一个位置,一旦该锚不再在视图比例中,它就会消失。

    不确定这是错误还是功能,但您可以通过直接设置位置来解决。

    因此,您应该使用h.Widthh.X 属性来调整Annotation 的大小和位置:

        h.X = startOfNewGraph;
        h.Width = newGraphWidth;
    

    您没有向我们展示您如何垂直定位Annoation;也许锚定到DataPoint?或者通过设置Y 属性?两者现在都应该可以放大 x 轴了。

    请注意Annotation.XAnnotation.AnchorX 都有默认值double.NaN,这在图表术语中通常表示“自动”;在这种情况下,我猜这意味着“不适用”。

    如果同时设置这两个属性,我发现Annotation.X 获胜。因此,将其设置回double.NaN 将使Annotation.AnchorX 再次处于活动状态。

    【讨论】:

    • 我现在终于回到了它,你的第一句话就是答案。我需要做的就是将值设置为 X 而不是 AnchorX,即使 X 不在视线范围内,它也能正常工作。知道这很简单!
    【解决方案2】:

    改为使用系列,您无需担心 .net 图表注释的锚点和调整大小问题:

    private void Line(Point start, Point end)
    {
        chart1.Series.Add("line");
        chart1.Series["line"].ChartType = SeriesChartType.Line;
        chart1.Series["line"].Color = System.Drawing.Color.Red;
        chart1.Series["line"].Points.AddXY(start.X, start.Y);
        chart1.Series["line"].Points.AddXY(end.X, end.Y);
    }
    

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多