XGraph

XGrpah

The xgraph program draws a graph on an X display given data read from either data files or from standard input if no files are specified. It can display up to 64 independent data sets using different colors and/or line styles for each set. It annotates the graph with a title, axis labels, grid lines or tick marks, grid labels, and a legend. There are options to control the appearance of most components of the graph.

A data set consists of an ordered list of points of the form “directive X Y”. For directive “draw”, a line will be drawn between the previous point and the current point. Specifying a “move” directive tells xgraph not to draw a line between the points. “draw” is the default directive. The name of a data set can be specified by enclosing the name in double quotes.

The interface used to specify the size and location of this window depends on the window manager currently in use. Once the window has been opened, all of the data sets will be displayed graphically with a legend in the upper right corner of the screen.

Xgraph also presents three control buttons in the upper left corner of each window: Hardcopy, Close and about xgraph accepts a large number of options most of which can be specified either on the command line, in the user’s .Xdefaults or .Xresources file, or in the data files themselves.

The xgraph can be installed to your linux by using the below code on the terminal window;

In Ubuntu 10.10 you can now install ns,nam and xgraph by just a single command in the Terminal:

$ sudo apt-get install ns2 nam xgraph

You will be prompted for the user password. Enter it and watch Ubuntu 10.10 do the things for You!

The following program shows how XGraph can be used to plot the bandwidth of two nodes connected through full duplex wired link.

#Create a simulator object

set ns [new Simulator]

#Open the output trace file

set f0 [open out0.tr w]

#Create 2 nodes

set n0 [$ns node]

set n1 [$ns node]

#Connect the nodes using duplex link

$ns duplex-link $n0 $n1 1Mb 100ms DropTail

#Define a 'finish' procedure

proc finish {} {

global f0

#Close the output files

close $f0

#Call xgraph to display the results

exec xgraph out0.tr -geometry 800x400 &

exit 0

}

#Define a procedure which periodically records the bandwidth received by the

#the traffic sink0 and writes it to the file f0.

proc record {} {

global sink0 f0

#Get an instance of the simulator

set ns [Simulator instance]

#Set the time after which the procedure should be called again

set time 0.5

#How many bytes have been received by the traffic sinks?

set bw0 [$sink0 set bytes_]

#Get the current time

set now [$ns now]

#Calculate the bandwidth (in MBit/s) and write it to the files

puts $f0 "$now [expr $bw0/$time*8/1000000]"

#Reset the bytes_ values on the traffic sinks

$sink0 set bytes_ 0

#Re-schedule the procedure

$ns at [expr $now+$time] "record"

}

#Create three traffic sinks and attach them to the node n4

set sink0 [new Agent/LossMonitor]

#Start logging the received bandwidth

$ns at 0.0 "record"

$ns at 60.0 "finish"

#Run the simulation

$ns run

xgraph plot of the above program

====================================================================

More improvements on XGraph

In the above program we can see that the xgraph is called and and the output is plotted by using the finish procedure;

ie:

#Define a 'finish' procedure

proc finish {} {

global f0

#Close the output files

close $f0

#Call xgraph to display the results

exec xgraph out0.tr -geometry 800x400 &

exit 0

}

The following commands can be used to improve the appearance of xgraph . Add these commands on the end of "exec xgraph out0.tr -geometry 800x400 &" line in the above finish procedure;and observe the change.

1) -geometry WxH+X+Y or /=WxH+X+Y (Geometry)

Specifies the initial size and location of the xgraph window.

2) -bar (BarGraph)

Specifies that vertical bars should be drawn from the data points to a base point which can be specified with -brb. Usually, the -nl flag is used with this option. The point itself is located at the center of the bar.

3) -fitx

Translate and scale the x data from all datasets to fit [0. . . 1].

4) -fity

Translate and scale the y data from all datasets to fit [0. . . 1].

5) -fmtx <printf-format> -fmty <printf-format>

Use the format specified to generate the legends for the x or y axis.

6) -bb (BoundBox)

Draw a bounding box around the data region. This is very useful if you

prefer to see tick marks rather than grid lines (see -tk).

7) -bd <color> (Border)

This specifies the border color of the xgraph window.

8) -bg <color> (Background)

Background color of the xgraph window.

9) -brb <base> (BarBase)

This specifies the base for a bar graph. By default, the base is zero.

10) -brw <width> (BarWidth)

This specifies the width of bars in a bar graph. The amount is specified

in the user’s units. By default, a bar one pixel wide is drawn.

11) -bw <size> (BorderSize)

Border width (in pixels) of the xgraph window.

12) -fg <color> (Foreground)

Foreground color. This color is used to draw all text and the normal grid lines in the window.

13) -gw (GridSize)

Width, in pixels, of normal grid lines.

14) -gs (GridStyle)

Line style pattern of normal grid lines.

15) -lf <fontname> (LabelFont)

Label font. All axis labels and grid labels are drawn using this font. A font name may be specified exactly (e.g. ”9x15” or ”-*-courier-bold-rnormal-*-140-*”) or in an abbreviated form: ¡family¿-¡size¿. The family is the family name (like helvetica) and the size is the font size in points (like 12). The default for this parameter is ”helvetica-12”.

16) -lnx (LogX)

Specifies a logarithmic X axis. Grid labels represent powers of ten.

17) -lny (LogY)

Specifies a logarithmic Y axis. Grid labels represent powers of ten.

18) -lw width (LineWidth)

Specifies the width of the data lines in pixels. The default is zero.

19) -lx <xl,xh> (XLowLimit, XHighLimit)

This option limits the range of the X axis to the specified interval. This (along with -ly) can be used to ”zoom in” on a particularly interesting portion of a larger graph.

20) -ly <yl,yh> (YLowLimit, YHighLimit)

This option limits the range of the Y axis to the specified interval.

21) -m (Markers)

Mark each data point with a distinctive marker. There are eight distinctive markers used by xgraph. These markers are assigned uniquely to each different line style on black and white machines and varies with each color on color machines.

22) -M (StyleMarkers)

Similar to -m but markers are assigned uniquely to each eight consecutive data sets (this corresponds to each different line style on color machines).

23) -nl (NoLines)

Turn off drawing lines. When used with -m, -M, -p, or -P this can be used to produce scatter plots. When used with -bar, it can be used to produce standard bar graphs.

24) ng (NoLegend)

Turn off drawing Legends. Can be used to increase the drawing area.

25) -t <string> (TitleText)

Title of the plot. This string is centered at the top of the graph.

26) -tf <fontname> (TitleFont)

Title font. This is the name of the font to use for the graph title. A font name may be specified exactly (e.g. ”9x15” or ”-*-courier-bold-r-normal-*-140-*”) or in an abbreviated form: ¡family¿-¡size¿. The family is the family name (like helvetica) and the size is the font size in points (like 12). The default for this parameter is ”helvetica-18”.

27) -x <unitname> (XUnitText)

This is the unit name for the X axis. Its default is ”X”.

30) -y <unitname> (YUnitText)

This is the unit name for the Y axis. Its default is ”Y”.

31) -zg <color> (ZeroColor)

This is the color used to draw the zero grid line.

32) -zw <width> (ZeroWidth)

This is the width of the zero grid line in pixels.

example:

#Define a 'finish' procedure

proc finish {} {

global f0

#Close the output files

close $f0

#Call xgraph to display the results

exec xgraph out0.tr -geometry 800x400 & -bar -fitx -fity -bb -bg white

exit 0

}

you can download an example program for plotting xgraph from here