[英]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
或 AnchorX
和AnchorY
值锚定它。 (然后你也可以将它们设置为固定的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/Y
和AnchorX/Y
值double.NaN
! 这里并不是真的需要,因为这些都是默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.