Writing scripts > Manipulating charts

Additional pen functions

Setting the Properties of Multiple Pens with One Call

Some Pen properties are also exposed at the Chart level, which allows you to set the properties of all Pens within a Chart with one call. In some cases, the Chart property will reflect the value of the CurrentPen. Keep in mind that if you have customized the properties of one of the Pens, setting one of these properties through the Chart will overwrite any previous changes.

The following Pen properties can be set through the Chart:

AutoScaleDisplayLimits

DaysBeforeNow

Duration

EndTime

FixedDate

FixedTime

GetDuration

GetInterval

GetTimeBeforeNow

HorizontalGridColor

HorizontalGridStyle

Interval

NumberofHorizontalGridLines

NumberofVerticalGridLines

ScrollBack

ScrollForward

SetDuration

SetInterval

SetTimeBeforeNow

ShowDate

ShowHorizontalGrid

ShowVerticalGrid

ShowLegend

ShowTimeAxis

ShowTimeAxisTitle

ShowValueAxis

ShowValueAxisTitle

StartDateMode

StartTime

StartTimeMode

TimeAxisNumLabels

TimeAxisNumTicks

TimeAxisTitle

ValueAxisNumLabels

ValueAxisNumTicks

ValueAxisTitle

VerticalGridColor

VerticalGridStyle

Adding a Pen

To add a new pen to a chart, use the following syntax:

    Chart.AddPen("Fix32.NODE.TAG.F_CV")

In the configuration environment, adding a pen will expand the chart by the height of the legend line. At run-time, the chart does not expand. Instead, the plot area shrinks.

Deleting a Pen

Deleting a pen is easy. For example, if you have a chart with three pens and you want to delete the second, use the following syntax:

Chart1.DeletePen(2)

The deletion will not work if there is a script referencing the name of the pen you are trying to delete.

For example:

Pen2.PenLineColor = 255
Chart1.DeletePen(2)

This deletion will fail because there is a script explicitly using Pen2. Instead, use the following script sequence:

Private Sub Rect2_Click()
    Dim pPen As Object
    Set pPen = Chart1.Pens.Item(2)
    pPen.PenLineColor = 255
    Chart1.DeletePen (2)
End Sub

If you delete all your pens, you will create a blank chart. To add the pen back into the chart, open the Chart Configuration dialog box, or use the AddPen method in VBA.

Note

If you are deleting a single pen, and you want to add another, change the pen source via Pen.Source = "Fix32.Node.Tag.f_cv". This will give your chart better performance.

Changing Data Sources in a Pen

The following example shows you how to change data sources in a pen.

Suppose you have a process variable, PumpTemp1 (AI) which is also the input tag for PumpTemp1-History (ETR storing an hour of data), and you are collecting historical data from PumpTemp1. To view the different data associated with PumpTemp1, create three buttons to view the different data, using the script sequence that follows:

Private Sub Rect2_Click()
    Pen1.Source = "Fix32.Area1.PumpTemp1.F_CV"
End Sub

Private Sub Rect3_Click()
    Pen1.Source = "Fix32.Area1.PumpTemp1-History.T_DATA"
End Sub

Private Sub Rect4_Click()
    Pen1.Source = "Hist.Area1.PumpTemp1.F_CV"
    'Now set the start time and fetch
    Pen1.StartTime = #10/31/98 11:30:00 AM#
    Chart1.RefreshChartData
End Sub

The previous example allows you to easily switch between different types of data. This next example changes the data source of a pen by editing an object's Click event.

  1. Create an AI block and a DO block in the database.

  2. Create a chart and add a pen with DO as the data source.

  3. Add a rectangle. Right-click the rectangle and select Edit Script from the pop-up menu.

  4. Enter the following code in the rectangle's Click event:

    Dim Obj As Object
    Dim ChartObj As Object
    Dim Count As Integer
    Dim ChartCount As Integer
    
    Set Obj = 
    Application.ActiveDocument.Page.ContainedObjects
    Count = Obj.Count
    While Count > 0
        If Obj.Item(Count).ClassName = "Chart" Then
            ChartCount = 
    Obj.Item(Count).ContainedObjects.Count
                While ChartCount > 0
                    Set ChartObj = _ 
                    Obj.Item(Count).ContainedObjects._
                    Item(ChartCount)
                    If ChartObj.ClassName = "Pen" Then
                        ChartObj.Source = 
    "dvsys.AI.F_CV"
                    End If
                    ChartCount = ChartCount - 1
                Wend
        End If
        Count = Count - 1
    Wend
    
  5. Switch to the run-time environment and click the rectangle.

    In the Chart, the Pen's Source will change from DO to AI.

