图表控件 - ChartType雷达的外观

时间:2017-04-10 13:19:55

标签: c# .net mschart appearance radar-chart

我使用System.Windows.Forms.DataVisualization.Charting中的Chart Control; 如果是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);
        }

    }

enter image description here

1 个答案:

答案 0 :(得分:1)

你真的不能这样做。但有两种解决方法:

  • 您可以自己绘制雷达区域,从而覆盖网格线。不容易但可能。有关这方面的一些提示,请参阅here

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

更新仔细观察后,我发现在Radar图表上绘图确实与Polar版本略有不同,最值得注意的是因为x值并不是真正的意思这里有什么......

所以这是一个例子:

enter image description here

图纸在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具有不同的颜色,则需要调整代码以绘制绘制子集多边形或三角形。