GTK: Configuring and Using gnuplot

Cogent Tools Demo and Tutorials: Version 4.0
Prev | Chapter 13. Log Functions | Next

# 13.2. GTK: Configuring and Using gnuplot

You can comprehend the data from the Cascade TextLogger more easily by plotting it. Since there is no widget available in GTK that can plot real-time trends for 3 values, we have used gnuplot, a Linux plotting program. But gnuplot was designed primarily for making static plots, so we've had to push it a little to do what we want.

To get two different plots (Recent and All), we use three configuration files and a data file. The `plmain.cfg` configuration file holds commands that are common to both plots, like labels, borders, and data styles. The `plrecent.cfg` and `plall.cfg` files each have different commands to make their unique plots. They each load the `plmain.cfg` file and the continuously-updated data file (`/tmp/cogentdemo/pllogtimes.dat` explained below), and then apply their own commands.

## `pllogtimes.dat`

To simulate a scrolliing trend graph, we put gnuplot into a loop and feed it new values every second. We control the x-axis (the time axis), with this three-line file that is updated by the `prepare_times` function every 1/10 second. Here is a typical example of the contents of `pllogtimes.dat`, written on Thursday, Feb 28, 2002, at 5:00 p.m. and 7 seconds (or 1014933607.7161740064620972 seconds in system time):

        cuttime = 1014930000
        top = 3607.7161740064620972
        bottom = 3592.7161740064620972

The `cuttime` variable is the current time in seconds, minus the last 4 digits. The `plot` commands in `plrecent.cfg` and `plall.cfg` subtract this number from the log time values to reduce them to 4-digit numbers. If we don't do this, the time values will be very high, causing gnuplot to display the x-axis labels in scientific notation, obscuring the number of seconds.

The `top` variable is simply the present time minus the `cuttime`. This value is used to specify the top of the range for `plrecent.cfg`. The `bottom` variable is 15 seconds less than the `top`, used for the bottom of the range in `plrecent.cfg`. The range for `plall.cfg` is the default--all the data.

## `plmain.cfg`

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #DOCUMENT:      plmain.cfg
    #DEPENDENCIES:  none
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    # Set up labels and borders.

    set xlabel "Time" 0,0
    set ylabel "Value" 1,0
    set border
    set tics out
    set grid xtics ytics
    set nokey

    # Set up line width and coloring.  We use the "set linestyle"
    # command to get a number for each line, which then gives us
    # access to the colors we need, in the order we need them.

    set data style lines

    set linestyle 1 lw 1 #red
    set linestyle 2 lw 1 #green
    set linestyle 3 lw 1 #blue
    set linestyle 4 lw 1 #magenta (purple)
    set linestyle 5 lw 1 #light blue
    set linestyle 6 lw 1 #dark brown
    set linestyle 7 lw 1 #orange
    set linestyle 12 lw 1 #magenta
    set linestyle 18 lw 1 #light green
    set linestyle 19 lw 1 #dark blue

    # Set up the terminal to X Windows.

    set terminal x11

    # Then set the y-axis to the range of MV (0 - 200).

    set yrange [0:200]

## `plrecent.cfg`

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #DOCUMENT:      plrecent.cfg
    #DEPENDENCIES:  plmain.cfg
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    set title "Cascade TextLogger Output - Recent"

    load "plmain.cfg"

    # Load the file that holds the present time data (within 1/10
    # second) used for setting labels, as well as the most recent
    # and 15-seconds-ago times used for making the Recent plot.

    load "/tmp/cogentdemo/pllogtimes.dat"

    set xrange [bottom:top]

    # Make the plot.  The cuttime value comes from pllogtimes.dat.  It is used
    # to cut the top digits off the system time, reducing it to a four-digit
    # number, which displays well.  Pause for 1 second, then plot the latest
    # values from the tlrecent.dat and tlold.dat files.  Continue until
    # gnuplot is terminated.

    plot '/tmp/cogentdemo/tlrecent.dat' using (column(1) - cuttime):2 title 'SP' ls 3, \
         '/tmp/cogentdemo/tlrecent.dat' using (column(1) - cuttime):3 title 'MV' ls 2, \
         '/tmp/cogentdemo/tlrecent.dat' using (column(1) - cuttime):4 title 'PV' ls 1, \
         '/tmp/cogentdemo/tlold.dat' using (column(1) - cuttime):2 title 'SP' ls 3, \
         '/tmp/cogentdemo/tlold.dat' using (column(1) - cuttime):3 title 'MV' ls 2, \
         '/tmp/cogentdemo/tlold.dat' using (column(1) - cuttime):4 title 'PV' ls 1

    pause 1
    load "/tmp/cogentdemo/pllogtimes.dat"
    reread

## `plall.cfg`

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #DOCUMENT:      plall.cfg
    #DEPENDENCIES:  plmain.cfg
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    set title "Cascade TextLogger Output - All"

    load "plmain.cfg"

    # Load the file that holds the present time data (within 1/10
    # second) used for setting labels, as well as the most recent
    # and 15-seconds-ago times used for making the Recent plot.

    load "/tmp/cogentdemo/pllogtimes.dat"

    # Make the plot.  The cuttime value comes from pllogtimes.dat.  It is used
    # to cut the top digits off the system time, reducing it to a four-digit
    # number, which displays well.  Pause for 1 second, then plot the
    # tloutput.dat file with the latest data. Continue until gnuplot is
    # terminated.

    plot '/tmp/cogentdemo/tloutput.dat' using (column(1) - cuttime):2 title 'SP' ls 3, \
         '/tmp/cogentdemo/tloutput.dat' using (column(1) - cuttime):3 title 'MV' ls 2, \
        '/tmp/cogentdemo/tloutput.dat' using (column(1) - cuttime):4 title 'PV' ls 1

    pause 1
    reread

Prev | Home | Next
Log Functions | Up | GTK: Preparing Plots - `prepare_times`, `get_recent_data`

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.