From WeBWorK_wiki
Revision as of 14:04, 1 February 2008 by Glarose (talk | contribs) (New page: <h2>Adding Labels, Lines and Points to Dynamic Graphs: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essent...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Adding Labels, Lines and Points to Dynamic Graphs: PG Code Snippet

This code snippet shows the essential PG code to add labels, lines and points to dynamically generated graphs. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation

We have no required additions to the tagging and description section of the file, and we obviously need to include PGgraphmacros.pl to do dynamic graphs.

  # a graph object
  $gr = init_graph(-1,4,-1,4,axes=>~[0,0],grid=>[5,5]);
  # plot a function
  "4-3*e^(-x) for x in <-1,4> using color:blue and weight:2");

  # add a label
  $gr->lb( new Label(0.25,3.25,'(0,3)',

  # a line

  # and a point
  $gr->stamps( closed_circle(0,3,'blue') );

In the initialization part of the file, we define a [dynamically generated graph|DynamicImages]. Then we can add labels, lines and points to the graph.

To add labels, we use the lb method of the graph object. The argument is a new Label object, defined by an x-coordinate, y-coordinate, label text, color, and justification. The justification can be left, center or right for the left-to-right justification, and top, center or bottom for the top-to-bottom justification.

Lines are added by moving the graph "cursor" to a point and then drawing a line to a new point.

And we add points by using the stamps method of the graph object. We've added a closed circle here; as might expect, open_circles are also supported.

  \{ image( insertGraph($gr) ) \}

And the graph is inserted in the text portion of the problem as we'd expect any static or dynamic image to be.

Problem Techniques Index