Pixel_95
Pixel_95

Reputation: 984

Plot/Draw Circle in WindowsForms Chart

Is there any possibility to plot a circle in a WindowsForm Chart?

A method-call as follows would be really nice!

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

w

Upvotes: 0

Views: 1037

Answers (1)

Pixel_95
Pixel_95

Reputation: 984

Well, I created myself a work around. Maybe it helps someone

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

You can call it for example via

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

Around 30 points should be enough to get a nice circle instead of a polygon, but depends on the size of your chart.

Upvotes: 1

Related Questions