public Form1()
{
InitializeComponent();
chartDruck.Titles.Add("Pressure");
chartVak.Titles.Add("Vacuumpressure");
chartVak.ChartAreas[0].AxisY.Maximum = 1000; //Vakuumgraph
chartTemp.Titles.Add("Temperature");
chartTemp.Legends[0].Docking = Docking.Top;
chartDruck.Legends[0].Docking = Docking.Top;
chartVak.Legends[0].Docking = Docking.Top;
}
public class ReadCSV
{
public DataTable readCSV;
public ReadCSV(string fileName, bool firstRowContainsFieldNames = true)
{
readCSV = GenerateDataTable(fileName, firstRowContainsFieldNames);
}
private static DataTable GenerateDataTable(string fileName, bool firstRowContainsFieldNames = true)
{
DataTable result = new DataTable();
if (fileName == "")
{
return result;
}
string delimiters = ",";
string extension = Path.GetExtension(fileName);
if (extension.ToLower() == "txt")
delimiters = "\t";
else if (extension.ToLower() == "csv")
delimiters = ",";
using (TextFieldParser tfp = new TextFieldParser(fileName))
{
tfp.SetDelimiters(delimiters);
// Get The Column Names
if (!tfp.EndOfData)
{
string[] fields = tfp.ReadFields();
for (int i = 0; i < fields.Count(); i++)
{
if (firstRowContainsFieldNames)
result.Columns.Add(fields[i]);
else
result.Columns.Add("Col" + i);
}
// If first line is data then add it
if (!firstRowContainsFieldNames)
result.Rows.Add(fields);
}
// Get Remaining Rows from the CSV
while (!tfp.EndOfData)
result.Rows.Add(tfp.ReadFields());
}
return result;
}
}
// fileName should consist of the CSV file name with full path
private void LoadCSVOnDataGridView(string fileName)
{
try
{
ReadCSV csv = new ReadCSV(fileName);
try
{
dataGridView1.DataSource = csv.readCSV;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void btnLaden_Click(object sender, EventArgs e)
{
//Einlesen der Abtastung
try
{
this.LoadCSVOnDataGridView(@"D:\MLP\SAMP01\00000\SA00000.csv");
var colCount = dataGridView1.ColumnCount;
int rowcount = dataGridView1.RowCount - 1;
int lastRow = dataGridView1.Rows.Count - 2;
object lastRowValue = dataGridView1.Rows[lastRow].Cells[2].Value;
DateTime.TryParse(dataGridView1.Rows[0].Cells[2].Value.ToString(), out DateTime startZeit);
DateTime.TryParse(dataGridView1.Rows[lastRow].Cells[2].Value.ToString(), out DateTime endZeit);
TimeSpan totalZeit = endZeit - startZeit;
double totalMinutes = totalZeit.TotalMinutes;
int totalMinutesInt = Convert.ToInt32(totalMinutes);
double druck1, druck2;
double vakuumDruck;
double tzMin;
double tzMinOffset;
int tzMinXPlot;
TimeSpan zeitMin0 = startZeit - DateTime.MinValue;
tzMinOffset = zeitMin0.TotalMinutes;
//Druckgraph
for (int i = 1; i < rowcount; i++)
{
//IstDruck
/*druck1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[15].Value);// Englisches Betriebssystem*/
druck1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[15].Value) / 100; //Deutsches Betriebssystem*/
DateTime.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out DateTime Zeit);
TimeSpan zeitMin = Zeit - DateTime.MinValue;
tzMin = zeitMin.TotalMinutes + 1;
tzMinXPlot = Convert.ToInt32(tzMin - tzMinOffset);
//Solldruck
/*druck2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[16].Value);//Englisches Betriebssystem */
druck2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[16].Value) / 100;//Deutsches Betriebssystem*/
chartDruck.Series["Series1"].Points.AddXY(tzMinXPlot, druck1);
//chart1.Series["Series1"].Points.AddY(druck1);
chartDruck.Series["Series2"].Points.AddXY(tzMinXPlot, druck2);
}
//Vakuumgraph zeichnen
for (int i = 1; i < rowcount; i++)
{
//Vakuumdruck
vakuumDruck = Convert.ToDouble(dataGridView1.Rows[i].Cells[17].Value);
DateTime.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out DateTime Zeit);
TimeSpan zeitMin = Zeit - DateTime.MinValue;
tzMin = zeitMin.TotalMinutes + 1;
tzMinXPlot = Convert.ToInt32(tzMin - tzMinOffset);
chartVak.Series[0].Points.AddXY(tzMinXPlot, vakuumDruck);
}
//Temperaturgraph zeichnen
double TemperaturP1, TemperaturP2, TemperaturP3;
if (dataGridView1.Rows[1].Cells[8].Value.ToString() != "0")
{
chartTemp.Series.Add("Temperatur P3");
chartTemp.Series[2].ChartType = SeriesChartType.Line;
}
for (int i = 1; i < rowcount; i++)
{
/*TemperaturP1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value); //Englisches Betriebssystem*/
/*TemperaturP2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value); //Englisches Betriebssystem*/
TemperaturP1 = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value) / 10;//Deutsches Betriebssystem*/
TemperaturP2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value) / 10;//Deutsches Betriebssystem*/
DateTime.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out DateTime Zeit);
TimeSpan zeitMin = Zeit - DateTime.MinValue;
tzMin = zeitMin.TotalMinutes + 1;
tzMinXPlot = Convert.ToInt32(tzMin - tzMinOffset);
chartTemp.Series[0].Points.AddXY(tzMinXPlot, TemperaturP1);
chartTemp.Series[1].Points.AddXY(tzMinXPlot, TemperaturP2);
if (chartTemp.Series[2] != null)
{
/*TemperaturP3 = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value);//Englisches Betriebssystem*/
TemperaturP3 = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value) / 10;//Deutsches Betriebssystem*/
chartTemp.Series[2].Points.AddXY(tzMinXPlot, TemperaturP3);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}