1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <title>PHPLOT Quick Start and Examples</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <meta name="author" content="Afan Ottenheimer" />
8 <meta name="author" content="Miguel de Benito" />
9 <meta name="created" content="20010302;20371700" />
10 <meta name="changed" content="20040121;19341300" />
11 <meta name="description" content="phplot quick start and examples" />
12 <meta name="keywords" content="phplot, graphs, images, php" />
13 <meta name="robots" content="all" />
14 <meta name="locale" content="en-us" />
15 <link type="text/css" rel="stylesheet" href="style.css" />
18 <body lang="en-us" bgcolor="#ffffff">
20 <h1>PHPlot Quick Start and Examples</h1>
22 <p>Afan Ottenheimer, January 7, 2001</p>
23 <p>Miguel de Benito, January 21, 2004</p>
24 <p><b>Contents</b></p>
26 <li><a href="#intro">Introduction</a></li>
27 <li><a href="#first">First steps</a></li>
28 <li><a href="#lines">Multiples lines per graph</a></li>
29 <li><a href="#multiple">Multiple graphs per image</a></li>
33 <h2><a name="intro"></a>Introduction</h2>
35 <p>Many web sites need to create real-time or dynamic charts and graphs.
36 from live data sets. Many users have found PHP a great way for this dynamic
37 creation of images using the GD library and there have been several
38 good articles describing this [] [] []. The advantage of using the server
39 to create an image (server side scripting) is that one does not have to
40 worry about browser compatibility or client operating system compatibility issues.
41 The PHP image generating library uses the GD library to create elementary
42 shapes (elipse, line, rectangle, ...).
43 PHPlot is a graphics library which provides a means by which you can have your
44 (PHP enabled) web server create and manipulate graphs as objects and
45 display the completed graph as an image. Data sets passed to PHPlot use a very
46 convenient way for database driven sites, in rows with y coordinate data.
49 <p>First, lets discuss how PHPlot works in general with some
50 terminology. A PHPlot <i>image </i>can consist of several <i>graphs </i>,
51 each graph consisting of several <i>elements</i>.
52 You define an object (e.g. a variable like <code>$graph</code>),
53 select the properties of the element that compose the graph and "<i>Draw</i>"
54 what you want into the object. Tipically by selecting the plot type with
55 <code>SetPlotType</code> and at the end calling <code>DrawGraph</code>. You
56 can also directly invoke <code>PrintImage</code>, which either inserts the image
57 into the data streaming to the client or writes it to disk.
59 <p>In PHPlot there are <em>World coordinates</em>, which are the XY coordinates
60 relative to the axis origin, in the units of the data set; and
61 <em>device (pixel)</em> coordinates which in GD are relative to the
62 origin at the upper left side of the image.
64 <p>You can think of the "Draw" functions as shaping the
65 image object and the "Print" function as the method of
66 finally creating the digital image. PHPlot is smart enough that if
67 you only have one graph on an image, then the "Print" is
68 done for you automatically. If you do have multiple graphs per image
69 then you'll need to use both the "Draw" and "Print"
70 functions. We'll talk about that a bit later.
72 <p>Since PHP is a server scripted language you have several options
73 for how you can "Print" the image. You can:</p>
75 <li>Write the image as a file on the server. (You specify a file
76 name and can specify caching as well)</li>
77 <li>Have the raw data stream out within an HTML file, as <IMG
78 SRC="my_PHPlot_code.php"></li>
79 <li>Precede the raw data with html image headers and call the
80 script directly (showing up as an image) e.g.
81 http://somewhere/my_PHPlot_code2.php .</li>
83 <p>This document explains how to create plots using PHPlot from a
84 PHP script. Information on PHP can be found at <a href="http://www.php.net/">www.php.net</a>.
85 Information about the GD library which PHP uses to create images can
86 be found at <a href="http://www.boutell.com/">www.boutell.com</a>.
87 More information about PHPlot can be found at <a href="www.phplot.com">www.PHPlot.com</a>.</p>
89 <h2>Creating the Object</h2>
91 <p>You create a PHPlot object by first including the code to be used
92 and then defining the variable:</p>
95 include('./phplot.php'); // here we include the PHPlot code
96 $graph =& new PHPlot(); // here we define the variable graph
98 //Rest of code goes below
102 <p>The above code assigns the PHPlot object to the variable <code>$graph</code>. Please,
103 use that '&' to create a reference, it is needed by phplot's approximation of a destructor,
104 a facility unavailable in PHP4.</p>
106 <p> <a href="#top">Back to top</a> </p>
109 <h2>Real World Examples</h2>
110 <h3><a name="first"></a>Case 1: A simple graph</h3>
112 <p>We will start with a simple line graph. </p>
116 include('./PHPlot.php');
119 $graph =& new PHPlot();
122 $example_data = array(
131 $graph->SetDataValues($example_data);
134 $graph->DrawGraph(); // remember, since in this example we have one graph, PHPlot
135 // does the <i>PrintImage </i>part for you
139 <p> And that's it! What we get is the following graph: </p>
142 <img src="imgs/qstart_fig1.png" name="fig1" alt="figure 1" align="bottom" border="0" />
146 <p>That's a great start, but now we'd like to specify the width and height
150 <h3>Case 1a: Different Size Images and Titles</h3>
152 Lets say we want it to have a width of 300 and a height of 250 pixels.
153 So instead of having the line <br /><br />
154 <code>$graph =& new PHPlot();</code> <br /><br />
155 we replace it with <br /><br />
156 <code>$graph =& new PHPlot(300,250);</code><br /><br />
157 and you have specified the size in pixels of the image to be created.
158 A couple of things to note:
161 <li>The default is <em>not</em> to use TTF fonts. </li>
162 <li>Since there was only one graph on the image we didn't have to
163 specify PrintImage, DrawGraph took care of it for us.
166 We did not specify the data type. If you do not specify the data
167 type PHPlot assumes <code>text-data</code>.
170 We did not specify the file type (gif, png, jpg, ...) .
171 PHPlot 5.0 assumes PNG image formats.
174 The data is passed in as an array of arrays. This may seem awkward
175 now, but as we add functionality this will be beneficial.
178 <p>Ok, now we're ready to add some customization to the plot. Let's change
179 the size, the title and the x/y axis labels. All we need to do is modify
180 the variable <code>$graph</code> before printing the image. We achieve this with:
184 include ('./phplot.php');
186 //create an graph object 300x250 pixels
187 $graph =& new PHPlot(300,250);
189 $graph->SetTitle("Title\n\rSubtitle");
190 $graph->SetXTitle('X data');
191 $graph->SetYTitle('Y data');
193 //...rest of the code
199 <img src="imgs/qstart_fig2.png" name="graphic1" align="bottom" border="0"><br />
203 <p>Note that in order for the "\n" and "\r " to be interpreted as
204 new line/new return characters for <code>SetTitle </code>you have to
205 enclose the string in <b>double </b>quotes.</p>
207 <h3><a name="lines"></a>Case 2: Multiple Lines per Graph </h3>
208 <p>Lets say we want to plot not just one
209 dataset but several y values for each x position. With PHPlot it is
210 easy to specify the multiple data lines by just passing in all the Y
211 values for a given X value at once. So instead of array('label', y)
212 we specify array('label', y<sub>1</sub>, y<sub>2</sub>, y<sub>3</sub>,
213 ...) This is very convenient when working with rows of data from databases.
215 Now our data will have three Y values for each position on the X axis
220 include('./phplot.php');
223 $graph =& new PHPlot(300,250);
226 $graph->SetTitle("Title\n\rSubtitle");
227 $graph->SetXTitle('X data');
228 $graph->SetYTitle('Y data');
232 $example_data = array(
234 array('b',5,'',1), // here we have a missing data point, that's ok
241 $graph->SetDataValues($example_data);
244 $graph->DrawGraph();
247 <p>Which gives us: </p>
249 <img src="imgs/qstart_fig3.png" name="graphic2" align=bottom border="0" />
253 <p>Notice that each set of Y data gets a different color.
254 Also the missing data point is skipped, this behaviour can be adjusted with
255 <code>SetDrawBrokenLines(TRUE);</code>
258 This gives you the basics of how to create a graph in PHPlot.
259 A nice start, but now we'd like to add some customization, namely different
260 fonts, margins and types of graphs.
262 <p><a href="#top">Back to top</a> </p>
265 <h3>Customization</h3>
266 <p>Valid types of plots (as of PHPlot 5.0):
268 <li><code>bars</code> (with optional shadows)</li>
269 <li><code>lines</code></li>
270 <li><code>linepoints</code> (a faster way of plotting when
271 you want both points and lines)</li>
272 <li><code>area</code></li>
273 <li><code>points</code> (lots of point types here)</li>
274 <li><code>pie</code> (2D or 3D)</li>
275 <li><code>thinbarline</code> (sometimes also called impulse) </li>
276 <li><code>error bar</code> (which can also be used for stock market data graphs)</li>
277 <li><code>squared</code> (for binary data) </li>
280 <p>You specify which type with the <code>SetPlotType</code> function.
281 We'll look at that function with bars and lines in the next example when we look at
282 multiple graphs per image.
284 <p>As we discussed before, there are several ways we can manipulate
285 the look/feel of the graph object. Almost every parameter of ticks, grids and data labels
286 can be adjusted via (among many others):
288 <li><code>SetXTickPos()</code></li>
289 <li><code>SetYTickPos()</code></li>
290 <li><code>SetXTickLength()</code></li>
291 <li><code>SetYTickLength()</code></li>
292 <li><code>SetXTickCrossing()</code></li>
293 <li><code>SetYTickCrossing()</code></li>
294 <li><code>SetXTickIncrement()</code></li>
295 <li><code>SetYTickIncrement()</code></li>
296 <li><code>SetNumXTicks()</code></li>
297 <li><code>SetNumYticks()</code></li>
298 <li><code>SetSkipTopTick()</code></li>
299 <li><code>SetSkipBottomTick()</code></li>
301 <li><code>SetDrawXGrid()</code></li>
302 <li><code>SetDrawYGrid()</code></li>
303 <li><code>SetDrawDashedGrid()</code></li>
304 <li><code>SetDrawXDataLabelLines()</code></li>
305 <li><code>SetDrawYDataLabelLines()</code> (not yet implemented)</li>
306 <li><code>SetXDataLabelPos()</code></li>
307 <li><code>SetYDataLabelPos()</code></li>
308 <li><code>SetXLabelAngle()</code></li>
309 <li><code>SetYLabelAngle()</code></li>
310 <li><code>SetXLabelType()</code></li>
311 <li><code>SetYLabelType()</code></li>
313 As we go further we will introduce some of these features of PHPlot.
314 For more specialized examples, please go <a href="index.php">back to the index</a> and
315 look in the examples section.
318 <p> <a href="#top">Back to top</a> </p>
320 <h3><a name="multiple"></a> <b>Case 3: Multiple Graphs per Image </b></h3>
322 <p>To create an image with several separate graphs
323 on it is a straightforward process. As in the previous examples we
324 first have to create an object (e.g. variable) but now we tell it to
325 <i>not</i> print the image at the same time as the draw command. Now
326 we want it to wait for the explicit <code>PrintImage</code> function call.
327 To tell PHPlot this is the way we want to work, we use the
328 <code>SetPrintImage</code> function.
329 <code>SetPrintImage(TRUE)</code> is the default, and tells to draw the image
330 when <code>DrawGraph</code> is called. To turn this
331 off we use <code>SetPrintImage(FALSE)</code>.</p>
332 <p>Now we will draw several images entirely within one object. That
333 means that if we set a value for one graph, there will be a couple of
334 other commands we will need.
336 <p>To specify in pixels the placement of each graph we use
337 <code>SetNewPlotAreaPixels</code>. The format is
338 <code>SetNewPlotAreaPixels(upper_left_x, upper_left_y, lower_right_x,
339 lower_right_y)</code> . Again we are using the GD coordinates where 0,0
340 is the upper left corner of the image.
342 <p>In more detail:</p>
345 include('./PHPlot.php'); // here we include the PHPlot code
346 $graph =& new PHPlot(400,250); // here we define the variable $graph
347 $graph->SetPrintImage(0); //Don't draw the image yet
349 //....Data and Values for first graph here .....
351 $graph->SetNewPlotAreaPixels(70,10,375,100); // where to place it
353 $graph->DrawGraph(); //Draw the first graph to the image.
355 //....Data and Values for second graph here .....
357 $graph->SetNewPlotAreaPixels(70,120,375,220); //where to place graph 2
358 $graph->DrawGraph(); //Draw the second graph to the image
360 //Print the image with both graphs
361 $graph->PrintImage();
365 <p>Lets now create an image with 2 graphs on it with some example data. </p>
370 include('./phplot.php');
373 $graph =& new PHPlot(400,250);
375 $graph->SetPrintImage(0); //Don't draw the image until specified explicitly
377 $example_data = array(
387 $graph->SetDataType("text-data"); //Must be called before SetDataValues
389 $graph->SetDataValues($example_data);
390 $graph->SetYTickIncrement(2); //a smaller graph now - so we set a new tick increment
392 $graph->SetXLabelAngle(90);
393 $graph->SetXTitle("");
394 $graph->SetYTitle("Price");
395 $graph->SetPlotType("lines");
396 $graph->SetLineWidth(1);
398 $graph->SetNewPlotAreaPixels(70,10,375,100); // where do we want the graph to go
399 $graph->DrawGraph(); // remember, since we said not to draw yet, PHPlot
400 // still needs a <i>PrintImage </i>command to write an image.
403 //Now do the second chart on the same image
404 unset($example_data); //we are re-using $example_data (to save memory), but you don't have to
405 $example_data = array(
407 array('b',50,'',10), // here we have a missing data point, that's ok
415 $graph->SetDataType("text-data"); //Must be called before SetDataValues
417 $graph->SetDataValues($example_data);
419 $graph->SetXTitle("");
420 $graph->SetYTitle("Verbal Cues");
421 $graph->SetYTickIncrement(10);
422 $graph->SetPlotType("bars");
423 $graph->SetXLabelAngle(0); //have to re-set as defined above
425 $graph->SetNewPlotAreaPixels(70,120,375,220);
426 $graph->SetPlotAreaWorld(0,0,7,80);
427 $graph->DrawGraph();
430 $graph->PrintImage();
434 <p> <br /> <br /> Which gives us: </p>
437 <img src="imgs/qstart_fig4.png" name="graphic3" align="top" border="0" /> <br />
442 You must remember that world Coordinates are the XY coordinates relative to the
443 axis origin that can be drawn. Not the device (pixel) coordinates
444 which in GD are relative to the origin at the upper left
448 <div style="background:#6699cc; color:#ffffff;">
449 <a href="#top">Back to top</a> <br />
450 <a href="index.php">Back to the index</a> <br />
452 <div class="foot">$Id$</div>