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
...
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]);
}
xBox.DataSource = lX;
yBox.DataSource = lY;
myStream.Close();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
Read.cs
class Read
{
private string[] header;
private float[,] data;
private int nLines;
private int nColumns;
public Read(Stream myStream)
{
string aux;
string[] pieces;
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++;
}
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
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();
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.