C#Win.Forms折线图,上面填写

时间:2018-05-19 15:42:50

标签: c# .net forms charts line

我正在使用C#win.forms和图表类在一个图表上绘制几个线函数,但我需要填充它们上面/下面的所有内容,但不知道如何做到这一点。我想象了我对快速绘画(我不是达芬奇)形象的意图。图表上标记的点是我算法的解决方案。

在图表类中有没有办法做到这一点?或者也许任何其他图书馆/方式来做到这一点?谢谢!

enter image description here

编辑:

    public partial class Form1 : Form
{
    public Series sps1, sps2;
    public Form1()
    {
        InitializeComponent();
        sps1 = new Series();
        sps2 = new Series();

        sps1.Name = "Super series 1";
        sps2.Name = "Super series 2";

        sps1.ChartType = SeriesChartType.Spline;
        sps2.ChartType = SeriesChartType.Spline;

        sps1.Points.AddXY(0, 0);
        sps1.Points.AddXY(10, 10);

        sps2.Points.AddXY(0, 10);
        sps2.Points.AddXY(10, 0);

        chart1.Series[0] = sps1;
        chart1.Series.Add(sps2);

    }


    private void chart1_Paint(object sender, PaintEventArgs e)
    {
        // we assume two series variables are set..:
        if (sps1 == null || sps2 == null) return;

        // short references:
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;

        // now we convert all values to pixels
        List<PointF> points1 = sps1.Points.Select(x =>
           new PointF((float)ax.ValueToPixelPosition(x.XValue),
                      (float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();

        List<PointF> points2 = sps2.Points.Select(x =>
           new PointF((float)ax.ValueToPixelPosition(x.XValue),
                      (float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();

        // one list forward, the other backward:
        points2.Reverse();

        GraphicsPath gp = new GraphicsPath();
        gp.FillMode = FillMode.Winding;  // the right fillmode

        // it will work fine with either Splines or Lines:
        if (sps1.ChartType == SeriesChartType.Spline) gp.AddCurve(points1.ToArray());
        else gp.AddLines(points1.ToArray());
        if (sps2.ChartType == SeriesChartType.Spline) gp.AddCurve(points2.ToArray());
        else gp.AddLines(points2.ToArray());

        // pick your own color, maybe a mix of the Series colors..
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(66, Color.DarkCyan)))
            e.Graphics.FillPath(brush, gp);
        gp.Dispose();
    }

}

这是我的简单形式有两行,但我没有像post那样的效果,只有2行,就是这样。我错过了什么?

using (SolidBrush brush = new SolidBrush(Color.FromArgb(66, Color.DarkCyan)))
        e.Graphics.FillPath(brush, gp);
    gp.Dispose();

它是如何工作的,你将图形gp处理到当前图表?因为那就是我可能错过的东西?感谢

@Taw,请你解释一下,你的意思是什么?我刚刚调试了两个100点抛物线相互交叉的应用程序,并意识到它甚至没有进入chart1_Paint方法。

    public partial class Form1 : Form
{
    public Series sps1, sps2;
    public Form1()
    {
        InitializeComponent();
        sps1 = new Series();
        sps2 = new Series();

        sps1.Name = "Super series 1";
        sps2.Name = "Super series 2";

        sps1.ChartType = SeriesChartType.Spline;
        sps2.ChartType = SeriesChartType.Spline;


        for (double i = 0; i < 10; i += 0.1)
        {
            sps1.Points.AddXY(i, 0.2*i*i);
        }


        for (double i = 0; i < 10; i += 0.1)
        {
            sps2.Points.AddXY(i, -0.2 * i*i+20);
        }

        chart1.Series[0] = sps1;
        chart1.Series.Add(sps2);
    }

    private void chart1_Paint(object sender, PaintEventArgs e)
    {
        // we assume two series variables are set..:
        if (sps1 == null || sps2 == null) return;

        // short references:
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;

        // now we convert all values to pixels
        List<PointF> points1 = sps1.Points.Select(x =>
           new PointF((float)ax.ValueToPixelPosition(x.XValue),
                      (float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();

        List<PointF> points2 = sps2.Points.Select(x =>
           new PointF((float)ax.ValueToPixelPosition(x.XValue),
                      (float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();

        // one list forward, the other backward:
        points2.Reverse();

        GraphicsPath gp = new GraphicsPath();
        gp.FillMode = FillMode.Winding;  // the right fillmode

        // it will work fine with either Splines or Lines:
        if (sps1.ChartType == SeriesChartType.Spline) gp.AddCurve(points1.ToArray());
        else gp.AddLines(points1.ToArray());
        if (sps2.ChartType == SeriesChartType.Spline) gp.AddCurve(points2.ToArray());
        else gp.AddLines(points2.ToArray());

        // pick your own color, maybe a mix of the Series colors..
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(66, Color.DarkCyan)))
            e.Graphics.FillPath(brush, gp);
        gp.Dispose();
    }

}

2 parabolas plot

0 个答案:

没有答案