【问题标题】:how to align polyline annotation with respect to graph point in ms chart如何将折线注释相对于 ms 图表中的图形点对齐
【发布时间】:2017-04-14 06:31:14
【问题描述】:

我在图形控件中添加了折线注释。但它在 Addline() 方法中的给定数据点中没有正确对齐。

        PolylineAnnotation annotation = new PolylineAnnotation();
        annotation.AxisX = chart1.ChartAreas[0].AxisX;
        annotation.AxisY = chart1.ChartAreas[0].AxisY;
        annotation.AnchorX = 0;
        annotation.AnchorY = 0;
        annotation.Height = 30;
        annotation.Width = -30;
        annotation.LineWidth = 3;
        annotation.StartCap = LineAnchorCapStyle.None;
        annotation.EndCap = LineAnchorCapStyle.None;
        annotation.Alignment = ContentAlignment.BottomLeft;
        annotation.AnchorAlignment = ContentAlignment.BottomRight;
        annotation.AnchorDataPoint = new DataPoint(this.chart1.Series[0]);

        annotation.AllowAnchorMoving = true;
        annotation.AllowMoving = true;
        annotation.AllowPathEditing = true;
        annotation.AllowResizing = true;
        annotation.AllowSelecting = true;

        annotation.GraphicsPath.AddLine(10, 20, 30, 30);
        chart1.Annotations.Add(annotation);

【问题讨论】:

  • 您的问题解决了吗?
  • 仍然。我没有得到解决该问题的方法。现在我正在寻找类似的带有注释功能的图表控件,无论是winform还是wpf。
  • 我的帖子中有什么不清楚的地方吗?添加矩形对于了解您的情况非常有帮助!
  • 您的解决方案对我很有帮助。但是这个 ms 图表控件不适合我的应用程序要求。感谢您回复我。

标签: c# winforms annotations mschart


【解决方案1】:

Annotations复杂,锚定它们也是如此。

开始很简单:要锚定Annotation,您需要将其AnchorDataPoint 设置为现有 DataPoint

这一行并没有这样做:

annotation.AnchorDataPoint = new DataPoint(this.chart1.Series[0]);

因为新创建的DataPoint 是空的。它已被添加,它的值为(0d, 0d),但您可能希望保持Annotation真实 DataPoint 对齐,可能像这样......:

annotation.AnchorDataPoint = chart1.Series[0].Points[someIndex];

但还有更多:实际上有两种方法可以锚定Annotation

  • 将其锚定到 DataPoint
  • 用固定的AnchorXAnchorY 值锚定它。

(然后您也可以将它们设置为固定的 X 和 Y 值。)

您的代码实际上两者兼而有之! 但是:锚定坐标优先优于锚定到DataPoint

这很好,因为您可以将它们组合起来,首先锚定到 DataPoint,然后将 一个 坐标锚定到固定值:例如,x 值与点保持一致,但 y -value 可能总是为 0..

还请注意,您只在折线中添加了一条线,并且您不是从 (0,0) 开始,而是从 (10,20) 开始,这可能离锚点还有很长的路要走..

然后还有折线本身的大小对齐的问题!

它的大小是MSDN 声称 以像素为单位。这是废话。相反,它以两个各自Axesvalue 单位给出。当您resize Chart Annotation 也会调整大小时,您会看到这一点;看截图!

现在了解GraphicsPath 及其分数:这些以AnnotationSize百分比 给出。要对此有所了解,请添加一个包含整个区域的测试注释路径:

 annotation.GraphicsPath.AddRectangle(new Rectangle(0, 0, 100, 100));

这是我们得到的截图:

如您所见,最合乎逻辑的对齐方式是 TopLeft,在将行翻译成 (0,0) 后,它会紧贴重点。

请注意,我添加了第二个 Series 以使锚点数据点突出。 - 另请注意,虽然 Annotation 的大小是 正方形 (10,10)沿整个图表水平拉伸

这是我使用的完整代码:

PolylineAnnotation annotation = new PolylineAnnotation();
annotation.AxisX = chart1.ChartAreas[0].AxisX;
annotation.AxisY = chart1.ChartAreas[0].AxisY;

annotation.Height = 10;
annotation.Width = 10;
annotation.LineWidth = 3;
annotation.StartCap = LineAnchorCapStyle.None;
annotation.EndCap = LineAnchorCapStyle.None;
annotation.Alignment = ContentAlignment.TopLeft;
annotation.AnchorAlignment = ContentAlignment.TopLeft;

annotation.X = annotation.Y = annotation.AnchorX = annotation.AnchorY = double.NaN;

DataPoint dp = chart1.Series[0].Points[33];
annotation.AnchorDataPoint = dp;
chart1.Series[1].Points.AddXY(dp.XValue, dp.YValues[0]);  // my red points series

annotation.AllowAnchorMoving = true;
annotation.AllowMoving = true;
annotation.AllowPathEditing = true;
annotation.AllowResizing = true;
annotation.AllowSelecting = true;

annotation.GraphicsPath.AddLine(10, 20, 30, 30);
Rectangle r = new Rectangle(0, 0, 100, 100);
annotation.GraphicsPath.AddRectangle(r);

chart1.Annotations.Add(annotation);

还请注意,为了确保没有错误的锚点处于活动状态,我通过将 X/YAnchorX/Y 值设置为 double.NaN重置!这在这里并不真正需要,因为无论如何这些都是默认值..

Here btw is another post on the topic!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多