c# - 使用多边形注释在图表中绘制

标签 c# charts mschart

我正在尝试使用 PolygonAnnotation 在图表中绘制一个多边形,其中我有四个点想用作顶点。

在我的图表下方

enter image description here chart1

这些点是在运行时在气泡系列中设置的:

System.Windows.Forms.DataVisualization.Charting.Series serieSA = chartCartesian.Series.FindByName("SeriesSafetyArea");

DataPoint dpA = new DataPoint(radarConfigured.SafetyArea.PointA.X,new Double[]{radarConfigured.SafetyArea.PointA.Y, -20});
DataPoint dpB = new DataPoint(radarConfigured.SafetyArea.PointB.X, new Double[]{radarConfigured.SafetyArea.PointB.Y, -20});
DataPoint dpC = new DataPoint(radarConfigured.SafetyArea.PointC.X, new Double[] { radarConfigured.SafetyArea.PointC.Y, -20 });
DataPoint dpD = new DataPoint(radarConfigured.SafetyArea.PointD.X, new Double[] { radarConfigured.SafetyArea.PointD.Y, -20 });
                dpA.Label = "A";
                dpB.Label = "B";
                dpC.Label = "C";
                dpD.Label = "D";
                serieSA.Points.Add(dpA);
                serieSA.Points.Add(dpB);
                serieSA.Points.Add(dpC);
                serieSA.Points.Add(dpD);

我想使用这些点获得一个像矩形的图形。 我尝试过使用此代码:

safetyAreaAnnotation = new PolygonAnnotation();
            safetyAreaAnnotation.ClipToChartArea = chartCartesian.ChartAreas[0].Name;
            //safetyAreaAnnotation.AnchorX = 10;
            //safetyAreaAnnotation.AnchorY = 20;
            PointF[] points = new PointF[4];
            points[0].X = (float)dpA.XValue;
            points[0].Y = (float)dpA.YValues[0] ;
            points[1].X = (float)dpB.XValue;
            points[1].Y = (float)dpB.YValues[0];
            points[2].X = (float)dpC.XValue;
            points[2].Y = (float)dpC.YValues[0];
            points[3].X = (float)dpD.XValue;
            points[3].Y = (float)dpD.YValues[0];
            byte[] type = new byte[4];
            type[0] = (byte)PathPointType.Start;
            type[1] = (byte)PathPointType.Line;
            type[2] = (byte)PathPointType.Line;
            type[3] = (byte)PathPointType.CloseSubpath;

            safetyAreaAnnotation.GraphicsPath=new System.Drawing.Drawing2D.GraphicsPath(points,type);  
           // //safetyAreaAnnotation.IsSizeAlwaysRelative = false;
           // //safetyAreaAnnotation.BackColor = Color.Red;
           safetyAreaAnnotation.AxisX = chartCartesian.ChartAreas[0].AxisX;
           safetyAreaAnnotation.AxisY = chartCartesian.ChartAreas[0].AxisY;
           safetyAreaAnnotation.AnchorDataPoint = serieSA.Points[0];
           // //safetyAreaAnnotation.AnchorX = 1;
           // //safetyAreaAnnotation.AnchorY = 20;
            chartCartesian.Annotations.Add(safetyAreaAnnotation);

但是这不起作用。我的图表中没有显示任何内容。 也许我应该使用不同的东西来绘制这个多边形?

更新

这是绘制多边形后的结果:

enter image description here

最佳答案

1 - 注释

您的注释未显示,因为您没有为其设置尺寸。

来自MSDN on PolyLines :

Remarks

A polyline must use coordinates relative to an annotation object, where (0,0) denotes the top-left coordinates and (100,100) denotes the bottom-right coordinates of the annotation.

因此,您需要做的第一件事就是给出 Annotation一个Size :

safetyAreaAnnotation.Width = 10;
safetyAreaAnnotation.Height = 10;

这使得它相当大,如 SizeChartArea 的百分比给出..

更多信息请参见MSDN on Height etc..

但即使您找到合适尺寸,从DataPoint设置折线坐标也相当困难。那些相对于 Annotation 的值面积..

不推荐!


2 - 绘图

您可以在 Chart 上绘图Pre- or PostPaint事件:

    ChartArea ca = chartCartesian.ChartAreas[0];
    Axis ax = ca.AxisX;
    Axis ay = ca.AxisY;

    List<DataPoint> dp = new List<DataPoint>() { dpA, dpB, dpC, dpD };
    List<PointF> points = dp.Select(x=> new PointF(
        (float)ax.ValueToPixelPosition(x.XValue), 
        (float)ay.ValueToPixelPosition(x.YValues[0])
        )).ToList();
    using (SolidBrush brush = new SolidBrush(Color.FromArgb(64, Color.Gold)))
       e.ChartGraphics.Graphics.FillPolygon(brush, points.ToArray());

只需确保代码可以访问 DataPoints .

如您所见,这相当简单:它利用 ValueToPixelPosition Axes的方法.

这与 DataPoints 直接相关,因此在调整大小时它会自动缩放和移动。 我强烈建议使用 Annotation 绘制该区域 .

enter image description here enter image description here


顺便说一句:将多边形添加到 GraphicsPath 的方法过于复杂。 AddPolygon(PointList.ToArray())本来也可以。只有当您想要圆角和尖角的复杂组合时,您才会真正开始设置 Types数组..

关于c# - 使用多边形注释在图表中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38391720/

相关文章:

c# - 在 DataGridView 中隐藏滚动条

C# HttpWebRequest.Write 不发送内容

javascript - 如何将多个数组添加到剑道图表

javascript - 气泡图 : how to avoid bubbles being cut off? 谷歌可视化

c# - MS 图表和 NaN

c# - 跟踪按钮点击次数

c# - WP工具包 : TiltEffect Doesn't Seem to be Affecting Image

javascript - 在 Node js 中直接将 chart.js 图表创建为 PNG?

asp.net - System.Web.UI.DataVisualization.Charting.Grid 在 GAC 中存在两次

c# - 如何在 MSChart 中设置线条颜色?