Chart控件——ChartType雷达外观

Chart Control - Appearance of ChartType Radar

我使用 System.Windows.Forms.DataVisualization.Charting 的图表控件; 在 ChartType = SeriesChartType.Radar 的情况下,在前景中绘制径向线。如何将这些线移到背景中。

    private void Form1_Load(object sender, EventArgs e)
    {

        chart1.ChartAreas.Clear();
        chart1.Series.Clear();
        ChartArea area = chart1.ChartAreas.Add("NewArea");
        Series serie1 = chart1.Series.Add("NewSerie1");
        serie1.ChartArea = "NewArea";
        serie1.ChartType = SeriesChartType.Radar;
        area.AxisY.LineColor = Color.Red;
        area.AxisY.LineWidth = 1;

        for (Int32 j = 0; j <= 72; j++ )
        {
            serie1.Points.AddXY(5 * j, 5 + j % 9);
        }

    }

你真的不能那样做。但是有两个解决方法:

  • 你可以自己画雷达区域,覆盖网格线。不容易但有可能。有关这方面的一些提示,请参阅 ..!

  • 您可以使网格线半透明:Color.FromArgb(64, Color.Red);

Update 仔细观察后,我发现在 Radar 图表上绘图确实与 Polar 版本有些不同,最显着的是 x-值在这里没有任何意义..

所以这是一个例子:

绘制完成于PostPaint:

private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
    Graphics g = e.ChartGraphics.Graphics;
    ChartArea ca = chart1.ChartAreas[0];
    Series s0 = chart1.Series[0];

    List<PointF> points = new List<PointF>();

    for (int i = 0; i < s0.Points.Count; i++)
        points.Add(RadarValueToPixelPosition(s0, i, chart1, ca));

    g.FillPolygon(Brushes.LightSalmon, points.ToArray());
}

此处计算坐标:

PointF RadarValueToPixelPosition(Series s, int index, Chart chart, ChartArea ca)
{
    RectangleF ipp = InnerPlotPositionClientRectangle(chart, ca);

    float phi = (float)( 360f / s.Points.Count * index - 90  );
    float rad = (float)( phi * Math.PI / 180f );
    DataPoint dp = s.Points[index];

    float yMax = (float)ca.AxisY.Maximum;
    float yMin = (float)ca.AxisY.Minimum;
    float radius = ipp.Width / 2f;
    float len =  (float)(dp.YValues[0] - yMin) / (yMax - yMin);
    PointF C = new PointF(ipp.X + ipp.Width / 2f, ipp.Y + ipp.Height / 2f);

    float xx = (float)(Math.Cos(rad) * radius * len);
    float yy = (float)(Math.Sin(rad) * radius * len); 
    return new PointF(C.X + xx, C.Y + yy);
}

为此,我们需要知道内部绘图区域的大小:

RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea CA)
{
    RectangleF IPP = CA.InnerPlotPosition.ToRectangleF();
    RectangleF CArp = ChartAreaClientRectangle(chart, CA);

    float pw = CArp.Width / 100f;
    float ph = CArp.Height / 100f;

    return new RectangleF(CArp.X + pw * IPP.X, CArp.Y + ph * IPP.Y,
                            pw * IPP.Width, ph * IPP.Height);
}

..这又取决于 ChartArea:

的大小
RectangleF ChartAreaClientRectangle(Chart chart, ChartArea CA)
{
    RectangleF CAR = CA.Position.ToRectangleF();
    float pw = chart.ClientSize.Width / 100f;
    float ph = chart.ClientSize.Height / 100f;
    return new RectangleF(pw * CAR.X, ph * CAR.Y, pw * CAR.Width, ph * CAR.Height);
}

请注意,绘图仅使用一种颜色。如果您的 DataPoints 有不同的颜色,您需要调整代码以绘制子集多边形或三角形。