Blame | Last modification | View Log | RSS feed
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>3.3. PHPlot Data Types</title><link rel="stylesheet" type="text/css" href="phplotdoc.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.78.1" /><link rel="home" href="index.html" title="PHPlot Reference Manual" /><link rel="up" href="concepts.html" title="Chapter 3. PHPlot Concepts" /><link rel="prev" href="conc-overview.html" title="3.2. Programming Overview" /><link rel="next" href="conc-plottypes.html" title="3.4. PHPlot Plot Types" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3.3. PHPlot Data Types</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="conc-overview.html">Prev</a> </td><th width="60%" align="center">Chapter 3. PHPlot Concepts</th><td width="20%" align="right"> <a accesskey="n" href="conc-plottypes.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="conc-datatypes"></a>3.3. PHPlot Data Types</h2></div></div></div><div class="abstract"><p class="title"><strong></strong></p><p>This section describes how data need to be organized for use with PHPlot.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-datatypes-types"></a>3.3.1. Available Data Types</h3></div></div></div><p>The data values to be plotted are presented to PHPlot with<a class="xref" href="SetDataValues.html" title="SetDataValues"><span class="refentrytitle">SetDataValues</span></a>.In all cases, the data values are stored in a PHP array.This data array contains elements, themselves also arrays, which are calledrecords.Each record contains labels and/or data values.The 'data type' of the data array determines how PHPlot will interpretthe records in the data array.To set the data type, use <a class="xref" href="SetDataType.html" title="SetDataType"><span class="refentrytitle">SetDataType</span></a>.</p><p>Not all plot types work with all data types. In some cases, the data typedoesn't make sense for that plot type, or perhaps it just isn'timplemented. See <a class="xref" href="conc-plottypes.html" title="3.4. PHPlot Plot Types">Section 3.4, “PHPlot Plot Types”</a> for a list of availableplot types and which data types are available for each.<a class="xref" href="conc-plottypes.html#conc-plottype-datatype" title="3.4.18. Plot Types and Data Types">Section 3.4.18, “Plot Types and Data Types”</a> contains a summary table ofsupported plot type / data type combinations.</p><p>The following data types are available in PHPlot:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><a id="text-data"></a>text-data</span></dt><dd><p>This is the simplest data type, used for vertical plots where the X values areimplied, rather than specified, and there are one or more Y values for each X.When using this data type, each record (row) in the data array contains alabel (which can be empty), followed by one or more Y values:<code class="literal">array('label', y1, y2, ...)</code>.PHPlot assigns X=0.5 to the first data record, X=1.5 to the second data record,and so on.</p></dd><dt><span class="term"><a id="data-data"></a>data-data</span></dt><dd><p>This data type is similar to <code class="literal">text-data</code> except that thevalues of the independent variable X are supplied in the data array ratherthan being implied. This is used for vertical plots.When using this data type, each record (row) in the data array contains alabel (which can be empty), an X value, then one or more Y values:<code class="literal">array('label', x, y1, y2, ...)</code>.</p></dd><dt><span class="term"><a id="data-data-error"></a>data-data-error</span></dt><dd><p>This data type is used to make error plots - a plot showing values anderror ranges at each point. This is for vertical plots.When using this data type, each record (row) in the data array contains alabel (which can be empty), an X value, then sets of 3 values for each Ypoint: the Y value, error in the positive direction, and error in thenegative direction:<code class="literal">array('label', x, y1, e1+, e1-, y2, e2+, e2-, ...)</code>.</p><p>Note that both errors (e+ and e-) are given as positive numbers. Theyrepresent the absolute value of the error in the positive and negativedirections respectively. These must be greater than or equal to zero.</p></dd><dt><span class="term"><a id="text-data-single"></a>text-data-single</span></dt><dd><p>This data type is only used for pie charts, where the size of each piesegment is represented by a single row and value. (There are other data typesthat work with pie charts too.)When using this data type, each record (row) in the data array contains alabel (which can be empty), and a single value:<code class="literal">array('label', value)</code>.</p></dd><dt><span class="term"><a id="text-data-yx"></a>text-data-yx</span></dt><dd><p>This data type is used for horizontal plots where the Y values are implied,rather than specified, and there are one or more X values for each Y.(For horizontal plots, Y is the independent variable.)When using this data type, each record (row) in the data array contains alabel (which can be empty), followed by one or more X values:<code class="literal">array('label', x1, x2, ...)</code>.PHPlot assigns Y=0.5 to the first data record, Y=1.5 to the second data record,and so on.This is the horizontal plot variation of <code class="literal">text-data</code>.</p></dd><dt><span class="term"><a id="data-data-yx"></a>data-data-yx</span></dt><dd><p>This data type is used for horizontal plots where the Y values are suppliedin the data array rather than being implied.(For horizontal plots, Y is the independent variable.)When using this data type, each record (row) in the data array contains alabel (which can be empty), a Y value, then one or more X values:<code class="literal">array('label', y, x1, x2, ...)</code>.This is the horizontal plot variation of <code class="literal">data-data</code>.</p></dd><dt><span class="term"><a id="data-data-yx-error"></a>data-data-yx-error</span></dt><dd><p>This data type is used to make horizontal error plots - a plot showing valuesand error ranges at each point.When using this data type, each record (row) in the data array contains alabel (which can be empty), a Y value, then sets of 3 values for each Xpoint: the X value, error in the positive direction, and error in thenegative direction:<code class="literal">array('label', y, x1, e1+, e1-, x2, e2+, e2-, ...)</code>.</p><p>Note that both errors (e+ and e-) are given as positive numbers. Theyrepresent the absolute value of the error in the positive and negativedirections respectively. These must be greater than or equal to zero.</p><p>Note that you can also use the alias <code class="literal">data-data-error-yx</code>for horizontal error plots.</p></dd><dt><span class="term"><a id="data-data-xyz"></a>data-data-xyz</span></dt><dd><p>This data type is used for plots which have a 3D component.When using this data type, each record (row) in the data array contains alabel (which can be empty), an X value, then one or more pairs of Y and Zvalues:<code class="literal">array('label', x, y1, z1, y2, z2, ...)</code>.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 thearray containing an X value and the corresponding Y and Z values.</p></dd></dl></div><p></p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-datatypes-array"></a>3.3.2. Building Data Arrays</h3></div></div></div><p>In most of the examples in this manual, the data array is built fromconstant values in PHP code. For example:</p><pre class="programlisting">$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),);</pre><p>This data array contains 6 records, each with an empty label, an X value(assuming the data type is 'data-data'), and then 3 Y values representing 3data sets to plot.</p><p>In a real application, of course, the data values will most likely comefrom a calculation, perhaps using values from a database.This section provides a few sample code fragments which constructdata arrays. We use the PHP ability to append a new value to the end of anarray using <code class="literal">$array[] = ...</code>.</p><p>This code fragment creates a data array of type 'text-data' with threedata sets for Y=X+1, Y=X*X/2, and Y=X*X*X/3.</p><pre class="programlisting">$data = array();for ($x = 0; $x <= 5; $x++) $data[] = array('', $x+1, $x*$x/2, $x*$x*$x/3);</pre><p></p><p>This code fragment creates a data array of type 'data-data' with about100 points from the equation X * Y = 10.</p><pre class="programlisting">$data = array();for ($x = 1.0; $x <= 10.0; $x += 0.1) $data[] = array('', $x, 10.0/$x);</pre><p></p><p>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 columnscorrespond to the format of a PHPlot data array record.The first query result column should be the data label, the second (fordata type 'data-data' only) should be the X value, and subsequent columnresults should be one or more Y values (depending on the number of datasets you are plotting).(Pie charts work differently - see <a class="xref" href="conc-plottypes.html#plottype-pie" title="3.4.10. Plot Type: pie (Pie Plot)">Section 3.4.10, “Plot Type: pie (Pie Plot)”</a>.)You aren't limited to simple table lookups - you can use the full power of the SQL languageto combine tables and perform calculations on the data.Be sure to use <code class="literal">ORDER BY</code> in your SQL query to order the results,or you will not get predictable plots.</p><p>Database access methods differ. This code is for PostgreSQL; for MySQL there aresimilar functions like <code class="function">mysql_fetch_row()</code>.</p><pre class="programlisting">$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);</pre><p>This works because <code class="function">pg_fetch_row</code> assigns the resultcolumns from the query to sequentially numbered elements in the array.</p><p>Using data arrays from database query results also works if the resultcolumns are in an array which is indexed by the field name, because PHPlotconverts the data array to use numeric indexes.So with PostgreSQL you can use <code class="function">pg_fetch_assoc()</code>.You can also use <code class="function">pg_fetch_array()</code>,but only if you specify the type as <code class="literal">PGSQL_ASSOC</code>or <code class="literal">PGSQL_NUM</code>.The default type <code class="literal">PGSQL_BOTH</code> will not work,because the result array will contain the data values duplicatedunder both number and field-name indexes,and PHPlot will see both copies of the data.</p><p>Going even further, with a properly designed query you can use<code class="function">pg_fetch_all()</code> to fetch the entire query result andassign it to a data array with one statement.</p><pre class="programlisting">$r = pg_query($db, 'SELECT ...');if (!$r) exit();$data = pg_fetch_all($r);...$plot->SetDataValues($data);</pre><p>This uses field-name indexes in the array representing each row, but as noted above PHPlotwill convert the data array to use numeric indexes.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-datatypes-order"></a>3.3.3. Duplicate and Out-of-order Points</h3></div></div></div><p>With data types 'data-data' and 'data-data-error', the independent variableX is explicitly given for each data point.With data types 'data-data-yx' and 'data-data-yx-error', the independentvariable Y is explicitly given for each data point.With data type 'data-data-xyz', the independent variables X and Yare explicitly given for each data point.With each of these data types, it is possible to create a data arraywith duplicate values for the independent variable(s), or points which areout of order. For example, with data type 'data-data',this array has 2 points at X=4, and the 4th row (X=2) is out of order:</p><pre class="programlisting">// Data type data-data with out-of-order points$data = array( // X Yarray('', 1, 10),array('', 3, 30),array('', 4, 40),array('', 2, 20),array('', 4, 50),);</pre><p>PHPlot will plot the data points as specified in the array, in row order.Depending on the plot type, this may or may not make sense.</p><p>With a <a class="link" href="conc-plottypes.html#plottype-points" title="3.4.11. Plot Type: points (Styled Dot Plot)">points</a> plot(which puts a marker at each data point),the data array can legitimately contain duplicate or out-of-orderindependent variable values (usually X) as shown above.A <a class="link" href="conc-plottypes.html#plottype-bubbles" title="3.4.4. Plot Type: bubbles (Bubble Plot)">bubbles</a> plot using the 'data-data-xyz'data type can also contain duplicate or out-of-order points by specifying Xand Y values in any order.(However, two bubbles with the same X and Y will overlap, and one might becovered and invisible.)</p><p>With plot types<a class="link" href="conc-plottypes.html#plottype-ohlc" title="3.4.9. Plot Type: ohlc (Basic OHLC Plot)">ohlc</a>,<a class="link" href="conc-plottypes.html#plottype-candlesticks" title="3.4.5. Plot Type: candlesticks (OHLC Candlesticks Plot)">candlesticks</a>,<a class="link" href="conc-plottypes.html#plottype-candlesticks2" title="3.4.6. Plot Type: candlesticks2 (OHLC Filled Candlesticks Plot)">candlesticks2</a>, and<a class="link" href="conc-plottypes.html#plottype-thinbarline" title="3.4.17. Plot Type: thinbarline (Thin Bar Line Plot)">thinbarline</a>,out-of-order points are OK but duplicate independent variable valuesusually will not produce useful results.</p><p>On the other hand, with a <a class="link" href="conc-plottypes.html#plottype-lines" title="3.4.8. Plot Type: lines (Lines Plot)">lines</a> plot(which draws lines between the points in the order defined by the data arrayrows), it probably makes no sense to have out-of-order or duplicateindependent variable values in the data array.The same is true for plot types<a class="link" href="conc-plottypes.html#plottype-area" title="3.4.1. Plot Type: area (Area Plot)">area</a>,<a class="link" href="conc-plottypes.html#plottype-linepoints" title="3.4.7. Plot Type: linepoints (Lines and Points Plot)">linepoints</a>,<a class="link" href="conc-plottypes.html#plottype-stackedarea" title="3.4.14. Plot Type: stackedarea (Stacked Area Plot)">stackedarea</a>,<a class="link" href="conc-plottypes.html#plottype-squared" title="3.4.12. Plot Type: squared (Squared Plot)">squared</a>,<a class="link" href="conc-plottypes.html#plottype-squaredarea" title="3.4.13. Plot Type: squaredarea (Squared Area Plot)">squaredarea</a>, and<a class="link" href="conc-plottypes.html#plottype-stackedsquaredarea" title="3.4.16. Plot Type: stackedsquaredarea (Stacked Squared Area Plot)">stackedsquaredarea</a>.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-datatypes-missing"></a>3.3.4. Missing Data in Data Arrays</h3></div></div></div><p>Most plot types support the concept of missing points.A missing point is represented in your data array with an empty stringinstead of a Y value. (Actually, any non-numeric value works.) For example:</p><pre class="programlisting">$data = array( array('1996', 45.5),array('1997', 53.8),array('1998', ''), # No data available for 1998array('1999', 34.1));</pre><p>(For horizontal plots, the missing value is X not Y.)</p><p>With the <a class="link" href="conc-plottypes.html#plottype-lines" title="3.4.8. Plot Type: lines (Lines Plot)">lines</a>,<a class="link" href="conc-plottypes.html#plottype-linepoints" title="3.4.7. Plot Type: linepoints (Lines and Points Plot)">linepoints</a>, and<a class="link" href="conc-plottypes.html#plottype-squared" title="3.4.12. Plot Type: squared (Squared Plot)">squared</a> 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<a class="xref" href="SetDrawBrokenLines.html" title="SetDrawBrokenLines"><span class="refentrytitle">SetDrawBrokenLines</span></a> to leave a gap at the missing pointinstead.</p><p>The <a class="link" href="conc-plottypes.html#plottype-candlesticks" title="3.4.5. Plot Type: candlesticks (OHLC Candlesticks Plot)">candlesticks</a>,<a class="link" href="conc-plottypes.html#plottype-candlesticks2" title="3.4.6. Plot Type: candlesticks2 (OHLC Filled Candlesticks Plot)">candlesticks2</a>,and <a class="link" href="conc-plottypes.html#plottype-ohlc" title="3.4.9. Plot Type: ohlc (Basic OHLC Plot)">ohlc</a> plot typessupport missing points. Specify all four Y values at the missing pointas empty strings. (This does not work with PHPlot-5.4.0 and earlier.)</p><p>The <a class="link" href="conc-plottypes.html#plottype-area" title="3.4.1. Plot Type: area (Area Plot)">area</a>,<a class="link" href="conc-plottypes.html#plottype-stackedarea" title="3.4.14. Plot Type: stackedarea (Stacked Area Plot)">stackedarea</a>,<a class="link" href="conc-plottypes.html#plottype-stackedbars" title="3.4.15. Plot Type: stackedbars (Stacked Bar Plot)">stackedbars</a>,<a class="link" href="conc-plottypes.html#plottype-squaredarea" title="3.4.13. Plot Type: squaredarea (Squared Area Plot)">squaredarea</a>, and<a class="link" href="conc-plottypes.html#plottype-stackedsquaredarea" title="3.4.16. Plot Type: stackedsquaredarea (Stacked Squared Area Plot)">stackedsquaredarea</a>plot types do not support missing points.Non-numeric values are taken as zero for these plot types.</p><p>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 thatposition.</p><p>With error plots, missing points are represented by an empty string for thedependent variable value.This is the Y value for vertical error plots(data type <a class="link" href="conc-datatypes.html#data-data-error">data-data-error</a>),and the X value for horizontal error plots(data type <a class="link" href="conc-datatypes.html#data-data-yx-error">data-data-yx-error</a>).There still must be two error value entries in the array for each missingpoint, although the actual values of these will be ignored.</p><p>With data type <a class="link" href="conc-datatypes.html#data-data-xyz">data-data-xyz</a>, missingpoints are represented by an empty string for the Y value. There still mustbe a Z value entry in the array for each missing Y, although the Z value isignored.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-datatypes-indexes"></a>3.3.5. Data Array Indexes</h3></div></div></div><p>There are some rules you need to follow when building data arrays, in orderfor PHPlot to correctly process your data. The following rules apply to thearray indexes, or keys, in your data array.</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Your data array must be indexed using sequential integers starting with zero.This is automatically true if you build an array with the empty-bracketssyntax (<code class="literal">$mydata[] = ...</code>), or if you use the<code class="literal">array(...)</code> construct without specifying keys.Note that this refers only to the data array itself, not the elements ofthe data array - the records.</p></li><li class="listitem"><p>The data records, which are elements of the data array, are also arrays.These record arrays are processed by PHPlot using the<code class="literal">array_values()</code> function. This means the array keysare ignored, and the elements of the record are processed in the same orderas they were assigned. As with the data array itself, you can use theempty-brackets syntax, or the array() language construct, to build recordsin the data array. You can also use words (such as database query resultfields) as indexes, as long as the assignments are made in the correctorder.</p></li></ul></div><p></p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-datatypes-validation"></a>3.3.6. Data Array Validation</h3></div></div></div><p>PHPlot checks the validity of the data array in 3 stages.<a class="xref" href="SetDataValues.html" title="SetDataValues"><span class="refentrytitle">SetDataValues</span></a> only checks that it really was given anarray, that the array contains zero-based sequential integer keys, andthat the values are also arrays.</p><p>More extensive checking takes place when the graph is drawn with<a class="xref" href="DrawGraph.html" title="DrawGraph"><span class="refentrytitle">DrawGraph</span></a>. At that time, PHPlot checks that the data arrayis not empty, and that the rows contain a correct number of entries dependingon 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 samenumber of values for each row. (These requirements are documented in<a class="xref" href="conc-plottypes.html" title="3.4. PHPlot Plot Types">Section 3.4, “PHPlot Plot Types”</a>.) If any of these checks fails,PHPlot produces an error image instead of a plot.</p><p>An empty plot will be produced if the data array is valid but contains nonumeric Y values (X values for horizontal plots). The result has titles, Xand Y axis with labels and tick marks (except for pie plots), and otherapplicable plot features, but no actual plotted data. There is no error orwarning from PHPlot when an empty plot is produced.</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="conc-overview.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="concepts.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="conc-plottypes.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.2. Programming Overview </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3.4. PHPlot Plot Types</td></tr></table></div></body></html>