14.2. Specifying and formatting the overall displayed graph

The overall look and feel of the graph can be adjusted in a number of ways.

14.2.1. Adjusting size, margins and frame

The overall size of the graph is specified in the initial creation of the "$graph" instance. This is mandatory.

$graph = new Graph($width,$height);

The margin determines the space between the edges of the plotarea and edge of the graph. Please note that the edge does not include the frame around the entire graph area, i.e. whatever weight the frame edge has it doesn't impact on the margin area. The margin is specified in the order left,right,top and bottom margin and the size is specified in pixels.


The default margin will be as narrow as possible but still have enough space to show the titles and labels. The final adjustments is wheter or not the graph will have a frame or not. There are two distinctions to be made here. No frame meaning that the margin area will not be colored (i.e. painted) and no frame as in no edge around the graph area. The first case is achieved by calling the SetFrame() method on the Graph class


and the second case is achieved by setting the frame edge to have weight zero. Since the weight is given as the third argument we wtill have to put in valid "dummy" argument for the on/off flag and the frame color parameter.


Refer to Figure 12.1 for definitoin of plot aea and margin.

14.2.2. Adding drop shadow to the graph

A graph can have a drop shadow specified with the method SetShadow() which has the signature


by default there will be no dropshadow. Note that the drop shadow is a straight simple shadow and has no Gaussian blur.

14.2.3. Formatting and specifying the titles of the graph

There are three possible standard titles in the graph

  1. $graph->title

  2. $graph->subtitle

  3. $graph->subsubtitle

Each title is an instance of the Text class which means it supports the standard text adjusting methods SetFont(), Set(), SetColor() etc. The title s will be centered on the top of the graph area with the title highest up, subtitle below and finally the subsubtitle below that as shown in Figure 14.6

Figure 14.6. The various titles in a graph (titleex1.php)

The various titles in a graph (titleex1.php)

In addition to the above standard titles there is also another type of graph titles called the "Tabular title". This is a title that sits directly on the plot area looking like a "tab" in a sorting cabinet. Figure 14.7 and Figure 14.8 shows examples of this.

Figure 14.7. Tabtitle and gradient background (gradbkgex1.php)

Tabtitle and gradient background (gradbkgex1.php)

Figure 14.8. Tabtitle and image marker in a line plot (imgmarkerex1.php)

Tabtitle and image marker in a line plot (imgmarkerex1.php)

These type of titles are created by using the


property in the Graph class. For example the tabtitle in Figure 7.2 was generated by adding the lines

$graph->tabtitle->Set('Year 2003');

Adding special background to the title

There is one more way to make the graph title stand out that. This feature is probably one of the least known ways to adjust the graph title and is used to put a lot of emphasis on the title by having a separate color (and style) for the background of the graph title.


This feature was added by the request of one of our users who had a company design policy that demanded these type sof formatting options for graphs.

The two methods used to control this style are

  • Graph::SetTitleBackground($aBackColor='gray', $aStyle=TITLEBKG_STYLE1, $aFrameStyle=TITLEBKG_FRAME_NONE, $aFrameColor='black', $aFrameWeight=1, $aBevelHeight=3, $aEnable=true)

    This method is the basic method to specify a solid color background with the defined style and type of framing. The first style paramatere specifies the extension of the background and how it interacts with the frame around the image.

    • TITLEBKG_STYLE1, The title frame will be drawn inside the overall graph frame

    • TITLEBKG_STYLE2, The title frame will extend all the way to the edge of the graph and overwrite any graph frame

    • TITLEBKG_STYLE3, This is the same as TITLEBKG_STYLE2 apart from when it is used together with framestyle TITLEBKG_FRAME_BEVEL then the bevel frame border is on top of the title frame border

  • Graph::SetTitleBackgroundFillStyle($aStyle,$aColor1='black',$aColor2='white')

    The method determines adjusts the way the background of the title is filled by allowing a simple pattern filling to be set. This pattern filling is either horizontal or vertical stripes (lines) with user selectable colors. The possible patterns are

    • TITLEBKG_FILLSTYLE_HSTRIPED, The background will have horizontal stripes with $aColor1 on a background with $aColor2

    • TITLEBKG_FILLSTYLE_HSTRIPED, The background will have vertical stripes with $aColor1 on a background with $aColor2

    • TITLEBKG_FILLSTYLE_SOLID, The background will only have one color specified as $aColor1

To better show the effect of the possible combinations the table below shows a matrix of the possible combinations and the resulting styles. To keep the table small only the relevant top are of the graph with the title is shown.In addition to keep the style simple we have only used shades of gray.

