Abstract
This section contains an overview of how to use PHPlot.
To create a plot with PHPlot, your PHP script will generally do the following:
Include the phplot.php
source using
require_once
.
Create an object which is an instance of the PHPlot
class.
Use PHPlot functions (methods of the class object) to select the plot type, present the data array, and optionally change settings which control the appearance of the plot.
Output the plot, typically to the user's browser but possibly to a file instead.
It is important to remember that if you are writing a PHP script that uses PHPlot to create an image for a web page, that PHP script must output only the image data. If you want to embed the image into a web page with text and other images, you need at least two scripts. Your main script returns an HTML page which includes an IMG (Image) tag for the plot. The IMG tag has a SRC attribute which references the second script, and it is this second script which creates the PHPlot image. You will most likely need a way to communicate parameters from your main script to your image script. Two good ways to do this are using URL parameters, and with PHP session variables.
Here is a simple, annotated example of a script which produces an image. More examples can be found in Chapter 5, PHPlot Examples.
require_once 'phplot.php';
This brings in the PHPlot source into your script. For this to work,
PHP needs to be able to find the PHPlot source file. A good way to arrange
this is to install PHPlot into a directory outside your web server's
document root and on the PHP Include Path. Other ways are to include a full
path to phplot.php
when including it, or to copy
phplot.php
into the same directory as your script.
$plot = new PHPlot();
Here we create a new PHPlot object and call it plot
.
Everything else we do with the plot will be through the $plot object.
$plot->SetPlotType('lines'); $plot->SetDataType('text-data');
Here we select the plot type 'lines', for a line plot (see Section 3.4, “PHPlot Plot Types”), and indicate our data will be represented in the 'text-data' format (see Section 3.3, “PHPlot Data Types”).
$plot->SetDataValues($data);
The data array $data is where we store the values to be plotted. We haven't shown where the data came from, but in a typical application it might be from a database query. How the data array is constructed is described in Section 3.3, “PHPlot Data Types”.
$plot->SetXDataLabelPos('none'); $plot->SetLineWidths(3); $plot->SetDrawXGrid(True);
These three functions illustrate how to change the appearance of the plot.
$plot->DrawGraph();
This final function call outputs the plot. More accurately, this function creates the plot using all the data and settings which were established by previous functions, and then outputs the plot. This is a crucial point when using PHPlot: Until you call DrawGraph, PHPlot is simply recording all the settings resulting from the functions you call, and saving a copy of your data array. Nothing really happens until you complete the plot with DrawGraph.