c#折线图。为什么我的线会转到 0 而不是停留在最后一点?

c# line chart. Why does my line go to 0 instead of just staying at the last point?

所以我做了一个按钮,当你按下它时,它会显示一个数组图,但当它完成时它会变为 0。

private void button14_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= period; i++)
        {
            xos[i] = i+1;
            yos[i] = pot[i];
            listBox1.Items.Add(xos[i]);
            listBox1.Items.Add("y " + yos[i]);
        }

        for (int i = 0; i <= period; i++)
        {
         x2os[i] = i + 1;
         y2os[i] = pot2[i];
         listBox2.Items.Add(x2os[i]);
         listBox2.Items.Add("y "+y2os[i]);

        }
        chart1.ChartAreas[0].AxisX.Minimum = 0;
        chart1.Series["Potražnja"].Points.DataBindXY(xos, yos);
        chart1.Series["Predviđanje"].Points.DataBindXY(x2os,y2os);
    }

这是一张图表照片

在知道数组需要多长时间之前不要创建数组,只需声明引用即可:

public double[] xos;
public double[] yos;
public double[] y2os;
public double[] x2os;

然后当你使用它们时,创建实际的数组:

private void button14_Click(object sender, EventArgs e)
{
    xos = new double[period + 1];
    yos = new double[period + 1];
    x2os = new double[period + 1];
    y2os = new double[period + 1];
    for (int i = 0; i <= period; i++)
    {
        xos[i] = i+1;
        yos[i] = pot[i];
        listBox1.Items.Add(xos[i]);
        listBox1.Items.Add("y " + yos[i]);
    }
    for (int i = 0; i <= period; i++)
    {
         x2os[i] = i + 1;
         y2os[i] = pot2[i];
         listBox2.Items.Add(x2os[i]);
         listBox2.Items.Add("y "+y2os[i]);

    }
    chart1.ChartAreas[0].AxisX.Minimum = 0;
    chart1.Series["Potražnja"].Points.DataBindXY(xos, yos);
    chart1.Series["Predviđanje"].Points.DataBindXY(x2os,y2os);
 }

要使黄线跳过第一个零值,您需要先检查这些零值,然后创建数组:

    var start = 0;
    while (start <= period && pot2[start] == 0) {
      start++;
    }
    x2os = new double[period - start + 1];
    y2os = new double[period - start + 1];
    for (int i = 0; i <= period - start; i++)
    {
         x2os[i] = start + i + 1;
         y2os[i] = pot2[start + i];
         listBox2.Items.Add(x2os[i]);
         listBox2.Items.Add("y "+y2os[i]);

    }