Switching from Real-time to Historical Data

This method uses two chart pens for the same value - one real-time pen and one historical pen - and uses the Visibility property to switch between the two. Although you can do nearly the same thing by simply changing the Source property on a single pen, this script is more advantageous because the historical data is fetched while the historical pen is invisible. Therefore, operators do not have to wait as long for the historical data to display. The pens are switched when the Back button is clicked, and the Forward button switches back to real-time once the scroll returns to the present time.

The code for these buttons is shown below:

Example: Scroll Back and Scroll Forward Buttons

Private Sub CommandButton2_Click() 'Scroll back button.

     'If the real-time pen is scrolled back in time, switch

     'to historical data.

     If Chart1.Pens.Item(1).ShowLine = 1 Then

          'Make the real-time pen invisible and historical

          'pen visible.

          Chart1.Pens.Item(1).ShowLine = False

          Chart1.Pens.Item(2).ShowLine = True

 

          'Set the historical pen to active.

          Chart1.CurrentPen = 2

 

          'Make real-time pen legend invisible and historic pen

          'legend visible.

          Chart1.Pens.Item(1).Legend.Visible = False

          Chart1.Pens.Item(2).Legend.Visible = True

 

          'Change the data type indicator.

          Text26.Caption = "Historical Data"

     End If

 

     'If it's a historical pen, scroll the chart backward.

     Chart1.ScrollBack

End Sub

 

Private Sub CommandButton1_Click() 'scroll forward button

     'If it's a real-time pen, scrolling forward is

     'not available.

     If Chart1.Pens.Item(1).ShowLine = 1 Then

          MsgBox "Can't move into the future!", _

          vbExclamation, "This isn't a Time Machine."

          Exit Sub

     End If

 

     'If historical pen, scroll the chart forward.

     Chart1.ScrollForward

 

     'If historical pen is scrolled beyond current

     'time, switch back to real-time.

     If Chart1.EndTime >= Now Then

          'Make the historical pen invisible and

          'real-time pen visible.

          Chart1.Pens.Item(2).ShowLine = False

          Chart1.Pens.Item(1).ShowLine = True

 

          'Set the real-time pen to active.

          Chart1.CurrentPen = 1

 

          'Make historical pen legend invisible

          'and real-time pen legend visible.

          Chart1.Pens.Item(1).Legend.Visible = True

          Chart1.Pens.Item(2).Legend.Visible = False

 

          'Change the data type indicator.

          Text26.Caption = "Real-Time Data"

     End If

End Sub