Passing in External Data to a Pen

If you have a real-time pen defined, and attempt to pass external (SQL) data to the pen using the SetPenDataArray method, the time and legend values keeps updating with the real-time value.

To avoid this problem, disconnect from the real-time data source before calling the SetPenDataArray method, as show in the following sample code:

Pen2.SetSource "ChartData", True
res=Pen2.SetPenDataArray (count, vtVal, vtDate, vtQual)

where ChartData can be any string.

The SetPenDataArray Method takes arrays of parameters. One of these parameters is quality. This parameter holds the OPC Quality of the data as a numeric constant. When creating your own data in a relational database, you need to specify a value of 192 for this field in order for your data to plot on the chart object.

Keep in mind that the data you pass to a pen does not have to be from a SQL query - it can consist of any external data. To bring this data into a pen, use the call Pen.SetPenDataArray. You can also use GetPenDataArray to extract the data from the pen. Refer to the following example:

Example: Using GetPenDataArray to Extract Data from Pen

Private Sub Rect2_Click()
Dim wrkSpace As Workspace
Dim db_var_name As Database
Dim record_var As Recordset

'Open up the database and the named query
Set wrkSpace = CreateWorkspace("", "admin", "", dbUseJet)
Set db_var_name = wrkSpace.OpenDatabase_
("c:\windows\ChartData.mdb")
Set record_var = db_var_name.OpenRecordset("Data Query",_
dbOpenDynaset)

'Get the number of records in the set
Dim count As Long
record_var.MoveLast
count = record_var.RecordCount
record_var.MoveFirst

Dim row As Integer
Dim col As Integer
Dim value(500) As Double
Dim times(500) As Date
Dim quality(500) As Long
Dim i As Integer

'Loop through and get the data from the record
For i = 0 To count - 1
    value(i) = record_var.Fields("Value").value
    times(i) = record_var.Fields("Time").value
    quality(i) = record_var.Fields("Quality").value
    record_var.MoveNext
Next i
db_var_name.Close

Dim vtVal As Variant
Dim vtDate As Variant
Dim vtQual As Variant
Dim res As Integer

'Set up correct array types
vtVal = value
vtDate = times
vtQual = quality
'Call the pen with the data arrays
res = Pen2.SetPenDataArray(count, vtVal, vtDate, vtQual)
End Sub

The following is sample code for the SetPenDdataArray method with hardcoded values. You can use this code to test or show the functionality of this method.

Example: SetPenDataArray Method with Hardcoded Values

Private Sub Rect2_Click()

Dim objPen As Object
Dim lngNumPoints As Long
Dim dblValue() As Double, dblValuePass As Variant
Dim dteDate() As Date, dteDatePass As Variant
Dim lngQuality() As Long, lngQualityPass As Variant
Dim intResults As Integer, intCounter As Integer

'Set the number of datapoints we are using and
'rediminsion our arrays to match
lngNumPoints = 5
ReDim dblValue(lngNumPoints - 1)
ReDim dteDate(lngNumPoints - 1)
ReDim lngQuality(lngNumPoints - 1)

'Set our array values
dblValue(0) = 10
dblValue(1) = 30
dblValue(2) = 50
dblValue(3) = 20
dblValue(4) = 5

dteDate(0) = #12/13/99 12:30:00 PM#
dteDate(1) = #12/13/99 12:35:00 PM#
dteDate(2) = #12/13/99 12:40:00 PM#
dteDate(3) = #12/13/99 12:45:00 PM#
dteDate(4) = #12/13/99 12:50:00 PM#

For intCounter = 0 To (lngNumPoints - 1)
    lngQuality(intCounter) = 192
Next intCounter

'Pass our array variable to Variant holding Variables
dblValuePass = dblValue()
dteDatePass = dteDate()
lngQualityPass = lngQuality()

Set objPen = Chart1.Pens.Item(1)

intResults = objPen.SetPenDataArray(lngNumPoints, 
dblValuePass, dteDatePass, lngQualityPass)

Set objPen = Nothing

'Set the chart endtime and refresh the data
Chart1.EndTime = #12/13/99 12:50:00 PM#
Chart1.RefreshChartData

End Sub

Using the Pens Collection

The pens contained in a chart are exposed in a collection called Pens. If you are constantly adding and deleting pens, writing specific scripts which operate on those pens may become cumbersome. Another way to write your scripts is to access Chart.Pens.Item(3) rather than access Pen3, for example. The order of the pens in this collection is the order that they appear in the Pen list in the Chart Configuration dialog box and in the legend.

Using the collection will also allow you to avoid any problems deleting pens that have scripts explicitly referencing them. Because of these advantages, we recommend you use the collection when you work with pens often.