Home Home Company Information Email Japanese

FAQ: XRT/graph www

How do I create XRT/graph images for the web?

XRT widgets have the capability to create graph images interactively or off-line using command-line arguments. The basic idea of creating graph images from the command line is to use resources to specify all the attributes of the graph. Since every visual attribute of the graph can be set this way, we need to specify only the resources and data file and link them using command-line arguments. In a simple case, we do not even need to use a resource file: we can specify everything on the command line:

 % graphutil -show -xrm "*.xrtData: mygraph.dat" -xrm "*.xrtType: typeBar"

This example would create a simple bar graph with data read from the file "mygraph.dat". While effective, this method leads to very long command lines and may be difficult to maintain. Instead, we create a resource file called "mygraph.res":

*.xrtData: mygraph.dat
*.xrtType: typeBar
*.graph.width: 300
*.graph.height: 300

Then we specify this resource file on the command line:

% graphutil -show -Rmygraph.res

Since XRT/graph is an X widget, it needs to connect to an X Windows Server in order to allocate resources such as fonts and colors. This does not mean that the widget ever needs to be displayed on a screen, however. By omitting the -show argument, the application's "XmNmappedWhenManaged"resource is set to "False" and the application is not displayed. In cases where we want to include the graph in reports or other documents or send it to a printer, we would like to use high-resolution PostScript output. This example creates a PostScript file called mygraph.ps without displaying it on the screen:

% graphutil -Rmygraph.res -Pmygraph.ps

We could also send the image directly to the printer using a Unix pipe:

% graphutil -Rmygraph.res -P | lpr

In cases where we want to use exactly what is on the screen for publishing on the World Wide Web, we want to grab an X Window Dump (XWD) of the graph image then convert it to the desired image format:

% graphutil -Rmygraph.res -Xmygraph.xwd

Alternatively, we can pipe the XWD output to the NetPBM utility to produce GIF files directly. This public toolkit for converting images between a large variety of formats is available from:

Image files in GIF format are very compact and ideal for use in Web pages:

% graphutil -Rmygraph.res -X | xwdtopnm | ppmtogif -interlace > mygraph.gif

A typical use of this facility is to call it automatically in a shell script to produce graph images on a regular basis or on demand:

#! /bin/sh
# This script creates a graph image mygraph.gif using the current time
# in the header and the resource file mygraph.res
tmpfile=/tmp/graphutil$$.res
date=`date '+%a %h %d at %r'`;
datestr="Last Updated: $date"
header="("/""Latest Available Results"/"" "/""$datestr"/"/)
hdrres="*.xrtHeaderStrings: "$header""
cp mygraph.res $tmpfile
echo $hdrres >> $tmpfile
(graphutil -R$tmpfile -X | xwdtopnm | ppmtogif -interlace > mygraph.gif; rm $tmpfile) &

Such a script could be called from a nightly cron job.

To place the image in a World Wide Web page, simply place the HTML tag <IMG SRC="mygraph.gif"> in the file.

The source code for this utility is available here:

Graphics demo