' FORM1 with ' Chart1 ' ComboBox1 with 12 hours, 7 days, Month, Year ' TextBox1 for StepSize ' Button1 to GO ' DataGridView1 for display of raw ' data if needed ' Needs the data file in a Folder named ' Data in same place as executable Option Strict On Option Explicit On Imports System.IO Imports System.Windows.Forms.DataVisualization.Charting Public Class Form1 Dim path As String = IO.Path.Combine(Application.StartupPath, "Data", "Agriculture_Humidity_Log.csv") Dim dt As New DataTable("Freddy") Dim LogData() As String Dim WithEvents FSW As FileSystemWatcher Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load If Not Directory.Exists(Directory.GetParent(path).FullName) Then MessageBox.Show("Need to have the datafile in a folder named 'Data' alongside the executable file", "ERROR") Directory.CreateDirectory(Directory.GetParent(path).FullName) End If If File.Exists(path) Then FSW = New FileSystemWatcher() With FSW .Path = Directory.GetParent(path).FullName .EnableRaisingEvents = False .NotifyFilter = NotifyFilters.LastWrite .Filter = "*.csv" End With End If With dt .Columns.Add("ID", GetType(Integer)) .Columns.Add("DateTime", GetType(DateTime)) .Columns.Add("Humidity", GetType(Double)) End With With ComboBox1 .Items.AddRange({"12 hours", "7 days", "Month", "Year"}) .SelectedIndex = 0 End With Chart1.Series(0).ChartType = SeriesChartType.Spline ' comment/uncomment the with ... end ' with block below to hide/see data ' in the datagridview (slows down ' performance) 'With DataGridView1 ' .DataSource = dt ' .SelectionMode = DataGridViewSelectionMode.FullRowSelect ' .Columns("ID").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells ' .Columns("DateTime").DefaultCellStyle.Format = "ddd, dd MMM yyyy HH:mm:ss" ' .Columns("DateTime").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill ' .Columns("Humidity").DefaultCellStyle.Format = "0.0" 'End With ReadLogData() End Sub Sub ReadLogData() LogData = ReadData() If LogData Is Nothing Then MessageBox.Show("Data file not found can not continue .......", "ERROR") End End If dt.Clear() With dt Dim id As Integer = 1 For Each s As String In LogData Dim a() As String = s.Split(","c) If a(0) = "Date" Then Continue For Dim comb As DateTime = CDate(String.Join(","c, a(0), a(1))) .Rows.Add(id, comb, a(2)) id += 1 Next End With FSW.EnableRaisingEvents = True End Sub Function ReadData() As String() Dim s() As String = Nothing If File.Exists(path) Then s = File.ReadAllLines(path) End If Return s End Function Dim max As Double = 0.0 Dim min As Double = 9999.9 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click max = 0 : min = 9999 Select Case ComboBox1.SelectedItem.ToString Case "12 hours" MakeGraph(12) Case "7 days" MakeGraph(7 * 24) Case "Month" ' 28 day month MakeGraph(28 * 24) Case "Year" MakeGraph(365 * 24) End Select With Chart1 With .ChartAreas(0).AxisY .Maximum = CInt(max + 1) .Minimum = CInt(min - 1) .Interval = 2 End With .ResetAutoValues() End With End Sub Sub MakeGraph(added As Integer) If dt.Rows.Count < 1 Then Exit Sub Dim stepsize As Integer Integer.TryParse(TextBox1.Text, stepsize) If stepsize < 1 Then Exit Sub Dim start As DateTime = CDate(dt(0)(1)) Chart1.Series(0).Points.Clear() For i As Integer = 0 To dt.Rows.Count - 1 Step stepsize If CDate(dt(i)(1)) > start.AddHours(added) Then Exit Sub End If If CDate(dt(i)(1)) < start Then Continue For With Chart1.Series(0).Points Dim v As Double = CDbl(dt(i)(2)) .AddXY((dt(i)(1)).ToString, v) If v > max Then max = v If v < min Then min = v End With Next End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged ReadLogData() Button1.PerformClick() End Sub Private Sub FSW_Changed(sender As Object, e As FileSystemEventArgs) Handles FSW.Changed FSW.EnableRaisingEvents = False Threading.Thread.Sleep(200) ReadLogData() Invoke(Sub() Button1.PerformClick()) End Sub Dim fillDGV As Boolean = False Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click fillDGV = Not fillDGV If Not fillDGV Then With DataGridView1 .Rows.Clear() .Columns.Clear() End With End If End Sub End Class