Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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:

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)
             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
         catch (Exception err)
             //Inform the user if we can't read the file

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);
        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]);

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[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];

        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.

Updated 10-Jan-17 22:06pm

1 solution

Because for each file you read you create new lists lX and lY. You then set these as the DataSource for your ComboBoxes. So when you exit the loop only the last two lists will be set as the sources of the ComboBoxes. You need to create the lists outside your loop and then add every element from each file into the lists. You then add those to the ComboBoxes after all data has been read.
Share this answer
Javiertxl 11-Jan-17 4:28am    
Yup, another guy pointed that out to me on stackoverflow. Managed to read multiple csv files into the graph now.

May I know how could I create legends at the side for the various different files i opened?
Richard MacCutchan 11-Jan-17 4:43am    
Sorry, no idea. I would suggest checking the documentation for the Chart class that you are working with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900