next up previous contents
Next: Captioning your graphs Up: Graphs Previous: Displaying your graphs   Contents

Labelling your graphs

First we consider an example with labels. We give two methods for doing this. The code is self-documenting.

##Initialize the graph object
##
$graph_object = init_graph($xmin,$ymin,$xmax,$ymax,
                    'axes' => [0,0],
                    'grid' => [12,20],
                    'pixels' => [300,300]
                    );

## Define the function to graph
$f = FEQ("${a}*x^2 + ${b}*x + ${c} for x in [$xmin, $xmax] using
color:blue and weight:2");


## Add the function to the graph object
##
add_functions($graph_object, $f);


## Add the label "My label" at the point (1,5).  The justification
## means that (1,5) will be at the top left of the label.
## Other options are {top, middle, bottom} and {left, right, center}
##
$label_object_reference = new Label (1,5,'My label','blue',('top','left'));

## Add the label to the graph object
$graph_object -> lb($label_object_reference);

## Ok, we are ready to begin...
##
TEXT(beginproblem());

BEGIN_TEXT

Below is the graph of a function \( f \): 
($BBOLD Click on image for a larger view $EBOLD) 
$PAR

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

$PAR

END_TEXT


## Here is a fancier approach which is smart about
## relating the placement of the label to the graph
## of the function

$new_graph_object = init_graph($xmin,$ymin,$xmax,$ymax,
                    'axes' => [0,0],
                    'grid' => [12,20],
                    'pixels' => [300,300]
                    );

## Define the function to graph
$g = FEQ("${a}*x^2 + ${b}*x + ${c} for x in [$xmin, $xmax] using
color:blue and weight:2");


## Add the function to the graph object
##
($g_ref) = add_functions($new_graph_object, $g);


## Add the label "My label" at the point ($xcoord, 3+g($xcoord)).  
## The justification means the point will be at the top left of the label.
## Other options are {top, middle, bottom} and {left, right, center}
##
## I do not yet fully understand this construction, but it appears to
## be the preferred method
##
$xcoord_label = 1;
$new_label_object_reference = 
         new Label ($xcoord_label,3+&{$g_ref->rule}($xcoord_label),
         'My label','blue',('top','left'));

## Add the label to the graph object
$new_graph_object -> lb($new_label_object_reference);


BEGIN_TEXT

Below is the graph of the same function \( f \): 
($BBOLD Click on image for a larger view $EBOLD) 
$PAR

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

$PAR

END_TEXT



Thomas R. Shemanske 2002-03-05