This section describes how data need to be organized for use with PHPlot.
The data values to be plotted are presented to PHPlot with SetDataValues. In all cases, the data values are stored in a PHP array. This data array contains elements, themselves also arrays, which are called records. Each record contains labels and/or data values. The 'data type' of the data array determines how PHPlot will interpret the records in the data array. To set the data type, use SetDataType.
The following data types are available in PHPlot:
Each record contains a label, followed by one or more Y values:
array('label', y1, y2, ...)
.
The corresponding X value for all Y values in a record is implicit.
PHPlot assigns x=0.5 to the first data record, x=1.5 to the second, etc.
This data type works with all plot types.
Each record contains a label, an X value, then one or more Y values:
array('label', x, y1, y2, ...)
.
This is the same as 'text-data' except the X values are explicitly given.
This data type works with all plot types except bars and stackedbars.
Note that with data type 'data-data', it is possible to create a data array with duplicate X values, or X values out of order. Depending on the plot type, this may or may not make sense. For example, with a point plot (which puts a marker at each data point), the data array can legitimately contain duplicate and out-of-order X values. With a line plot (which connects adjacent points in the data array with a line), it probably makes no sense to have out-of-order or duplicate X values in the data array.
Each record contains a label, an X value, then sets of 3 values for each Y
point: the Y value, error in the positive direction, and error in the
negative direction:
array('label', x, y1, e1+, e1-, y2, e2+, e2-, ...)
.
This data type works with plot types lines, points, and linepoints only.
Note that both errors (e+ and e-) are given as positive numbers. They represent the absolute value of the error in the positive and negative directions respectively.
Each record contains a label and a single value:
array('label', factor)
.
This data type is only for the pie chart plot type.
Each record contains a label, followed by one or more X values:
array('label', x1, x2, ...)
.
The corresponding Y value for all X values in a record is implicit.
PHPlot assigns y=0.5 to the first data record, y=1.5 to the second, etc.
This data type is for horizontal plots, and works with bar, stackedbar,
and thinbarline plot types.
Each record contains a label, a Y value, then one or more X values:
array('label', y, x1, x2, ...)
.
This is the same as 'text-data-yx' except the Y values are explicitly given.
This data type is for horizontal plots, and only works with the thinbarline
plot type.
Each record contains a label, an X value, and then one or more pairs of Y
and Z values:
array('label', x, y1, z1, y2, z2, ...)
.
A single data set in an array using this data type contains (x, y, z)
triplets. Multiple data sets can also be represented, with each row in the
array containing an X value and the corresponding Y and Z values.
An array with this data type can have duplicate X values, and X values need
not be in order.
This data type only works with the 'bubbles' plot type.
In most of the examples in this manual, the data array is built from constant values in PHP code. For example:
$data = array( array('', 0, 0, 0, 0), array('', 1, 1, 1, -10), array('', 2, 8, 4, -20), array('', 3, 27, 9, -30), array('', 4, 64, 16, -40), array('', 5, 125, 25, -50), );
Which contains 6 records, each with an empty label, an X value (assuming the data type is 'data-data'), and then 3 Y values representing 3 data sets to plot.
In a real application, of course, the data values will most likely come
from a calculation, perhaps using values from a database.
This section provides a few sample code fragments which construct
data arrays. We use the PHP ability to append a new value to the end of an
array using $array[] = ...
.
This code fragment creates a data array of type 'text-data' with three data sets for Y=X+1, Y=X*X/2, and Y=X*X*X/3.
$data = array(); for ($x = 0; $x <= 5; $x++) $data[] = array('', $x+1, $x*$x/2, $x*$x*$x/3);
This code fragment creates a data array of type 'data-data' with about 100 points from the equation X * Y = 10.
$data = array(); for ($x = 1.0; $x <= 10.0; $x += 0.1) $data[] = array('', $x, 10.0/$x);
The next code fragments use database queries to build data arrays for PHPlot.
In many cases, you can create a query such that the returned columns
correspond to the format of a PHPlot data array record.
The first query result column should be the data label, the second (for
data type 'data-data' only) should be the X value, and subsequent column
results should be one or more Y values (depending on the number of datasets you are plotting).
(Pie charts work differently - see Section 3.4.9, “Plot Type: pie (Pie Chart)”.)
You aren't limited to simple table lookups - you can use the full power of the SQL language
to combine tables and perform calculations on the data.
Be sure to use ORDER BY
in your SQL query to order the results,
or you will not get predictable plots.
Database access methods differ. This code is for PostgreSQL; for MySQL there are
similar functions like mysql_fetch_row()
.
$r = pg_query($db, 'SELECT ...'); if (!$r) exit(); $data = array(); $n_rows = pg_num_rows($r); for ($i = 0; $i < $n_rows; $i++) $data[] = pg_fetch_row($r, $i); ... $plot->SetDataValues($data);
This works because pg_fetch_row
assigns the result
columns from the query to sequentially numbered elements in the array.
Using data arrays from database query results also works if the result
columns are in an array which is indexed by the field name, because PHPlot
converts the data array to use numeric indexes.
So with PostgreSQL you can use pg_fetch_assoc()
.
You can also use pg_fetch_array()
,
but only if you specify the type as PGSQL_ASSOC
or PGSQL_NUM
.
The default type PGSQL_BOTH
will not work,
because the result array will contain the data values duplicated
under both number and field-name indexes,
and PHPlot will see both copies of the data.
Going even further, with a properly designed query you can use
pg_fetch_all()
to fetch the entire query result and
assign it to a data array with one statement.
$r = pg_query($db, 'SELECT ...'); if (!$r) exit(); $data = pg_fetch_all($r); ... $plot->SetDataValues($data);
This uses field-name indexes in the array representing each row, but as noted above PHPlot will convert the data array to use numeric indexes.
Most plot types support the concept of missing points. A missing point is represented in your data array with an empty string instead of a Y value. (Actually, any non-numeric value works.) For example:
$data = array( array('1996', 45.5), array('1997', 53.8), array('1998', ''), # No data available for 1998 array('1999', 34.1));
(For horizontal plots, the missing value is X not Y.)
With the lines, linepoints, and squared plot types, there are two ways to handle missing points. By default, PHPlot will act as if the missing point does not exist, connecting the points before it and after it. You can use SetDrawBrokenLines to leave a gap at the missing point instead.
The candlesticks, candlesticks2, and ohlc plot types support missing points. Specify all four Y values at the missing point as empty strings. (This does not work with PHPlot-5.4.0 and earlier.)
The area, stackedarea, and stackedbars plot types do not support missing points. Non-numeric values are taken as zero for these plot types.
All other plot types support missing points and simply ignore the point. That is, no bar, point shape, thinbar line, etc. will be plotted at that position.
With data type data-data-xyz, missing points are represented by an empty string for the Y value. There still must be a Z value entry in the array for each missing Y, although the Z value is ignored.
There are some rules you need to follow when building data arrays, in order for PHPlot to correctly process your data. The following rules apply to the array indexes, or keys, in your data array.
Your data array must be indexed using sequential integers starting with zero.
This is automatically true if you build an array with the empty-brackets
syntax ($mydata[] = ...
), or if you use the
array(...)
construct without specifying keys.
Note that this refers only to the data array itself, not the elements of
the data array - the records.
The data records, which are elements of the data array, are also arrays.
These record arrays are processed by PHPlot using the
array_values()
function. This means the array keys
are ignored, and the elements of the record are processed in the same order
as they were assigned. As with the data array itself, you can use the
empty-brackets syntax, or the array() language construct, to build records
in the data array. You can also use words (such as database query result
fields) as indexes, as long as the assignments are made in the correct
order.
PHPlot checks the validity of the data array in 3 stages. SetDataValues only checks that it really was given an array, that the array contains zero-based sequential integer keys, and that the values are also arrays.
More extensive checking takes place when the graph is drawn with DrawGraph. At that time, PHPlot checks that the data array is not empty, and that the rows contain a correct number of entries depending on the data type. A third stage of checking takes place for specific plot types. For example, OHLC plots require 4 values, and area plots require the same number of values for each row. (These requirements are documented in Section 3.4, “PHPlot Plot Types”.) If any of these checks fails, PHPlot produces an error image instead of a plot.
An empty plot will be produced if the data array is valid but contains no numeric Y values (X values for horizontal plots). The result has titles, X and Y axis with labels and tick marks (except for pie plots), and other applicable plot features, but no actual plotted data. There is no error or warning from PHPlot when an empty plot is produced.