C # Win.Forms график с заливкой выше - PullRequest
0 голосов
/ 19 мая 2018

Я использую 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 на текущем графике?Потому что это то, что мне, вероятно, не хватает?Спасибо

@ Тау, не могли бы вы объяснить, что именно вы имеете в виду?Я только что отладил приложение, строящее две пересечения параболы на 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 параболы, сюжет

...