Author Topic: Chart (FastLine) Interpolation  (Read 1419 times)

Phrog30

  • Guest
Chart (FastLine) Interpolation
« on: November 12, 2019, 04:02:25 PM »
This is not AHMI related, but was curious if anyone could help.  I have a fastline chart that is bound (SQL data).  The chart is setup for x axis as time.  I insert data in the database on data change.  The issue is if no data change occurs the chart will not interpolate between the last point and min/max interval.  Can anyone help on this?  Let me know if I left out important details.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Chart (FastLine) Interpolation
« Reply #1 on: November 12, 2019, 08:52:58 PM »

Phrog30

  • Guest
Re: Chart (FastLine) Interpolation
« Reply #2 on: November 12, 2019, 09:22:54 PM »
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.

Code: [Select]
        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

Phrog30

  • Guest
Re: Chart (FastLine) Interpolation
« Reply #3 on: November 12, 2019, 11:24:11 PM »
Any way to prevent the chart from resizing?  I'm setting X/Y axis min/max so it shouldn't be doing auto scale.  I only have two points and the X axis is a constant 60 seconds.

Phrog30

  • Guest
Re: Chart (FastLine) Interpolation
« Reply #4 on: November 14, 2019, 02:31:45 PM »
My attempt at a SparkLine. Data from SQLite, datalogging via DataSubscriber.  Has desired setpoint range.