WindowsForms图表中的绘制/绘制圆

问题描述 投票:0回答:1

是否有可能在WindowsForm Chart中绘制一个圆?

如下所示的方法调用会非常好!

Graph.Series["circle"].Circle.Add(centerX, centerY, radius);

w

c# winforms plot charts draw
1个回答
1
投票

嗯,我为自己创建了一个解决方案。也许对某人有帮助

public void DrawCircle(Chart Graph, double centerX, double centerY, double radius, int amountOfEdges)
{
    string name = "circle_" + centerX + centerY + radius + amountOfEdges;

    // Create new data series
    if (Graph.Series.IndexOf(name) == -1)
        Graph.Series.Add(name);

    // preferences of the line
    Graph.Series[name].ChartType = SeriesChartType.Spline;
    Graph.Series[name].Color = Color.FromArgb(0, 0, 0);
    Graph.Series[name].BorderWidth = 1;
    Graph.Series[name].IsVisibleInLegend = false;

    // add line segments (first one also as last one)
    for (int k = 0; k <= amountOfEdges; k++)
    {
        double x = centerX + radius * Math.Cos(k * 2 * Math.PI / amountOfEdges);
        double y = centerY + radius * Math.Sin(k * 2 * Math.PI / amountOfEdges);
        Graph.Series[name].Points.AddXY(x, y);
    }
}

例如,您可以通过以下方式调用它

DrawCircle(Graph, 5, 4, 3, 30);

大约30点应该足以获得一个漂亮的圆形而不是多边形,但是要取决于图表的大小。

© www.soinside.com 2019 - 2024. All rights reserved.