title: Plotting sampling graph on VB.NET
body: <p>I am collecting data from an Arduino and transmit on Visual Basic through the serial port. Now I wanted to plot a graph with time vs electrical energy (unit <a href="http://en.wikipedia.org/wiki/Kilowatt_hour" rel="nofollow noreferrer">kWh</a>) - time along the x-axis and electrical energy along the y-axis. Usually I am getting data of the current from Arduino.</p>
  <p>Now I wanted to learn how to start plotting the graph. I need a simple example explaining plotting a graph for sample. I tried some example code. It seems they are not working.</p>
  <p>How do I plot a graph time vs current read from serial? Once Visual Basic starts running, it should save data w.r.t system time and date.</p>
  <p><a href="http://www.dreamincode.net/forums/topic/325112-how-to-display-hourly-increments-on-x-axis-on-a-chart-graph/" rel="nofollow noreferrer">Example available here</a></p>
  <h3>Current code</h3>
  <pre><code>Imports System
  Imports System.IO.Ports
  Imports System.ComponentModel
  Imports System.Threading
  
  Imports System.Drawing
  
  Public Class Form1
      Dim myPort As Array
      Dim Distance As Integer
  
  
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          myPort = IO.Ports.SerialPort.GetPortNames()
          PortComboBox.Items.AddRange(myPort)
          BaudComboBox.Items.Add(9600)
          BaudComboBox.Items.Add(19200)
          BaudComboBox.Items.Add(38400)
          BaudComboBox.Items.Add(57600)
          BaudComboBox.Items.Add(115200)
          ConnectButton.Enabled = True
          DisconnectButton.Enabled = False
      End Sub
  
      Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
          SerialPort1.PortName = PortComboBox.Text
          SerialPort1.BaudRate = BaudComboBox.Text
          SerialPort1.Open()
          Timer1.Start()
  
          'lblMessage.Text = PortComboBox.Text &amp; &quot; Connected.&quot;
          ConnectButton.Enabled = False
          DisconnectButton.Enabled = True
      End Sub
  
      Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
          SerialPort1.Close()
  
          DisconnectButton.Enabled = False
          ConnectButton.Enabled = True
      End Sub
  
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
          Try
              SerialPort1.Write(&quot;c&quot;)
              System.Threading.Thread.Sleep(250)
              Dim k As Double
              Dim distance As String = SerialPort1.ReadLine()
              k = CDbl(distance)
              ListBoxSensor.Text = k
          Catch ex As Exception
  
          End Try
      End Sub
  
      Private Sub Relay_ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_ON.Click
          SerialPort1.Write(&quot;1&quot;)
      End Sub
  
      Private Sub Relay_Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_Off.Click
          SerialPort1.Write(&quot;0&quot;)
      End Sub
  
  End Class
  </code></pre>