如何将多个CSV文件中的数据读入线图

如何将多个CSV文件中的数据读入线图

本文介绍了如何将多个CSV文件中的数据读入线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的代码能够使用 OpenFileDialog MultiSelect = True 选择多个csv文件。但我的图表似乎只是从一个csv文件获取数据而不是其他文件。



我所有的csv文件只有2列(X轴和Y轴):

Currently my code is able to select multiple csv files using OpenFileDialog and MultiSelect = True. But my graph seems to be only getting data from one csv file and not the others.

All my csv files only have 2 columns (X and Y Axis):

Entropy Values,File Offset
5.55675,1024
5.3757,1536
5.68973,2048
...



我需要能够得到来自多个csv文件的数据,并带有一个包含多行的图表(例如,3 csv文件=图中显示的3行)。



什么我试过了:



目前我的代码如下:



GraphDemo.cs


I need to be able to get data from multiple csv files, and come out with a graph that has multiple lines (e.g. 3 csv files = 3 lines shown in the graph).

What I have tried:

Currently my code look like this:

GraphDemo.cs

private void openToolStripMenuItem_Click(object sender, EventArgs e)
 {
     Stream myStream = null;
     OpenFileDialog ff = new OpenFileDialog();

     ff.InitialDirectory = "C:\\";
     ff.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
     ff.Multiselect = true;
     ff.FilterIndex = 1;
     ff.RestoreDirectory = true;

     if (ff.ShowDialog() == DialogResult.OK)
     {
         try
         {
             foreach (String file in ff.FileNames)
             {
                 myStream = File.OpenRead(file);
                 rr = null;
                 rr = new Read(myStream);
                 string[] header = rr.get_Header();
                 List<string> lX = new List<string>();
                 List<string> lY = new List<string>();
                 for (int i = 0; i < header.Length; i++)
                 {
                     lX.Add(header[i]); lY.Add(header[i]);
                 }
                 //Populate the ComboBoxes
                 xBox.DataSource = lX;
                 yBox.DataSource = lY;
                 // Close the stream
                 myStream.Close();
             }
         }
         catch (Exception err)
         {
             //Inform the user if we can't read the file
             MessageBox.Show(err.Message);
         }
     }
 }



Read.cs


Read.cs

class Read
{
    private string[] header;
    private float[,] data;
    private int nLines;
    private int nColumns;

    public Read(Stream myStream)
    {
        string aux;
        string[] pieces;

        //read the file line by line
        StreamReader sr = new StreamReader(myStream);
        aux = sr.ReadLine();
        header = aux.Split(',');
        nColumns = header.Length;
        nLines = 0;
        while ((aux = sr.ReadLine()) != null)
        {
            if (aux.Length > 0) nLines++;
        }

        //read the numerical data from file in an array
        data = new float[nLines, nColumns];
        sr.BaseStream.Seek(0, 0);
        sr.ReadLine();
        for (int i = 0; i < nLines; i++)
        {
            aux = sr.ReadLine();
            pieces = aux.Split(',');
            for (int j = 0; j < nColumns; j++)
            {
                data[i, j] = float.Parse(pieces[j]);
            }
        }
        sr.Close();
    }



Plot.cs


Plot.cs

class Plot
{
    public Plot(Read rr, ComboBox xBox, ComboBox yBox, Chart chart)
    {
        int indX = xBox.SelectedIndex;
        int indY = yBox.SelectedIndex;
        float[,] data = rr.get_Data();
        int nLines = rr.get_nLines();
        int nColumns = rr.get_nColumns();
        string []header = rr.get_Header();

        chart.Series.Clear(); //ensure that the chart is empty
        chart.Series.Add("Series0");
        chart.Series[0].ChartType = SeriesChartType.Line;
        chart.ChartAreas[0].AxisX.LabelStyle.Format = "{F2}";
        chart.ChartAreas[0].AxisX.Title = header[indX];
        chart.ChartAreas[0].AxisY.Title = header[indY];

        chart.Legends.Clear();
        for (int j = 0; j < nLines; j++)
        {
            chart.Series[0].Points.AddXY(data[j, indX], data[j, indY]);
        }
    }
}



我在编程方面非常糟糕,如果有人可以帮助我,我将不胜感激。



谢谢。


I am quite bad at programming, would appreciate if someone could help me on this.

Thanks.

推荐答案


这篇关于如何将多个CSV文件中的数据读入线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:29