20.5. Adding icon and text objects to the graph
Prev | Chapter 20. Odometer | Next

## 20.5. Adding icon and text objects to the graph

Odometer plots supports the ordinary way of adding icon and text objects to the graph.

### 20.5.1. Adding a text object

Text objects are added by first creating an instance of `class Text` for each text needed and then adding the text to the graph with the usual call to `MatrixGraph::Add()`.

A basic text will only require two additional lines of code

    1
    2
    3
    4

    <?php
    $txt = new Text('Simple string',20,20);
    $graph->Add($txt);
    ?>

The following code snippet is slightly more complicated and will create a boxed text in the upper right corner of the graph.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    <?php
    // Add a boxed text
    $txt = new Text();
    $txt->SetFont(FF_ARIAL,FS_NORMAL,10);
    $txt->Set("Arbitrary text\non a\nMatrix Plot");
    $txt->SetParagraphAlign('center');
    $txt->SetPos(0.95,0.15,'right');
    $txt->SetBox('lightyellow');
    $txt->SetShadow();
    $graph->Add($txt);
    ?>

The snippet above adds a text at coordinates X=20, Y=20 using the default lower left corner as the text anchor point.

### Note

To add a newline you must remember to use double-quotes to enclose the text otherwise the "\n" will only be interpreted literally.

### Note

Remember that the "text align", as adjusted with `SetAlign()`, specifies the anchor point for the text, i.e. what part of the text is aligned with the specified position.

To add many text strings it is often useful to specify them in an array and then have a loop creating the text object and add the text array of all the created objects to the graph as the following short snippet shows.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    <?php
    //--------------------------------------------------------------
    // Add texts to the graph
    //--------------------------------------------------------------
    $txts = array(
        array('Textstring one ...',$tx1,$ty1),
        array('Textstring two ...',$tx2,$ty2),
        array('Textstring three ...',$tx3,$ty3),

    $n=count($txts);
    $t=array();
    for($i=0; $i < $n; ++$i){
        $t[$i] = new Text($txts[$i][0],$txts[$i][1],$txts[$i][2]);
        $t[$i]->SetFont(FF_ARIAL,FS_NORMAL,12);
        $t[$i]->SetColor('brown');
        $t[$i]->SetAlign('center','top');
    }
    $graph->Add($t);
    ?>

### 20.5.2. Adding icons to the graph

Icons are added as instances of `class IconPlot` to the graph (as usual with a call to `OdometerGraph::Add()`). This means that to use icons the library module "`jpgraph_iconplot.php`" must first be included.

The following example shows how to add a small icon in the lower right corner of the graph. For more information on formatting icons that are added to a graph see Section 14.14

### Caution

Since Odometer graphs doesn't have a ny concept of linear scale the position of icons can only be specified as absolute pixels or as fractions of the width/height.

Figure 20.21. Adding an icon and text to a Odometer graph `(`odotutex19.php`)`

We also recall that it is possible to use country flags as icons by making use of the method

- `IconPlot::SetCountryFlag($aFlag,$aX=0,$aY=0,$aScale=1.0,$aMix=100,$aStdSize=3)`

### 20.5.3. Adding background images

In just the same way as for all other plots it is possible to add a background images, background gradients and a background flag to the graph. Since this has been discussed several time previously in the manual we only show a simple example here.

### Note

Since odometer graphs does not have the concept of plot area the background image positioning alternative `BGIMG_FILLPLOT` is the same as `BGIMG_FILLFRAME` as we have used in this example below.

Prev | Up | Next
20.4. Adding and positioning multiple odometers to a graph | Home | Chapter 21. Windrose