98 |
- |
1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
|
|
|
2 |
<!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>Chapter 2. Getting Started with PHPlot</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="part1.html" title="Part I. PHPlot Programming" /><link rel="prev" href="install-next.html" title="1.3. Next Step" /><link rel="next" href="starting-create.html" title="2.2. Creating the Object" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 2. Getting Started with PHPlot</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="install-next.html">Prev</a> </td><th width="60%" align="center">Part I. PHPlot Programming</th><td width="20%" align="right"> <a accesskey="n" href="starting-create.html">Next</a></td></tr></table><hr /></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a id="starting"></a>Chapter 2. Getting Started with PHPlot</h2></div></div></div><div class="abstract"><p class="title"><strong></strong></p><p>
|
|
|
3 |
This chapter will help you get started with PHPlot.
|
|
|
4 |
</p><p>
|
|
|
5 |
The material in this chapter was originally from the PHPlot Quick Start
|
|
|
6 |
and Examples document, by Afan Ottenheimer and Miguel de Benito,
|
|
|
7 |
distributed with PHPlot. It has undergone much editing and any mistakes
|
|
|
8 |
are not their fault.
|
|
|
9 |
</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="starting-intro"></a>2.1. Introduction</h2></div></div></div><p>
|
|
|
10 |
Many web sites need to create real-time or dynamic charts and graphs from
|
|
|
11 |
live data sets. Many users have found PHP a great way for this dynamic
|
|
|
12 |
creation of images using the <a class="link" href="concepts.html#def-gd">GD</a> library.
|
|
|
13 |
The advantage of using the server to create an image (that is, using a
|
|
|
14 |
server-side scripting language rather than a client-side language such as
|
|
|
15 |
Java) is that one does not have to worry about browser compatibility or
|
|
|
16 |
client operating system compatibility issues.
|
|
|
17 |
</p><p>
|
|
|
18 |
PHPlot is a specialized graphics library which provides a means for your
|
|
|
19 |
PHP-enabled web server to create and manipulate graphs as objects and
|
|
|
20 |
display the completed graph as an image.
|
|
|
21 |
PHPlot uses the GD library to create elementary shapes such as lines,
|
|
|
22 |
rectangles, and text, but hides the details of GD from the application
|
|
|
23 |
programmer.
|
|
|
24 |
</p><p>
|
|
|
25 |
Data sets are passed to PHPlot using PHP arrays, in a convenient format
|
|
|
26 |
for database-driven sites.
|
|
|
27 |
</p><p>
|
|
|
28 |
First, lets discuss how PHPlot works in general with some terminology.
|
|
|
29 |
(More complete definitions can be found in <a class="xref" href="concepts.html#conc-defs" title="3.1. Definitions">Section 3.1, “Definitions”</a>.)
|
|
|
30 |
A PHPlot <span class="emphasis"><em>image</em></span> can consist of several
|
|
|
31 |
<span class="emphasis"><em>graphs</em></span> (but usually has only one), each graph consisting
|
|
|
32 |
of several <span class="emphasis"><em>elements</em></span> (like lines, axes, and labels).
|
|
|
33 |
</p><p>
|
|
|
34 |
To use PHPlot, you create a PHP object from the PHPlot class, for example:
|
|
|
35 |
</p><pre class="programlisting">$plot = new PHPlot;
|
|
|
36 |
</pre><p>
|
|
|
37 |
Then you set the properties of the object, by using a series of function
|
|
|
38 |
calls (actually methods of the class). These define the characteristics of
|
|
|
39 |
the image, the graph or graphs, and their elements. This includes setting
|
|
|
40 |
the array containing the data to be plotted, defining titles if you want
|
|
|
41 |
them, and many optional elements and style settings. You can think of this
|
|
|
42 |
as "drawing" elements into the image, but in fact PHPlot just notes your
|
|
|
43 |
intentions and doesn't do much until you are finished.
|
|
|
44 |
</p><p>
|
|
|
45 |
When you are done describing a graph, you instruct PHPlot to "draw" the
|
|
|
46 |
graph into the image. When you are done with all graphs in an image,
|
|
|
47 |
you need to instruct PHPlot to "print" (output) the image.
|
|
|
48 |
Since most images contain only one graph, PHPlot simplifies
|
|
|
49 |
this process by default. Unless instructed otherwise, PHPlot will "print"
|
|
|
50 |
the image (output it to the browser) as soon as you tell it to "draw"
|
|
|
51 |
(render) the first graph.
|
|
|
52 |
</p><p>
|
|
|
53 |
Usually, PHPlot will "print" the image directly to the user's browser.
|
|
|
54 |
The result will be a complete HTTP response with headers, so your PHP
|
|
|
55 |
script must not produce any other output (except for optional headers).
|
|
|
56 |
The user will be see the image either as a result of accessing your
|
|
|
57 |
script directly with a URL, like
|
|
|
58 |
<code class="literal">http://www.example.com/graphs/myplot.php</code>,
|
|
|
59 |
or you can embed the image in a web page using an image tag, like this:
|
|
|
60 |
</p><pre class="screen"><IMG SRC="http://www.example.com/graphs/myplot.php">
|
|
|
61 |
</pre><p>
|
|
|
62 |
</p><p>
|
|
|
63 |
Instead of sending the image to the browser, your application can instead
|
|
|
64 |
choose to write the PHPlot image to a file on the server. This could be
|
|
|
65 |
useful if you want to implement server-side caching of image files.
|
|
|
66 |
(PHPlot itself does not currently provide caching.)
|
|
|
67 |
</p><p>
|
|
|
68 |
Before continuing, we need to mention coordinates.
|
|
|
69 |
PHPlot uses two coordinate spaces: one for the image, and one for the
|
|
|
70 |
data you are plotting.
|
|
|
71 |
<span class="emphasis"><em>World Coordinates</em></span>
|
|
|
72 |
are the X,Y coordinates of your data, relative to the axis origin,
|
|
|
73 |
in the units of the data sets.
|
|
|
74 |
Your data array uses world coordinates, as do tick mark labels on the X and
|
|
|
75 |
Y axis.
|
|
|
76 |
<span class="emphasis"><em>Device Coordinates</em></span>
|
|
|
77 |
measure pixels in the image according the the GD convention, with
|
|
|
78 |
the origin in the upper left corner of the image. These are also
|
|
|
79 |
called Pixel Coordinates.
|
|
|
80 |
PHPlot tries to place elements on your graph appropriately, but if
|
|
|
81 |
you need to override its choices you will use device coordinates to
|
|
|
82 |
position the elements.
|
|
|
83 |
</p><p>
|
|
|
84 |
The rest of this chapter explains how to write a PHP script which creates
|
|
|
85 |
a plot using PHPlot.
|
|
|
86 |
Information on PHP can be found at
|
|
|
87 |
<a class="ulink" href="http://www.php.net/" target="_top">www.php.net</a>.
|
|
|
88 |
Information about the GD library which PHP uses to create images can
|
|
|
89 |
be found at <a class="ulink" href="http://libgd.org/" target="_top">libgd.org</a>.
|
|
|
90 |
More information about PHPlot can be found at
|
|
|
91 |
<a class="ulink" href="http://phplot.sourceforge.net/" target="_top">phplot.sourceforge.net</a>.
|
|
|
92 |
</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="install-next.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="part1.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="starting-create.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.3. Next Step </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 2.2. Creating the Object</td></tr></table></div></body></html>
|