繁体   English   中英

如何在ms图表中相对于图形点对齐折线注释

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

我已将Polyline注释添加到图形控件中。 但是在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) ,但您可能希望将Annotation实际 DataPoint保持一致,可能是这样的..:

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

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

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

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

你的代码实际上都做到了! 但是 :锚定坐标优先于锚定到DataPoint

这很好,因为你可以将它们组合起来并首先锚定到DataPoint ,然后将其中一个坐标锚定到一个固定值:比如说,x值保持在点上,但y值可能总是为0。

另请注意,您只在折线上添加一条线,而不是在(0,0)处开始,而是在(10,20) ,这可能离锚点很远。

然后是折线本身的尺寸对齐问题!

它具有MSDN 声称的大小,以像素为单位。 这是胡说八道 相反,它以两个相应Axes value单位给出。 调整 Chart 大小时,您可以看到这一点, Annotation也会调整大小; 看截图!

现在对于GraphicsPath及其要点:它们以Annotation Size百分比给出。 为了感觉这一点,添加一个封闭整个区域的测试注释路径:

 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);

另请注意,为了确保没有错误的锚点处于活动状态,我通过将它们设置为double.NaN重置 X/YAnchorX/Ydouble.NaN 这里并不是真的需要,因为这些都是默认值。

这里顺便说一下这个话题的另一篇文章!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM