Display circle in chart with radius in chart units

2019-08-14 04:16发布

Summary: I've tried things like bubble points and messing around with marker size but neither can be set a radius in actual grid units. Bubble chart=% and marker=points(ignores grid)

How can I add a circle to my chart with a radius in grid units?!

----- Old ----- I've looked up how to change the size of a bubble in the bubble chart and apparently the second Y value should control the size... except it does absolutely nothing.

I can not use marker size as a work around because the size needs to be in correct units and not pixels.

The only thing that managed to change the size was the Min and Max size values... but I can't accurately control the size with that (not to mention that would be quite silly).

No code as this Has no special codes or settings and should be easy to replicate.

EDIT: Just realized that bubble sizes are based on other bubble sizes and not the grid. This is a big problem because I need a circle that has a radius in grid units. Also it needs to be alone.

1条回答
狗以群分
2楼-- · 2019-08-14 04:36

I need a circle that has a radius in grid units

There are 3 coordinate system in MSChart:

  • The data values
  • Percentages of some area, most notably the ChartArea and the InnerPlotPosition
  • Pixels of the Chart control's ClientArea

I assume by grid units you mean the data values, which are also used to display grid lines and ticks.

To create a circle or other shapes you can either add a PolygonAnnotation with a GraphicsPath that contains an ellipse, or simply an EllipseAnnotation.

Or you can draw the circle. Here is how to draw:

private void chart_PrePaint(object sender, ChartPaintEventArgs e)
{
    ChartArea ca = chart.ChartAreas[0];
    Axis ax = ca.AxisX;
    Axis ay = ca.AxisY;
    Series s = chart.Series[0];

    DataPoint dpCenter = s.Points[22];

    dpCenter.MarkerStyle = MarkerStyle.Circle;

    double xVal = dpCenter.XValue;
    double yVal = dpCenter.YValues[0];

    double rad = 2;  // <<<<<= radius in values

    float xRad = (float)(ax.ValueToPixelPosition(0) - ax.ValueToPixelPosition(rad));
    float yRad = (float)(ay.ValueToPixelPosition(0) - ay.ValueToPixelPosition(rad));

    float xc = (float)ax.ValueToPixelPosition(xVal);
    float yc = (float)ay.ValueToPixelPosition(yVal);

    Rectangle r = Rectangle.Round(new RectangleF(xc - xRad, yc - yRad, 
        xRad * 2, yRad * 2));

    e.ChartGraphics.Graphics.DrawEllipse(Pens.Red, r );
}

enter image description here

The size is given in data value units. To draw the shape we need toconvert to pixels; for this there are axis functions.

Note that it isn't a perfect circle because the axes don't have the same scales. One could easily pick the max or min of both values, of course.

I draw the circle around a specific point. Position and size will move and scale with the chart itself.

To draw at another position simply calculate it e.g. from the values just like I already do..

查看更多
登录 后发表回答