从单个csv文件绘制多个y值

本文关键字:绘制 单个 csv 文件 | 更新日期: 2023-09-27 18:13:30

我在一个图表上绘制多个Y值时遇到了麻烦。以下代码仅适用于单个y值,但不会绘制来自同一csv数据文件的多个y值。

csv文件的格式为:日期戳,值1,值2,值3

我可以让图表显示日期戳与csv文件中的任何列y数据点,但不会显示其他y值。

是的,很明显我新的OOP和winforms,但我正在尝试。当我在PDP11上编程的时候,它是Fortran。

无论如何,这段代码是从这里的某个地方,但我还没有能够找到我正在寻找的答案

表单是一个图表控件和一个按钮。

谢谢你的帮助

7/27


这段代码可以工作,但是否有更好的方法来加载多个Y值?

而且,代码已经被修改了。

如果将它们加载到数据表中,则将数据表绑定到图表。这不会占用内存空间吗?

            private void button1_Click(object sender, EventArgs e)
    {
        // Full path to the data source file
        string file = "testlog2.csv";
        string path = @"g:'";
        // Create a connection string.
        string filepPathConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
            path + ";Extended Properties='"Text;HDR=No;FMT=CSVDelimited'"";
        // Create Connection Object
        OleDbConnection myConnection = new OleDbConnection(filepPathConnectStr);
        // Create a database command on the connection using query
        string mySelectQuery = "Select * from " + file;
        OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
        // Open the connection and create the reader
        myCommand.Connection.Open();
        OleDbDataReader myReader = myCommand.ExecuteReader();
        // Column 1 is a time value, column 2 is a double
        // databind the reader to the chart using the DataBindXY method
        Chart1.Series["Series1"].Points.DataBindXY(myReader, "0", myReader, "1");
        //Chart1.Series["Series2"].Points.DataBindY(myReader, "3");

        myReader.Close();

myConnection.Close ();

    }
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
       // Full path to the data source file
        string file = "testlog2.csv";
        string path = @"g:'";
        // Create a connection string.
        string filepPathConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
            path + ";Extended Properties='"Text;HDR=No;FMT=CSVDelimited'"";
        // Create Connection Object
        OleDbConnection myConnection = new OleDbConnection(filepPathConnectStr);
        // Create a database command on the connection using query
        string mySelectQuery = "Select * from " + file;
        OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
        // Open the connection and create the reader
        myCommand.Connection.Open();
        OleDbDataReader myReader = myCommand.ExecuteReader();
        // Column 1 is a time value, column 2 is a double
        // databind the reader to the chart using the DataBindY method
        Chart1.Series["Series2"].Points.DataBindY(myReader, "3");
        myReader.Close();
        myConnection.Close();
    }
    }
}

从单个csv文件绘制多个y值

您只是从1 y列的数据绑定点。如果希望显示多个y列,可以执行以下两种操作之一:

  1. 代替数据绑定,手动添加点。这将需要循环遍历csv中的所有行,并为每行添加2个(或更多)点:

    series.Points.AddXY(csvLine[0], csvLine[3]);
    series.Points.AddXY(csvLine[0], csvLine[4]);
    

您可以更改不同列中点的颜色来区分它们,但图例不会反映不同的颜色。

  1. 创建多个Series对象,并将每个对象绑定到不同的y列。就像

    Series series2 = chart1.Series.Add("Series2");
    series2.Points.DataBindXY(myReader, "0", myReader, "4");
    

你可以将series2的属性设置为不同的颜色,等等。这是首选的方式,因为它可以为您处理很多东西。