我希望看到的MSChart剩余行注释

问题描述 投票:1回答:1

在MSChart的,我用SetAnchor(点8,点12)线注释。如果我滚动图表和隐藏点8,也看不出剩余线注释(点9至12)。我想看看剩下的行注释。帮我!

我的参考; Samples Environments for Microsoft Chart Controls - >图表功能 - >注释 - >注释锚定

private void AddLineAnnotation()
{
    // create a line annotation
    LineAnnotation annotation = new LineAnnotation();

    // setup visual attributes
    annotation.StartCap = LineAnchorCapStyle.Arrow;
    annotation.EndCap = LineAnchorCapStyle.Arrow;
    annotation.LineWidth = 3;
    annotation.LineColor = Color.OrangeRed;
    annotation.ShadowOffset = 2;
    annotation.ClipToChartArea = "Default";

    // prevent moving or selecting
    annotation.AllowMoving = false;
    annotation.AllowAnchorMoving = false;
    annotation.AllowSelecting = false;

    if(Chart1.Series[0].Points.Count > 13)
    {
        // Use the Anchor Method to anchor to points 8 and 12...
        annotation.SetAnchor(Chart1.Series[0].Points[8], Chart1.Series[0].Points[12]);
    }

    // add the annotation to the collection
    Chart1.Annotations.Add(annotation);
}

enter image description here

c# annotations mschart
1个回答
2
投票

这是一个棘手的一个。

坏消息是:我不认为这是可能的。我认为MSChart会省略开始可见区域之外的所有注释。也许理由是为了避免混乱,但谁可以告诉..?

一种解决方法时,有两个端点外兼顾的情况下,我们仍然希望看到的注解..

好消息是,随着ownerdrawing可以编写一个解决方法,确实将借鉴这两种情况的线。

下面的示例示出了绘图代码。确保模式用于拖动缩放和拖动画出一个新的注释分开。我用一个复选框和CheckChanged事件。

让我们先来看看它在行动:

enter image description here

当一个注释的开始滚动了,行了在绘制套。非常难以发现..

下面是一个xxxPaint事件的代码:

private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
    // loop only over line annotations:
    List<LineAnnotation> annos =
        chart1.Annotations.Where(x => x is LineAnnotation)
                            .Cast<LineAnnotation>().ToList();
    if (!annos.Any()) return;

    // a few short references
    Graphics g = e.ChartGraphics.Graphics;
    ChartArea ca = chart1.ChartAreas[0];
    Axis ax = ca.AxisX;
    Axis ay = ca.AxisY;

    // we want to clip the line to the innerplotposition excluding the scrollbar:
    Rectangle r = Rectangle.Round(InnerPlotPositionClientRectangle(chart1, ca));
    g.SetClip(new Rectangle(r.X, r.Y, r.Width, r.Height - (int)ax.ScrollBar.Size));
    g.InterpolationMode = InterpolationMode.NearestNeighbor;  // pick your mode!
    foreach (LineAnnotation la in annos)
    {
        if (Double.IsNaN(la.Width)) continue;  // *
        // calculate the coordinates
        int x1 = (int)ax.ValueToPixelPosition(la.AnchorX);
        int y1 = (int)ay.ValueToPixelPosition(la.AnchorY);
        int x2 = (int)ax.ValueToPixelPosition(la.AnchorX + la.Width);
        int y2 = (int)ay.ValueToPixelPosition(la.AnchorY + la.Height);

        // now we draw the line if necessary:
        if (x1 < r.X || x1 > r.Right)
            using (Pen pen = new Pen(la.LineColor, 0.5f)) g.DrawLine(pen, x1, y1, x2, y2);
    }
    // reset the clip to allow the system drawing a scrollbar
    g.ResetClip();
}

几点注意事项:

  • 该代码假定(*)的Annotations都固定有AnchorX/Y并有Width/Height集。如果您已经使用固定的不同的方式,你需要适应的代码。
  • 对于剪辑的一部分,我们需要知道InnerPlotPosition的像素尺寸/当前位置。为此,您可以使用例如代码在this link的底部。
  • 我没有任何代码,但一条直线。如果您有资格佩戴您的注释,你可能需要在代码扩展;
© www.soinside.com 2019 - 2024. All rights reserved.