I opted for a hack job. Since I don't really know the ins and outs of the charts, I just bookend the data table with the min/max dates and values if necessary. I'm sure better ways exist but for now it will have to do.
If dt Is Nothing Then
dt = New DataTable
End If
dt.Clear()
Dim TableName = "MyTagName"
Dim startTime As Date = Date.Now.AddSeconds(-chartInterval).ToString("yyyy-MM-dd HH:mm:ss")
Dim endTime As Date = Date.Now.ToString("yyyy-MM-dd HH:mm:ss")
Dim st = startTime.ToString("yyyy-MM-dd HH:mm:ss")
Dim endt = endTime.ToString("yyyy-MM-dd HH:mm:ss")
Dim cnn As New SQLiteConnection
cnn.ConnectionString = "Data Source=" + Application.StartupPath & "\MyHMI\Data\" + TableName + ".s3db"
Try
cnn.Open()
Dim mycommand As New SQLiteCommand(cnn)
mycommand.CommandText = "SELECT * FROM " + TableName + " WHERE t_stamp BETWEEN """ + st + """ AND """ + endt + """"
Dim reader As SQLiteDataReader = mycommand.ExecuteReader()
If reader.HasRows Then
dt.Load(reader)
Else
reader.Close()
mycommand.CommandText = "SELECT * FROM " + TableName + " ORDER BY Id DESC LIMIT 1"
reader = mycommand.ExecuteReader()
dt.Load(reader)
End If
reader.Close()
cnn.Close()
Catch sqlError As SQLiteException
End Try
dt.Rows.Item(0).Item("t_stamp") = startTime
Dim data = dt.Rows.Item(0).Item("data")
If dt.Rows.Count < 2 Then
Dim dr As DataRow = dt.NewRow
dr("Data") = data
dr("t_stamp") = endTime
dt.Rows.InsertAt(dr, dt.Rows.Count)
Else
dt.Rows.Item(dt.Rows.Count - 1).Item("t_stamp") = endTime
End If
Chart1.Series(0).XValueMember = "t_stamp"
Chart1.Series(0).YValueMembers = "Data"
Chart1.Series(0).XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime
Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "yyyy/MM/dd HH:mm:ss"
Chart1.ChartAreas(0).AxisX.Minimum = startTime.ToOADate()
Chart1.ChartAreas(0).AxisX.Maximum = endTime.ToOADate()
Chart1.DataSource = dt