GRAPH Examples

From STX Wiki
Jump to navigation Jump to search
Graph Item
INTRODUCTION NEW SET ATTRIBUTES MESSAGES EXAMPLES

Graph Examples

The following scripts demonstrate some of the display, graph and dialog functionality. An alternative to the examples below is to use the macros and classes available (e.g. XPLOT for graphs, ModalDialog for dialogs etc.

Display a Graph and a Dialog

A basic script displaying an x/y plot.

/*++
Macro:  gui_basic_example
Descriptions:
    Creates a basic graphical user interface,
Parameters:
    #standalone Set to 1 if the script is running on it's own
    (i.e. not called by another script). This is the default.
    Otherwise set to 0.
Author:
    Jonnie White
History:
    2006-11-13  First implemented
    2007-10-04  Dialog button now toggles function color
        2009-06-25  Modernising
--*/
[macro gui_basic_example #standalone=1]
    //
    // create and configure display
    //
    #display := new display * 'Display Title' 1 1 /Dialog=Above
    if '$#display[?]' != 'display' then
        em -1 ; display creation failed ($emsg)
    end
    $#display enabled restore 0 0 600 600
    //
    // create spu to generate data for graph
    //
    // generate some data
    #nValues := 100
    #vector := eval fill($#nValues,0,1)
    // create and run spu
    #spu := new spu * table2output $#vector 1 0 1 /n
    if '$#spu[?]' != 'spu' then
        em -1 ; spu creation failed ($emsg) 
    end
    //
    // create and configure graph
    //
    #graph := new graph * 1 $#display 0 0
    if '$#graph[?]' != 'graph' then
        em -1 ; graph creation failed ($emsg)
    end
    theGraph := $#graph // shell variable used for graph commands in message handler
    $#graph axis both both * yellow both both
    $#graph xscale 0 $#nValues-1 '' 'X'
    $#graph yscale 0 $#nValues-1 '' 'Y'
    $#graph y 0 $#spu.x0 xyplot 1 green lines solid 2
    $#graph x $#spu.x0 /a // set x data and apply all graph settings
    //
    // run spu (sending data to graph
    //
    $#spu run 1
    // return if this is not a standalone call
    // caller should clean up
    if '$#standalone' != 1 exit 1
    //
    // create and configure the dialog
    //
    #dialog := new menu * $#display
    if '$#dialog[?]' != 'menu' then
        em -1 ; dialog creation failed ($emsg)
    end
    $#dialog $#dialog[!controls] button 0 0 'Toggle function color'
    $#dialog enabled restored
    //
    // process messages until display is close
    // by the user
    //        
    setmsghandler display $#display gui_basic_example_mh
    //
    // process dialog messages too
    //
    setmsghandler menu $#dialog gui_basic_example_mh
    AppMode := 1
    do while '$AppMode' != '0'
        GetMessage
    end
    setmsghandler display $#display
    //
    // clean up
    //
    delete $#graph $#display $#dialog $#spu
exit

[MACRO gui_basic_example_mh #objtype #objname #msgid #msgpar]
    if '$#objtype' == display || '$#objtype' == shell then
        if '$#msgid' == Close || '$#msgId' == Exit then
            AppMode := 0    
        end
    else if '$#objtype' == 'menu' then
        if '$#msgid' == 'command' && '$#msgpar' == '0' then
            // toggle function color
            fcolor := COND '$fcolor' == 'green' || '$fcolor' == '' ? 'blue' : 'green'
            $theGraph y 0 * * * $fcolor * * * /Apply
        end
    end
exit 1

Display and Processing Cursor Information

The following STx script example demonstrates basic use of the graphical cursors.

[macro gui_cursors_example #standalone=1]
    //
    // create display, graph and dialog
    // and configure
    //
    if '$#standalone' == 1 then
        if '$(load macrocode $scriptdirectory\gui_basic_example.sts)' > 0 em $rc ; $#mac - $emsg
        gui_basic_example 0
    end
    //
    // display cursor styles combobox in dialog
    // and static control for cursor position values.
    //
    cursorStyle := set '0'
    cursorStyleList := set 'cross crosshair hbar vbar hbarcross vbarcross harmonic rangeselection'
    functionBound := set '0'
    functionBoundList := set 'unbound bound'
    //
    // show cursors in graph
    //
    $myGraph cursormode both $cursorStyle on $(int $functionBound-1) off off red yellow /Apply
    //
    //    connect them with a value item, so the value can be
    //    extracted at any time using the syntax $cursors[!data,$#i]
    //
    myCursors := new value *
    if '$myCursors[?]' != 'value' em -1 ; value creation failed ($emsg)
    $myCursors input $myGraph.cursors /Read
    //
    // return if this is not a standalone call
    //
    if '$#standalone' != 1 exit 1 // caller will clean up
    //
    // create dialog controls and show
    // the display
    //
    $myDialog $myDialog[!controls] combobox 0 0 'Cursor Style' cursorStyle * * cursorStyleList
    functionBound := set '0'
    functionBoundList := set 'unbound bound'
    $myDialog $myDialog[!controls] combobox 1 0 'Function Bound' functionBound * * functionBoundList
    staticCtrlId := $myDialog[!controls]
    $myDialog $staticCtrlId static 2 0 'text' * 50 1
    $myDialog enabled restored /Write // update dialog    
    $myDisplay enabled restored 100 100 800 800
    //
    //  process messages until display is close
    //  by the user
    //
    setmsghandler display $myDisplay gui_cursors_example_mh
    setmsghandler menu $myDialog gui_cursors_example_mh
    setmsghandler value $myCursors gui_cursors_example_mh
    AppMode := 1    
    do while '$AppMode' != '0'
        GetMessage
    end
    setmsghandler display $myDisplay
    setmsghandler menu $myDialog
    setmsghandler value $myCursors
    //
    // clean up
    //
    delete $myGraph $myDisplay $myDialog $mySpu $myCursors
exit

[MACRO gui_cursors_example_mh #objtype #objname #msgid #msgpar]
    if '$#objtype' == display || '$#objtype' == shell then
        if '$#msgid' == Close || '$#msgId' == Exit then
            AppMode := 0    
        end
    else if '$#objtype' == 'menu' then
        if '$#msgid' == 'update' then
            $#objname /Read // retrieve new
            $myGraph cursormode both $(word $cursorStyle $cursorStyleList) * $(int $functionBound-1) /Apply
        end
    else if '$#objtype' == 'value' then
        if '$#objname' == '$myCursors' then
            #x1 := format '%.2f' $myCursors[!data,5]
            #y1 := format '%.2f' $myCursors[!data,6]
            #x2 := format '%.2f' $myCursors[!data,10]
            #y2 := format '%.2f' $myCursors[!data,11]
            $myDialog $staticCtrlId * 'x1=$#x1 y1=$#y1 x2=$#x2 y2=$#y2'
        end  
    end
exit 1

Setting Up a Graph's Colors

The following script demonstrates how to set a graphs colors, fonts etc.

[macro gui_colors_example #standalone=1]
    //
    // create display, graph and dialog
    // and configure, display cursors
    //
    if '$#standalone' == 1 then
        if '$(load macrocode $scriptdirectory\gui_basic_example.sts)' > 0 em $rc ; $#mac - $emsg
        gui_basic_example 0
        if '$(load macrocode $scriptdirectory\gui_cursors_example.sts)' > 0 em $rc ; $#mac - $emsg
        gui_cursors_example 0
    end
    //
    // configure graph
    //
    // the title (top left) of the graph
    $myGraph title on 'arial:13:italic' black 'Graph Title'
    // axis data range, units and title
    //    this data is used for the labels as well as the cursor
    //    position data.
    $myGraph xscale 0 $#nValues-1 'X Unit' 'X Title'
    $myGraph yscale 0 $#nValues-1 'Y Unit' 'Y Title'
    // colors / fonts / labels etc
    //    specify the background color
    $myGraph bgcolor hlgray white
    //    specify which axes labels to display, the font and color to use
    $myGraph axis below left 'courier:10' gray below left 'arial:12:bold' black on on
    //    specify which axes to display and whether to display a grid or not
    //    and the number of steps between labels (ticks).
    $myGraph frame below left gray both gray 5 5 -20 -10
    // plot type and data source    
    $myGraph y 0 * * 1 red lines solid 3
    // cursors
    $myGraph cursormode both cross on 0 off off red yellow /Apply
    //
    // set the display position
    //
    $myDisplay enabled restored 100 100 800 800
    //
    //  process messages until display is close
    //  by the user
    //
    setmsghandler display $myDisplay gui_colors_example_mh
    AppMode := 1
    do while '$AppMode' != '0'
        GetMessage
    end
    setmsghandler display $myDisplay
    //
    // clean up
    //
    delete $myGraph $myDisplay $myDialog $mySpu $myCursors
exit

[MACRO gui_colors_example_mh #objtype #objname #msgid #msgpar]
    if '$#objtype' == display || '$#objtype' == shell then
        if '$#msgid' == Close || '$#msgId' == Exit then
            AppMode := 0    
        end
    end
exit 1