如何相对于 MS 图表中的图形点对齐多段线注释

how to align polyline annotation with respect to graph point in ms chart

我在图形控件中添加了折线注释。但是它在 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);

Annotations 复杂的 并且锚定它们也是如此。

它开始相当简单:要锚定一个 Annotation,您需要将其 AnchorDataPoint 设置为一个 现有 DataPoint.

这一行与此完全不同:

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

因为新创建的 DataPoint 是空的。它已被添加并且它的值为 (0d, 0d),但您可能希望保持 Annotationreal DataPoint 对齐,也许像这样..:[=​​48=]

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

但还有更多:实际上有两种方式来锚定一个Annotation:

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

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

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

优先

这很好,因为您可以将它们组合起来,首先锚定到一个 DataPoint,然后将 一个 坐标锚定到一个固定值:比如说,x 值保持点,但 y 值可能始终为 0..

另请注意,您只向多段线添加了一条线,并且不是从 (0,0) 开始,而是从 (10,20) 开始, 可能 离锚点还有一段距离..

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

它的尺寸 MSDN claims 以像素为单位。这是无稽之谈。相反,它以两个相应 Axesvalue 单位给出。当您 resize Chart Annotation 也会调整大小时,您可以看到这一点;看截图!

现在 GraphicsPath 和它的分数:它们以 Size百分比 给出Annotation。为了感受一下,添加一个包含整个区域的测试注释路径:

 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!这里并不是真正需要的,因为无论如何这些都是默认值..