DynamicImages
Dynamically Generated Graphs: PG Code Snippet
This code snippet shows the essential PG code to include dynamically generated graphs in a problem. 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.
Note that in the following we consider first a very simple example that shows the basics of including a dynamically generated graph, and then, below that, a couple of more complicated examples that demonstrate some of the additional features of the graph macros.
PG problem file | Explanation |
---|---|
loadMacros("PGgraphmacros.pl"); |
Initialization:
We need to load |
$gr = init_graph(-1,-1,4,4,axes=>[0,0]); add_functions($gr, "x^2/4 for x in <-1,4> using color:blue and weight:2"); $gr->gifName($gr->gifName()."-$newProblemSeed"); |
Setup:
We create a graph object by calling
We add functions to all three graphs with <i>function</i> for x in <i>range</i> using color:<i>colorname</i> and weight:<i>numericalweight</i>
The function should be given as a function of x, and should be written with
The range specified is given as a mathematical interval: that is, specifying a square bracket ( Finally, colorname and numericalweight are what we would expect: the name of the color to graph the function, and the weight of the curve used. The default weight is two, and the default color is black.
We can specify that images in the web browser's cache not be used by inserting |
BEGIN_TEXT $BCENTER \{ image( insertGraph($gr), width=>400,height=>400,tex_size=>800 ) \} $BR (Click on graph to enlarge) $BCENTER END_TEXT |
Main Text:
Images are included in the text section of the problem with the |
In addition to this basic graph insertion, we can do more interesting things as well: for example, we can specify the size of the image in the hardcopy that a student can print out, the size of the image when viewed on screen, and extra HTML tags to use when displaying the image on the screen. In addition, there are cases where we want to leave off the default graph labeling, which is also easily done. We can decorate images with labels, lines and points as well.
PG problem file | Explanation |
---|---|
loadMacros("PGgraphmacros.pl"); |
We include |
$gr1 = init_graph(-1,-1,4,4,axes=>[0,0],grid=>[5,5]); $gr2 = init_graph(-1,-1,4,4,axes=>[0,0],size=>[150,150]); $gr2->lb('reset'); add_functions($gr1, "sin(x) for x in <-1,4> using color:black and weight:2"); add_functions($gr2, "2*x for x in in <-1,4> using color:green and weight:2", "-x for x in <-1,4> using color:red and weight:2"); |
To illustrate a couple of different features of dynamically generated graphs, we create two new graph objects in the problem set-up section of the file:
For the graph object
Then, for the graph object
Next, we remove all of the default labels and tick marks from the graph object
And we add functions to all three graphs with |
BEGIN_TEXT \{ image( insertGraph($gr1), tex_size=>250 ) \} $BR \{ image( insertGraph($gr2), tex_size=>250, width=>150, height=>150, extra_html_tags=>'alt="graph of a green ' . 'line through the origin into the first and ' . 'third quadrants and a red line through the ' . 'origin extending into the second and fourth ' . 'quadrants."' ) \} END_TEXT |
Then, in the text section of the file, we have made a couple of additional changes. The first is the inclusion of the
When inserting graph object
Finally, for the It's worth making one more note about this option, which has been broken over a couple of lines to avoid having one very long line on this help page. The short version of the option would be: extra_html_tags=>'alt="alternate text"'
To break the text over multiple lines in this example, we have used the Perl concatenation operator extra_html_tags=>'alt="graph of sin(' . $a . 'x)"'
It is possible to prohibit the use of cached images by setting
|