14.14. Adding icons (and small images) to the graph

In addition to the standard background image you can also add an arbitrary number of icons onto the background of the graph. These icons are created with a call to the special Plot class IconPlot defined in the module "jpgraph_iconplot.php". The image which is the base of the icons can be

  1. an arbitrary image from a file

  2. one of the built-in country flags

The basic structure of adding an icon somewhere on the graph is to first create an instance of the IconPlot class and then position the icon at the wanted x,y-position on the graph and finally add the object to the graph using the standard Graph::Add() method. The constructor for the IconPlot class have the following signature

Some useful methods in this class are

The following short example shows how an icon is created and added to a graph

1
2
3
$icon = new IconPlot('myimage.png',0.2,0.3,1,30);
$icon->SetAnchor('center','center');
$graph->Add($icon);

It is possible to control how much of the icon should be blended into the background by specifying a percentage (1-100). The example below in Figure 14.84 shows how to mix in the picture of "Tux the penguin" into the background of a filled line graph.

Figure 14.84. Mixing an icon image into the background of the graph. The area plot in the graph uses alpha blending to achieve see-through affect (lineiconex1.php)

Mixing an icon image into the background of the graph. The area plot in the graph uses alpha blending to achieve see-through affect (lineiconex1.php)


To specify any of the roughly 200 country flags as an icon the first step is to create an empty Icon and then call the IconPlot::SetCountryFlag() method with the appropriate parameters. This is illustrated below by adding the Icelandic flag into the background as an icon.

Figure 14.85. Adding a country flag icon in the background (lineiconex2.php)

Adding a country flag icon in the background (lineiconex2.php)


Caution

Some older versions of PHP (< 4.3.3 using the built-in GD) have problems rendering blended images. If you have this problem then you need to upgrade to a more recent version of PHP.