21.5. Adding icon and text objects to the graph
Prev | Chapter 21. Windrose | Next

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

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

### 21.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 `WindroseGraph::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\nWidrose Plot");
    $txt->SetParagraphAlign('center');
    $txt->SetPos(0.95,0.15,'right');
    $txt->SetBox('lightyellow');
    $txt->SetShadow();
    $graph->Add($txt);
    ?>

An example of adding text to a graph can for example be seen in Figure 21.23. 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);
    ?>

### 21.5.2. Adding icons to the graph

Icons are added as instances of `class IconPlot` to the graph (as usual with a call to `WindroseGraph::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 "tornado" icon in the upper left corner of the graph. For more information on formatting icons that are added to a graph see Section 14.14

### Caution

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

Example 21.5. Adding a "tornado" icon to the top left corner (`windrose_icon_ex1.php`)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    <?php // content="text/plain; charset=utf-8"
    require_once ('jpgraph/jpgraph.php');
    require_once ('jpgraph/jpgraph_windrose.php');
    require_once ('jpgraph/jpgraph_iconplot.php');

    $data = array(
        0 => array(1,1,2.5,4),
        1 => array(3,4,1,4),
        'wsw' => array(1,5,5,3),
        'N' => array(2,7,5,4,2),
        15 => array(2,7,12));

    // First create a new windrose graph with a title
    $graph = new WindroseGraph(400,400);

    // Creta an icon to be added to the graph
    $icon = new IconPlot('tornado.jpg',10,10,1.3,50);
    $icon->SetAnchor('left','top');
    $graph->Add($icon);

    // Setup title
    $graph->title->Set('Windrose icon example');
    $graph->title->SetFont(FF_VERDANA,FS_BOLD,12);
    $graph->title->SetColor('navy');

    // Create the windrose plot.
    $wp = new WindrosePlot($data);

    // Add to graph and send back to client
    $graph->Add($wp);
    $graph->Stroke();
    ?>

Figure 21.16. Adding a "tornado" icon to the top left corner `(`windrose_icon_ex1.php`)`

Prev | Up | Next
21.4. Some more advanced formatting | Home | 21.6. Using layout classes to position Windrose plots