Displaying a Diagram on a Form

23.04.2021

This example illustrates a visualization of measured or calculated low-frequency data.

Every second, a new value pair (time plus measured value) is added to a signal. After that, a diagram, which contains this signal as a curve, is updated and also updated in the form. As long as the data set with the acquired values contains less than 100 values, the complete signal is displayed. Otherwise, only the last 100 values are shown.

The example code and the corresponding FlexPro objects are stored in the Visualize.fpd database. The path to the Project Database is usually C:\Users\Public\Documents\Weisang\FlexPro\2021\Examples\VBA\Visualize\Visualize.fpd or C:>Users>Public>Public Documents>Weisang>FlexPro>2021>Examples>VBA>Visualize>Visualize.fpd.

Creating a Form for Diagram Display

The required form only contains an image control that handles the display of the diagram as well as a button for closing the form.

While the form is initialized, the API function SetTimer is used to initialize an interval timer, which calls the UpdateProc procedure once per second. During the termination phase of the form, the timer created is shut down using the KillTimer API function.

Private Sub UserForm_Initialize()

    '   init random number generator (needed for the sample data)

    Randomize

    nTimerID = SetTimer(0, 0, 1000, AddressOf UpdateProc)

End Sub

Private Sub UserForm_Terminate()

    KillTimer 0, nTimerID

End Sub

Inserting and Updating the Diagram

In the UpdateProc procedure,

On Error Resume Next

is used to prevent the procedure from being aborted if an error occurs. Use of the SetTimer API requires this to make sure that the procedure call always returns to Windows. After that, the procedure verifies whether the target signal for the data is already available in the root folder of the database. If that is not the case, it is created and initialized; otherwise, a new row is added to the signal.

If oSignal Is Nothing Then

    Set oSignal = ActiveDatabase.RootFolder.Add("Signal", fpObjectTypeDataSet)

    oSignal.DataStructure = fpDataStructureSignal

    oSignal.DataType(fpDataComponentX)= fpDataTypeCalendarTime

    oSignal.DataType(fpDataComponentY) = fpDataTypeFloat64

    ' we now have a signal with one value

Else ' increase the rows of the signal

    oSignal.NumberOfRows = oSignal.NumberOfRows + 1

End If

The new value is then calculated and appended to the existing signal using the Range object (see also Working with Data Sets).

' you could acquire the data here, in this sample we calculate

' the new value pair

oSignal.Value(fpDataComponentX, , oSignal.NumberOfRows) = Now

oSignal.Value(fpDataComponentY, , oSignal.NumberOfRows) = (fMax - fMin + 1)_

                                                          * Rnd + fMin

At the end of the function, the diagram to be shown in the form is updated and the Picture property of the diagram is assigned to the Picture property of the viewing control field in the form.

Set oDiag = ActiveDatabase.RootFolder.Object("2D-Diagram.2D")

oDiag.Update

Display.DiagramImage.Picture = oDiag.Picture

FlexPro Objects Required for the Visualization

To be able to visualize the data as described above, a diagram and a formula object were created in the root folder of the database. The diagram serves as the basis for the visual presentation in the form. It contains exactly one curve consisting of a signal that provides the LastValuesOfSignal formula. The formula verifies whether the number of values in the signal is larger than the customizable maximum (100 in the example). If this is the case, the formula returns the most recent 100 values of the signal; otherwise, it returns the complete signal.

nMax = 100

nCount = NumberOfRows(Signal)

if nCount < nMax then

 return Signal

else

 return Signal[-nMax, -1]

end

Share article or send as email:

You might be interested in these articles