Chapter 5. PHPlot Examples

Abstract

This chapter contains examples of plots produced with PHPlot. Each example shows the image and the associated PHP code.

To ensure accuracy, the example plots are generated from the PHP scripts by PHPlot, and the PHP script is inserted directly into this manual during production. So the example code should exactly generate the image presented here, provided a compatible version of PHPlot is used.

Note

The PHP CLI (command line interface), used to generate the examples here, never outputs HTTP headers. So it isn't necessary to use SetIsInline to suppress headers when using the CLI. This is a useful method you can use to debug and test your own PHPlot scripts without having to modify them for stand-alone use. Also, by using the CLI instead of a web server and browser, you can more readily see any error messages. Run your PHPlot scripts with the PHP CLI like this (using the ImageMagick display program to view the results):

$  php myscript.php > output.png
$  display output.png
    

ImageMagick is available for many operating systems. Another good image viewer, for Linux, Solaris, FreeBSD, and HP-UX systems, is qiv - the Quick Image Viewer.

5.1. Example - Line Plot

This is a simple line plot with a single data set. Data type 'data-data' is used to include the X values (the years) in the data points.

Example 5.1. Line Plot

Line Plot Example
<?php
# PHPlot Example: Simple line graph
require_once 'phplot.php';

$data = array(
  array('', 1800,   5), array('', 1810,   7), array('', 1820,  10),
  array('', 1830,  13), array('', 1840,  17), array('', 1850,  23),
  array('', 1860,  31), array('', 1870,  39), array('', 1880,  50),
  array('', 1890,  63), array('', 1900,  76), array('', 1910,  92),
  array('', 1920, 106), array('', 1930, 123), array('', 1940, 132),
  array('', 1950, 151), array('', 1960, 179), array('', 1970, 203),
  array('', 1980, 227), array('', 1990, 249), array('', 2000, 281),
);

$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain');

$plot->SetPlotType('lines');
$plot->SetDataType('data-data');
$plot->SetDataValues($data);

# Main plot title:
$plot->SetTitle('US Population, in millions');

# Make sure Y axis starts at 0:
$plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);

$plot->DrawGraph();