classes addexpr erase_all line_info vector addobject exec_menu mark vfixed addvar family menu_action view align fastflush menu_remove view_count axis fixed menu_tool view_info begin flush plot xaxis beginline getline printfile xexpr brush gif relative yaxis color glyph save_name crosshair label size erase line unmap
g = new Graph()
g = new Graph(0)
An instance of the Graph class manages a window on which x-y plots can be drawn by calling various member functions. The first form immediately maps the window to the screen. With a 0 argument the window is not mapped but can be sized and placed with the view function.
objref g //Creates an object reference "g" which can be made to //point to any object. g = new Graph() //Assigns "g" the role of pointing to a Graph instance //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.size(0, 10, -1, 1) // specify coordinate system for the canvas drawing area // numbers refer to xmin, xmax, ymin, ymax respectively g.beginline() //The next g.line command will move the drawing pen // to the indicated point without drawing anything for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.line(x, sin(x)) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
The function .line()
, however, only allows the user to plot one function
per for
loop, whereas the function .plot()
can produce several
plots per for
loop and is therefore more effective in comparing plots.
You must use .begin()
and .addvar()
or .addexpr()
in
conjunction with the .plot
function.
objref g g = new Graph() g.size(0, 10, -1, 1) g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g g.addexpr("cos(x)") //stores cos(x) for use with g g.addexpr("exp(-x)") //stores exp(x) for use with g x=0 g.begin() //The next g.plot command will move the drawing pens // for the three curves to indicated x position for(x=0; x<=10; x=x+0.1){ g.plot(x) // The x value used for each expression in the // addexpr list } g.flush()
The size in the above example is appropriate to show the sine waves nicely but the view of the exponential only shows the first few points before it goes out of view. Hold the right mouse button while the mouse in in the graph window and select the "View = plot" menu item to see the entire exponential. Selecting the "Whole Scene" menu item will return to the size specified in the size command.
Graphsee yaxis
Graph
g.xaxis()
g.xaxis(mode)
g.xaxis(xstart, xstop)
g.xaxis(xstart, xstop, ypos, ntic, nminor, invert, shownumbers)
Graph
g.yaxis()
g.yaxis(mode)
g.yaxis(ystart, ystop)
g.yaxis(ystart, ystop, ypos, ntic, nminor, invert, shownumbers)
Note:
It is easiest to control the size of the axes and the scale of
the graph through the graphical user interface. Normally, when a
new graph is declared (eg. g = new Graph()
), the y axis
ranges from 20-180 and the x axis ranges from 50-250.
With the mouse arrow on the graph window, click on the right button
and set the arrow on "View" at the top of the button window
column. A second button
window will appear to the right of the first, and from this button window
you can select several options. Two of the most common are:
g.size(-1,1,-1,1)
makes both axes go from -1 to 1.
Graph
g.addvar("variable")
g.addvar("variable", color_index, brush_index)
g.addvar("label", "variable")
g.addvar("lable", "variable", color_index, brush_index)
g.plot(x)
is called.
The address of the variable is computed so this is fast. The current
color and brush is used if the optional arguments are not present. The name
of the variable is
also added to the graph as a label associated with the line. If the
first two args are strings, then the first "label" arg is associated
with the line on the
graph whereas the second arg defines the variable.
Graph
g.addexpr("expression")
g.addexpr("expression", color_index, brush_index)
g.plot(x)
is called.
The current
color and brush is used if the optional arguments are not present. A label
is also added to the graph that indicates the name of the variable.
The expression is interpreted every time g.plot(x)
is
called so it is more general than addvar , but slower.
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph g.size(0,10,-1,1) //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g graphs g.addexpr("cos(x)") //stores cos(x) for use with g g.addexpr("exp(-x)") //stores exp(x) for use with g x=0 // has to be defined prior to execution of expressions g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(x) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
Graph
g.addobject(rangevarplot)
g.addobject(rangevarplot, color, brush)
Graph
g.begin()
g.plot(x)
is the first point of each graph line.
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g graphs g.addexpr("cos(x)") //stores cos(x) for use with g g.addexpr("-exp(x)") //stores exp(x) for use with g x=0 g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(x) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
Graph
g.plot(x)
for
loop.
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g graphs g.addexpr("cos(x)") //stores cos(x) for use with g g.addexpr("cos(2*x)") //stores cos(2*x) for use with g x=0 g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(x) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
Graph
g.xexpr("expression")
g.xexpr("expression", usepointer)
.plot
is called, while functions
declared by .addexpr
will calculate the y value when .plot
is called.
This can be used for phase plane plots, etc. Note that the normal argument to
.plot
is ignored when such an expression is invoked. When usepointer
is 1 the expression must be a variable name and its address is used.
plots a black circle of radius=3 and a blue infinity-like figure, spanning from x=-3 to x=3.objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.size(-4,4,-4,4) //sizes the window to fit the graph t = 0 //Declares t as a possible variable g.addexpr("3*sin(t)") //stores 3*sin(t) as a function to be plotted in g graphs g.color(3) //the next graph will be drawn in blue g.addexpr("3*sin(2*t)") //stores 3*sin(2*t) as a function to be plotted g.xexpr("3*cos(t)") //stores 3*cos(t) as the x function to be plotted in g graphs //sin(x) becomes the y function g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(t=0; t<=2*PI+0.1; t=t+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(t) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
Graph
.flush()
Graph
.fastflush()
fastflush
).
This is useful for seeing the progress of addvar plots during long
computations in which the graphlines contain many thousands of points.
Make sure you do a normal .flush
when the lines are complete since
fastflush does not notify the system of the true size of the lines.
In such cases, zooming, translation, and crosshairs do not always
work properly till after the flush()
command has been given.
(Note, this is most useful for time plots).
objectvar g g = new Graph() g.size(0,4000, -1,1) g.addexpr("cos(x/100)") g.addexpr("cos(x/150)") g.addexpr("cos(x/200)") g.addexpr("cos(x/250)") g.addexpr("cos(x/300)") g.addexpr("cos(x/450)") proc pl() { g.erase() g.begin() for (x=0; x < 4000; x=x+1) { g.plot(x) if (x%10 == 0) { g.fastflush() doNotify() } } g.flush() doNotify() } pl()
Graph
g.family(boolean)
g.family("varname")
With a string argument which is a variable name, the string is printed as a label and when keep lines is selected each line is labeled with the value of the variable.
When graphs are printed to a file in Ascii mode, the lines are labeled with these labels. If every line has a label and each line has the same size, then the file is printed in matrix form.
Graph
.vector(n, &x[0], &y[0])
.vector("namey")
.vector(n, &x[0], &y[0])
.flush()
(x-vector is not reread). Cannot save
and cannot keep lines.
Notes:
These vectors are assumed to be doubles and not vectors from
the Vector class. The Vector class has its own function .graph()
for
graphing vectors constructed in that class.
A segmentation violation will result if
n is greater than the vector size.
.vector("namey")
.vector(n, ..., &namey[0])
above with the advantage
that it is saved in a session (because the symbol name is known).
It is simpler in that the size n is obtained from the symbol but
the plot is vs. the index of the vector. Not implemented.
Graph
thisindex = g.getline(previndex, xvec, yvec)
objref xvec, yvec xvec = new Vector() yvec = new Vector() for (j=0 i=-1; (i = Graph[0].getline(i, xvec, yvec) != -1 ; j+=1 ) { // xvec and yvec contain the line with Graph internal index i. // and can be associated with the sequential index j. print j, i, yvec.label xline[j] = xvec.c yline[j] = yvec.cl // clone label as well }
Graph
thisindex = g.line_info(previndex, Vector(5))
Graph
.erase()
Graph
e.erase_all()
Graph
g.size(xstart, xstop, ystart, ystop)
g.size(1-4)
g.size(&vec[0])
Graph
.label(x, y, "label")
.label(x, y)
.label("label")
.label(x, y, "string", fixtype, scale, x_align, y_align, color)
.label(x, y, "label")
.label("label")
.label(x, y)
label("string")
will be printed at this location
The many arg form is used by sessions to completely specify an individual label.
Graph
.fixed(scale)
Graph
.vfixed(scale)
Graph
.relative(scale)
Graph
.align([x_align], [y_align])
objref g g = new Graph() g.align(0, 0) g.label(.5,.5, "left bottom at (.5,.5)") g.align(0, 1) g.label(.5,.5, "left top at (.5,.5)") g.align(1, 0) g.label(.5,.5, "right bottom at (.5,.5)") g.align(.5,2) g.label(.5,.5, "middle but twice height at (.5, .5)")
Graph
.color(index)
.color(index, "colorname")
0 white 1 black 2 red 3 blue 4 green 5 orange 6 brown 7 violet 8 yellow 9 gray
.color(index, "colorname")
Graph
.brush(index)
.brush(index, pattern, width)
.brush(index)
.brush(index, pattern, width)
*default_brush: 0.0
Graph
.view(mleft, mbottom, mwidth, mheight, wleft,
wtop, wwidth, wheight)
.view(2)
The single argument form maps a view in which the aspect ratio between x and y axes is always 1. eg like a shape window.
Graph
.save_name("objectvar")
.save_name("objectvar", 1)
Graph
.beginline()
.beginline(color_index, brush_index)
.beginline("label")
.beginline("label", color, brush)
g.line(x)
is the first point of the next line to be graphed.
This is a less general command than .begin()
which prepares a graph for
the .plot()
command.
The optional label argument labels the line.
g.line()
is the expression sin(x)
itself, whereas if you were using the .plot()
command, the arguments
would have to be specified before the for
loop using .addexpr()
commands. The addexpr/begin/plot method of plotting is preferred since it
is capable of simultaneously plotting multiple lines.
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.beginline() //Tells the interpreter that commands to create a line for //specific functions will follow. for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.line(x, sin(x)) //States that the y values on the line //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
Graph
.line(x, y)
for
loop. It is analogous to .plot()
and the commands which
go along with it. In the case of .line()
however, all arguments are given in
the line command itself. Therefore, the line command only plots one line at a time, whereas
the .plot*()
command can plot several lines using the same for loop on the same graph.
This command takes arguments for both x and y values, so it can serve the same purpose of
the .plot
command in conjunction with an .addexpr()
command and an .xexpr()
command.
objref g g = new Graph() g.beginline() for(t=0; t<=2*PI+0.1; t=t+0.1){ g.line(sin(t), cos(t)) } g.flush()
graphs a circle of radius=1, just as would the following code using g.plot()
:
objref g g = new Graph() t = 0 g.addexpr("sin(t)") g.xexpr("cos(t)") g.begin() for(t=0; t<=2*PI+0.1; t=t+0.1){ g.plot(t) } g.flush()
Note that the arguments to g.line
are doubles, and not chars as they are in g.plot()
.
Graph
.mark(x, y)
.mark(x, y, "style")
.mark(x, y, "style", size)
.mark(x, y, "style", size, color, brush)
+,x,*,o,O,t,T,s,S
where o,t,s
stand for circle, triangle,
square and capitalized means filled. Default size is 12 points.
At this time only the "+,o.O
" is implemented.
Graph
.crosshair_action("procedure_name")
.crosshair_action("procedure_name", vectorflag=0)
.crosshair_action("")
procedure_name(x, y, c)
where x and y are the coordinates of the crosshair (in model
coordinates) and c is the ascii code for the key pressed.
The procedure will be executed in the context of the object
where crosshair_action
was executed.
When the optional vectorflag argument is 1, then, just prior
to each call of the procedure_name due to a keypress,
two temporary objectref's are created and assigned to a
new Vector()
and the line coordinate data is copied to those Vectors.
With this form the call to the procedure has two args added:
procedure_name(i, c, $o3, $o4)
where i
is the index of the crosshair into the Vector.
If you wish the Vector data to persist then you can assign to
another objectvar before returning from the procedure_name
.
Note that one can copy any line to a Vector with this method whereas
the interpreter controlled Graph.dump("expr", y_objectref)
is
limited to the current graphline of an addvar
or addexpr
.
With an empty string arg, the existing action is removed.
Graph
.view_count()
flush_list
and graphList[]
when this goes to
0. If no other objectvar
points to the scene, it will be
freed.)
Graph
.unmap()
.unmap
is called
automatically when no hoc object variable references the Graph.
Graph
.printfile("filename")
Graph
g.menu_remove("item name")
Graph
g.exec_menu("item name")
objref g g = new Graph() g.exec_menu("Keep Lines")
Graph
.menu_action("label", "action")
objref g g = new Graph() g.menu_action("Print File", "g.printfile(\"temp.eps\") system(\"lp temp.eps\")")
Graph
.menu_tool("label", "procedure_name")
.menu_tool("label", "procedure_name", "select_action")
If the third arg exists, the select_action will be executed when the radioitem is pressed (if it is not already selected).
When selected, the item will be marked and the label will appear on the window title bar (but not if the Graph is enclosed in a VBox ). When this tool is selected, pressing the left mouse button, dragging the mouse, and releasing the left button, will cause procedure_name to be called with four arguments: type, x, y, keystate. x and y are the scene (model) coordinates of the mouse pointer, and type is 2 for press, 1 for dragging, and 3 for release. Keystate reflects the state of control (bit 1), shift (bit 2), and meta (bit 3) keys, ie control and shift down has a value of 3.
The rate of calls for dragging is of course dependent on the time it takes to execute the procedure name.
objref g g = new Graph() g.menu_tool("mouse events", "p") proc p() { print $1, $2, $3, $4 }
Graph
g.gif("file.gif")
g.gif("file.gif", left, bottom, width, height)
Graph
i = g.view_info()
val = g.view_info(i, case)
val = g.view_info(i, case, model_coord)
Return information about the ith view.
With no args the return value is the view number where the mouse is. If the mouse was not last in a view of g, the return value is -1. Therefore this no arg function call should only be made on a mouse down event and saved for handling the other mouse events. Note that the two arg cases are generally constant between a mouse down and up event.
case 1: // width case 2: // height case 3: // point width case 4: // point height case 5: // left case 6: // right case 7: // bottom case 8: // top case 9: // model x distance for one point case 10: // model y distance for one point The following cases (11 - 14) require a third argument relative location means (0,0) is lower left and (1,1) is upper right. case 11: // relative x location (from x model coord) case 12: // relative y location (from y model coord) case 13: // points from left (from x model coord) case 14: // points from top (from y model coord) Note: this last is from the top, not from the bottom. case 15: // height of font in points
Graph
g.glyph(glyphobject, x, y, scalex, scaley, angle, fixtype)