Frame/Fill style





























There is a change in the behaviour of the latest GD library that causes style 2 and style 3 in JpGraph 2.x to be more or less visually identical as can be seen above. Using 1.x will more clearly show the difference. The reason is that with JpGraph 2.x we rely more heavily on the GD built in functions while in 1.x many of the functions where instead implemented in the library (since they weren't earlier available in GD 1.x). The change in JpGraph 2.x was made to increase the performance, unfortunately some minor differences exist in the way GD 2.x handles border line cases as how to handle pixel-by-pixel accuracy (or convention). Over time these changes will become less and less as parts of the library will be adjusted to the new GD 2.x behaviour.

A typical example of changed behaviour is how rectangles with thickness > 1 are positioned. The library assumes that the top left (0,0) corner is the very top left part of the rectangle. This is not so with GD 2.x. Instead (0,0) is the center point on the line. If the line has thickness=1 they are identical but not if the line has a thickness > 1

14.2.4. Specifying the image format to use

As described in Section 3.2 the actual image compression available depends on the system setup. Assuming the needed sewtup is done then the image format can be specified by using the method Graph::SetImgFormat() and specifying the wanted format as a string. For example as


The method call above selects JPEG image compression algorithm and tells the algorithm to set the quality parameter to 80%. This additional parameter is only applicable together with JPG formatting.

14.2.5. Generic line formatting

Any lines in the library can have a number of formatting option. Attributes that can be adjusted are

  • Lineweight (thickness). The weight is specified in pixels and can be any integer values

  • Linestyle , (dashed, dotted, solid etc). The style is specified as a string using the line method SetLineStyle().

    The available line styles are

    • 'solid', A solid line

    • 'dotted', A line with dots

    • 'dashed', A line with short hyphens

    • 'longdashed', A line with long hyphens

    For example to following method call set the y-grid to use the "dotted" style


    The image in Figure 14.9 shows example of different line formatting styles

    Figure 14.9. Different line formatting

    Different line formatting


    Different line formatting


    Different line formatting

    Thick (3 pixels)

    Different line formatting


    Different line formatting


    Different line formatting


  • Color. The line color is (as usual) adjusted with the method SetColor().

    For example the following line sets the color of the x-grid to lightblue.



If anti-aliasing is enabled (see ??.) then all line weights will always be interpreted as having weight 1 regardless.


To avoid drawing a line set the line weight to 0

14.2.6. Adding a footer to the graph

It is possible to add a footer to the graph as well. The footer can have a left, center and right part. The footer is accessed trough the footer property of the Graph class and its three parts "left", "right" and "center". These parts are instances of the Text class and hence all normal text formatting c an be applied to each part of the footer. The following code snippet shows how the footer parts can be set.

$graph->footer->left->Set('Left footer');
                        $graph->footer->center->Set('Center footer');
                        $graph->footer->right->Set('Right footer');

An example of using a footer is shown in Figure 14.11

Figure 14.10. Using a footer in a graph

Figure 14.11. Adding a left,right and center footer (footerex1.php)

Using a footer in a graph

14.2.7. Adding timing of graphs

The footer in the bottom of the graph can also be used to show an approximative timing of the graph generation. This is done with the help of class JpgTimer. This timer class offers two methods

  1. JpgTimer::Push(), Starts a new timer and puts it at the top of the timer stack

  2. JpgTimer::Pop(), Pops the timer at the top of the stack and returns the time (in ms) since the timer was pushed

(The stack based design makes it possible to have multiple timers running at the same time without interfering each other.)

The footer class (Section 14.2.6) have a special provision to handle an instance of the timer class

  • Footer::SetTimer($aTimer,$aSuffix='')

    $aTimer, an instance of JpgTimer

    $aSuffix, an optional suffix string that will be added at the end of the timing value

At the end of the graph generation the specified timer will be sent the sign pop and the resulting timer value will be stored in the right footer of the graph (appended to any previous value in the footer). For example the following simple lines would time the graph generation.

// Create a new timer instance
$timer = new JpgTimer();
// Start the timer
// Create the graph. These two calls are always required
$graph = new Graph(300,200);
// Make the bottom margin large enough to hold the timer value
$graph->title->Set("Timing a graph");
$graph->footer->right->Set('Timer (ms): ');
// The rest of the script as normal ..


Remember to make the bottom margin large enough.

Figure 14.12 shows an example of using a timer to add the time (in ms) it took to generate the graph

Figure 14.12. Adding a timer to the graph (in the footer) (example11.php)

Adding a timer to the graph (in the footer) (example11.php)