Reputation: 304
I'm conducting a thermal test, where there are 3 setpoint values (with +/1C) I need to confirm. Setpoints are
I have created a Windows Forms Application where I can upload the data from a "CSV" file and plot it on the application.
how do I calculate the average, min, and max values for each setpoint range? (red boxes marked are not from the application)
is there a way to determine a center point for each setpoint and calculate between 2.5 mins either side of the middle?
CSV data looks like this
any comments to help me towards the right direction is much appreciated
Apologies for the very late reply. I have updated the question with the Code and a link to the CSV data
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
namespace TBE_Temperature_Contral_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CHART_CH1.MouseWheel += CHART_CH1_MouseWheel;
CHART_CH2.MouseWheel += CHART_CH2_MouseWheel;
}
private void BTN_Load_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = @"C:\temp",// object of file dialog sent to open file to default c drive
Title = "Browse txt File",// text to show on the bar
CheckFileExists = true,// check whether file exit
CheckPathExists = true,//check file path
DefaultExt = "txt",// the file default extensio
Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*",//filter by default to only txt files
FilterIndex = 2,
RestoreDirectory = true,
ReadOnlyChecked = true,
ShowReadOnly = true
};//creation and inialization of open file dialog
if (openFileDialog1.ShowDialog() == DialogResult.OK)// prompt for a file dialog
{
string SourcePath = openFileDialog1.FileName;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[10] { new DataColumn("No."), new DataColumn("Data"), new DataColumn("Time"), new DataColumn("ID"), new DataColumn("CH1"), new DataColumn("Type_1"), new DataColumn("Unit_1"), new DataColumn("CH2"), new DataColumn("Type_2"), new DataColumn("Unit_2") });
List<string> list = new List<string>();
using (StreamReader sr = new StreamReader(SourcePath))
{
while (sr.Peek() >= 0)
{
list.Add(sr.ReadLine());
}
}
for (int i = 1; i < list.Count; i++)
{
string[] strlist = list[i].Split('\t');
dt.Rows.Add(strlist[0], strlist[1], strlist[2], strlist[3], strlist[4], strlist[5], strlist[6], strlist[7], strlist[8], strlist[9]);
}
var filteredCH136 = dt.AsEnumerable()
.Where(r => r.Field<string>("CH1").Contains("36"));
//dt.DefaultView.RowFilter = "No. LIKE '%" + "36" + "%'";
string FirstCH136 = (from DataRow dr in dt.Rows
where (string)dr["CH1"] == "36"
select (string)dr["No."]).FirstOrDefault();
label1.Text = FirstCH136.ToString();
var filteredCH138 = dt.AsEnumerable()
.Where(r => r.Field<string>("CH1").Contains("38"));
//dt.DefaultView.RowFilter = "No. LIKE '%" + "36" + "%'";
string FirstCH138 = (from DataRow dr in dt.Rows
where (string)dr["CH1"] == "38"
select (string)dr["No."]).FirstOrDefault();
label2.Text = FirstCH138.ToString();
CHART_CH1.Series.Add("CH1_36_38");
CHART_CH1.Series.Add("CH1");
CHART_CH2.Series.Add("CH2");
CHART_CH1.Series["CH1_36_38"].ChartType = SeriesChartType.Line;
CHART_CH1.Series["CH1"].ChartType = SeriesChartType.Line;
CHART_CH1.Series["CH1"].Color = Color.Orange;
CHART_CH2.Series["CH2"].ChartType = SeriesChartType.Line;
CHART_CH2.Series["CH2"].Color = Color.Blue;
//CHART_CH1.Series["CH1_36_38"].Points.AddXY(float.Parse(FirstCH136), 36);
//CHART_CH1.Series["CH1_36_38"].Points.AddXY(float.Parse(FirstCH138), 38);
CHART_CH1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
CHART_CH1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
CHART_CH2.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
CHART_CH2.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
CHART_CH1.DataSource = dt;
CHART_CH2.DataSource = dt;
CHART_CH1.Series["CH1"].XValueMember ="No.";
CHART_CH1.Series["CH1"].YValueMembers = "CH1";
CHART_CH2.Series["CH2"].XValueMember = "No.";
CHART_CH2.Series["CH2"].YValueMembers = "CH2";
//databind
CHART_CH1.DataBind();
CHART_CH2.DataBind();
}
}
private void CHART_CH1_MouseEnter(object sender, EventArgs e)
{
if (!CHART_CH1.Focused)
CHART_CH1.Focus();
}
private void CHART_CH2_MouseEnter(object sender, EventArgs e)
{
if (!CHART_CH2.Focused)
CHART_CH2.Focus();
}
private void CHART_CH1_MouseLeave(object sender, EventArgs e)
{
if (CHART_CH1.Focused)
CHART_CH1.Parent.Focus();
}
private void CHART_CH2_MouseLeave(object sender, EventArgs e)
{
if (CHART_CH2.Focused)
CHART_CH2.Parent.Focus();
}
private void CHART_CH1_MouseWheel(object sender, MouseEventArgs e)
{
var chart = (Chart)sender;
var xAxis = chart.ChartAreas[0].AxisX;
var yAxis = chart.ChartAreas[0].AxisY;
try
{
if (e.Delta < 0) // Scrolled down.
{
xAxis.ScaleView.ZoomReset();
yAxis.ScaleView.ZoomReset();
}
else if (e.Delta > 0) // Scrolled up.
{
var xMin = xAxis.ScaleView.ViewMinimum;
var xMax = xAxis.ScaleView.ViewMaximum;
var yMin = yAxis.ScaleView.ViewMinimum;
var yMax = yAxis.ScaleView.ViewMaximum;
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
var posYStart = yAxis.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
var posYFinish = yAxis.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
yAxis.ScaleView.Zoom(posYStart, posYFinish);
}
}
catch { throw; }
}
private void CHART_CH2_MouseWheel(object sender, MouseEventArgs e)
{
var chart = (Chart)sender;
var xAxis = chart.ChartAreas[0].AxisX;
var yAxis = chart.ChartAreas[0].AxisY;
try
{
if (e.Delta < 0) // Scrolled down.
{
xAxis.ScaleView.ZoomReset();
yAxis.ScaleView.ZoomReset();
}
else if (e.Delta > 0) // Scrolled up.
{
var xMin = xAxis.ScaleView.ViewMinimum;
var xMax = xAxis.ScaleView.ViewMaximum;
var yMin = yAxis.ScaleView.ViewMinimum;
var yMax = yAxis.ScaleView.ViewMaximum;
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
var posYStart = yAxis.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
var posYFinish = yAxis.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
yAxis.ScaleView.Zoom(posYStart, posYFinish);
}
}
catch { throw; }
}
}
}
C# Winform has two char controls as the CSV has two channels of temp data.
CSV data is saved in Pastebin C# Temp Data
I want to check if the temperature is stabilized in the 3 given ranges. it should be stabilized at least for 5 minutes with +/1 degree tolerance. I cannot use min-max and average for the whole list
Upvotes: 0
Views: 449
Reputation: 5986
The running result shows the maximum temperature, minimum temperature, and average temperature within the set value of 37 degrees.
If you want to test the temperature 60, you can replace 60 with 37. (In addition, your txt file does not have 80 degrees.)
I make the following code example.
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace TBE_Temperature_Contral_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CHART_CH1.MouseWheel += CHART_CH1_MouseWheel;
CHART_CH2.MouseWheel += CHART_CH2_MouseWheel;
}
private void BTN_Load_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = @"D:\TestData",// object of file dialog sent to open file to default c drive
Title = "Browse txt File",// text to show on the bar
CheckFileExists = true,// check whether file exit
CheckPathExists = true,//check file path
DefaultExt = "txt",// the file default extensio
Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*",//filter by default to only txt files
FilterIndex = 2,
RestoreDirectory = true,
ReadOnlyChecked = true,
ShowReadOnly = true
};//creation and inialization of open file dialog
if (openFileDialog1.ShowDialog() == DialogResult.OK)// prompt for a file dialog
{
string SourcePath = openFileDialog1.FileName;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[10] { new DataColumn("No."), new DataColumn("Data"), new DataColumn("Time"), new DataColumn("ID"), new DataColumn("CH1"), new DataColumn("Type_1"), new DataColumn("Unit_1"), new DataColumn("CH2"), new DataColumn("Type_2"), new DataColumn("Unit_2") });
List<string> list = new List<string>();
using (StreamReader sr = new StreamReader(SourcePath))
{
while (sr.Peek() >= 0)
{
//MessageBox.Show(sr.ReadLine());
list.Add(sr.ReadLine());
}
}
for (int i = 1; i < list.Count; i++)
{
string[] strlist = list[i].Split(',');
dt.Rows.Add(strlist[0], strlist[1], strlist[2], strlist[3], strlist[4], strlist[5], strlist[6], strlist[7], strlist[8], strlist[9]);
}
var filteredCH136 = dt.AsEnumerable()
.Where(r => r.Field<string>("CH1").Contains("36"));
//dt.DefaultView.RowFilter = "No. LIKE '%" + "36" + "%'";
string FirstCH136 = (from DataRow dr in dt.Rows
where (string)dr["CH1"] == "36"
select (string)dr["No."]).FirstOrDefault();
label1.Text = FirstCH136.ToString();
var filteredCH138 = dt.AsEnumerable()
.Where(r => r.Field<string>("CH1").Contains("38"));
//dt.DefaultView.RowFilter = "No. LIKE '%" + "36" + "%'";
string FirstCH138 = (from DataRow dr in dt.Rows
where (string)dr["CH1"] == "38"
select (string)dr["No."]).FirstOrDefault();
label2.Text = FirstCH138.ToString();
CHART_CH1.Series.Add("CH1_36_38");
string FirstCH137 = (from DataRow dr in dt.Rows
where (string)dr["CH1"] == "37"
select (string)dr["No."]).FirstOrDefault();
string BefNO = (Convert.ToInt32(FirstCH137) - 150).ToString();//Two minutes is 150 seconds
string AftNo = (Convert.ToInt32(FirstCH137) + 150).ToString();
int TmpNum = Convert.ToInt32(AftNo) - Convert.ToInt32(BefNO) +1;
IEnumerable<double> tmp = (from DataRow dr in dt.Rows
where Convert.ToInt32(dr["No."]) >= Convert.ToInt32(BefNO) && Convert.ToInt32(dr["No."]) <= Convert.ToInt32(AftNo)
orderby Convert.ToDouble(dr["CH1"]) descending
select Convert.ToDouble(dr["CH1"]));
double min =tmp.FirstOrDefault();
double max =tmp.LastOrDefault();
IEnumerable<double> TmpSum = (from DataRow dr in dt.Rows
where Convert.ToInt32(dr["No."]) >= Convert.ToInt32(BefNO) && Convert.ToInt32(dr["No."]) <= Convert.ToInt32(AftNo)
select Convert.ToDouble(dr["CH1"]));
double sum = 0;
foreach (var v in TmpSum)
{
sum += v;
}
label3.Text = string.Format("{0:F2}", sum / TmpNum);//Average value
label4.Text = min.ToString();//Max value
label5.Text = max.ToString();//Min value
CHART_CH1.Series.Add("CH1");
CHART_CH2.Series.Add("CH2");
CHART_CH1.Series["CH1_36_38"].ChartType = SeriesChartType.Line;
CHART_CH1.Series["CH1"].ChartType = SeriesChartType.Line;
CHART_CH1.Series["CH1"].Color = Color.Orange;
CHART_CH2.Series["CH2"].ChartType = SeriesChartType.Line;
CHART_CH2.Series["CH2"].Color = Color.Blue;
//CHART_CH1.Series["CH1_36_38"].Points.AddXY(float.Parse(FirstCH136), 36);
//CHART_CH1.Series["CH1_36_38"].Points.AddXY(float.Parse(FirstCH138), 38);
CHART_CH1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
CHART_CH1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
CHART_CH2.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
CHART_CH2.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
CHART_CH1.DataSource = dt;
CHART_CH2.DataSource = dt;
CHART_CH1.Series["CH1"].XValueMember = "No.";
CHART_CH1.Series["CH1"].YValueMembers = "CH1";
CHART_CH2.Series["CH2"].XValueMember = "No.";
CHART_CH2.Series["CH2"].YValueMembers = "CH2";
//databind
CHART_CH1.DataBind();
CHART_CH2.DataBind();
}
}
private void CHART_CH1_MouseEnter(object sender, EventArgs e)
{
if (!CHART_CH1.Focused)
CHART_CH1.Focus();
}
private void CHART_CH1_MouseLeave(object sender, EventArgs e)
{
if (CHART_CH1.Focused)
CHART_CH1.Parent.Focus();
}
private void CHART_CH2_MouseEnter(object sender, EventArgs e)
{
if (!CHART_CH2.Focused)
CHART_CH2.Focus();
}
private void CHART_CH2_MouseLeave(object sender, EventArgs e)
{
if (CHART_CH2.Focused)
CHART_CH2.Parent.Focus();
}
private void CHART_CH1_MouseWheel(object sender, MouseEventArgs e)
{
var chart = (Chart)sender;
var xAxis = chart.ChartAreas[0].AxisX;
var yAxis = chart.ChartAreas[0].AxisY;
try
{
if (e.Delta < 0) // Scrolled down.
{
xAxis.ScaleView.ZoomReset();
yAxis.ScaleView.ZoomReset();
}
else if (e.Delta > 0) // Scrolled up.
{
var xMin = xAxis.ScaleView.ViewMinimum;
var xMax = xAxis.ScaleView.ViewMaximum;
var yMin = yAxis.ScaleView.ViewMinimum;
var yMax = yAxis.ScaleView.ViewMaximum;
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
var posYStart = yAxis.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
var posYFinish = yAxis.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
yAxis.ScaleView.Zoom(posYStart, posYFinish);
}
}
catch { throw; }
}
private void CHART_CH2_MouseWheel(object sender, MouseEventArgs e)
{
var chart = (Chart)sender;
var xAxis = chart.ChartAreas[0].AxisX;
var yAxis = chart.ChartAreas[0].AxisY;
try
{
if (e.Delta < 0) // Scrolled down.
{
xAxis.ScaleView.ZoomReset();
yAxis.ScaleView.ZoomReset();
}
else if (e.Delta > 0) // Scrolled up.
{
var xMin = xAxis.ScaleView.ViewMinimum;
var xMax = xAxis.ScaleView.ViewMaximum;
var yMin = yAxis.ScaleView.ViewMinimum;
var yMax = yAxis.ScaleView.ViewMaximum;
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
var posYStart = yAxis.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
var posYFinish = yAxis.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
yAxis.ScaleView.Zoom(posYStart, posYFinish);
}
}
catch { throw; }
}
}
}
Test Result:
Upvotes: 1
Reputation: 343
I am not able to exactly understand what Setpoint means(pls add some explanation if you can).
However, it is quite easy to perform calculations of Lists.
double minvalue = listname.Min();
double maxvalue = listname.Max();
double avgvalue = listname.Average();
Upvotes: 0