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>3.2. Programming Overview</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="concepts.html" title="Chapter 3. PHPlot Concepts" /><link rel="next" href="conc-datatypes.html" title="3.3. PHPlot Data Types" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3.2. Programming Overview</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="concepts.html">Prev</a> </td><th width="60%" align="center">Chapter 3. PHPlot Concepts</th><td width="20%" align="right"> <a accesskey="n" href="conc-datatypes.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-overview"></a>3.2. Programming Overview</h2></div></div></div><div class="abstract"><p class="title"><strong></strong></p><p>
|
|
|
3 |
This section contains an overview of how to use PHPlot.
|
|
|
4 |
</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-overview-howitworks"></a>3.2.1. How It Works</h3></div></div></div><p>
|
|
|
5 |
To create a plot with PHPlot, your PHP script will generally do the
|
|
|
6 |
following:
|
|
|
7 |
</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
|
|
|
8 |
Include the <code class="filename">phplot.php</code> source using
|
|
|
9 |
<code class="literal">require_once</code>.
|
|
|
10 |
</p></li><li class="listitem"><p>
|
|
|
11 |
Create an object which is an instance of the <code class="literal">PHPlot</code> class.
|
|
|
12 |
</p></li><li class="listitem"><p>
|
|
|
13 |
Use PHPlot functions (methods of the class object) to select the plot type,
|
|
|
14 |
present the data array, and optionally change settings which control the
|
|
|
15 |
appearance of the plot.
|
|
|
16 |
</p></li><li class="listitem"><p>
|
|
|
17 |
Output the plot, typically to the user's browser but possibly to a file
|
|
|
18 |
instead.
|
|
|
19 |
</p></li></ol></div><p>
|
|
|
20 |
The order of operations you use between creating a PHPlot object and output
|
|
|
21 |
of the plot does not matter. The PHPlot class sets internal class variables
|
|
|
22 |
as you configure the plot, but doesn't do anything with the values until
|
|
|
23 |
you are ready. For example, setting a font for the plot title, then setting
|
|
|
24 |
the title text, is the same as setting the title text first, then the font.
|
|
|
25 |
In both cases, the title will be drawn using the font you selected.
|
|
|
26 |
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
|
|
|
27 |
It is important to remember that if you are writing a PHP script that uses
|
|
|
28 |
PHPlot to create an image for a web page, that PHP script must output
|
|
|
29 |
<span class="emphasis"><em>only</em></span> the image data.
|
|
|
30 |
If you want your plot image to appear on a web page with text and other images,
|
|
|
31 |
you need at least two scripts.
|
|
|
32 |
Your main script returns an HTML page which includes an IMG (Image) tag for
|
|
|
33 |
the plot.
|
|
|
34 |
The IMG tag has a SRC attribute which references the second script, and it
|
|
|
35 |
is this second script which creates the PHPlot image.
|
|
|
36 |
An example of this can be found in <a class="xref" href="ex-webform.html" title="5.23. Example - Complete Web Form with Plot">Section 5.23, “Example - Complete Web Form with Plot”</a>.
|
|
|
37 |
You will most likely need a way to communicate parameters from your main
|
|
|
38 |
script to your image script. Two good ways to do this are using URL parameters,
|
|
|
39 |
and with PHP session variables.
|
|
|
40 |
</p><p>
|
|
|
41 |
An alternative to using two scripts - one generating HTML, and one using PHPlot
|
|
|
42 |
to create the plot image - is available starting with PHPlot-5.5.0. You can
|
|
|
43 |
write a single script which generates HTML and also embeds the PHPlot-generated
|
|
|
44 |
plot image. See <a class="xref" href="ex-encodeimage.html" title="5.39. Example - Embedding Image with EncodeImage">Section 5.39, “Example - Embedding Image with EncodeImage”</a>.
|
|
|
45 |
</p></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="conc-overview-annoex"></a>3.2.2. Annotated Example</h3></div></div></div><p>
|
|
|
46 |
Here is a simple, annotated example of a script which produces an image.
|
|
|
47 |
More examples can be found in <a class="xref" href="examples.html" title="Chapter 5. PHPlot Examples">Chapter 5, <em>PHPlot Examples</em></a>.
|
|
|
48 |
</p><p>
|
|
|
49 |
</p><pre class="programlisting">require_once 'phplot.php';
|
|
|
50 |
</pre><p>
|
|
|
51 |
This brings in the PHPlot source into your script. For this to work,
|
|
|
52 |
PHP needs to be able to find the PHPlot source file. A good way to arrange
|
|
|
53 |
this is to install PHPlot into a directory outside your web server's
|
|
|
54 |
document root and on the PHP Include Path. Other ways are to include a full
|
|
|
55 |
path to <code class="filename">phplot.php</code> when including it, or to copy
|
|
|
56 |
<code class="filename">phplot.php</code> into the same directory as your script.
|
|
|
57 |
</p><p>
|
|
|
58 |
</p><pre class="programlisting">$plot = new PHPlot();
|
|
|
59 |
</pre><p>
|
|
|
60 |
Here we create a new PHPlot object and call it <code class="literal">plot</code>.
|
|
|
61 |
Everything else we do with the plot will be through the $plot object.
|
|
|
62 |
</p><p>
|
|
|
63 |
</p><pre class="programlisting">$plot->SetPlotType('lines');
|
|
|
64 |
$plot->SetDataType('text-data');
|
|
|
65 |
</pre><p>
|
|
|
66 |
Here we select the plot type 'lines', for a line plot
|
|
|
67 |
(see <a class="xref" href="conc-plottypes.html" title="3.4. PHPlot Plot Types">Section 3.4, “PHPlot Plot Types”</a>),
|
|
|
68 |
and indicate our data will be represented in the 'text-data' format
|
|
|
69 |
(see <a class="xref" href="conc-datatypes.html" title="3.3. PHPlot Data Types">Section 3.3, “PHPlot Data Types”</a>).
|
|
|
70 |
</p><p>
|
|
|
71 |
</p><pre class="programlisting">$plot->SetDataValues($data);
|
|
|
72 |
</pre><p>
|
|
|
73 |
The data array $data is where we store the values to be plotted.
|
|
|
74 |
We haven't shown where the data came from, but in a typical application
|
|
|
75 |
it might be from a database query.
|
|
|
76 |
How the data array is constructed is described in
|
|
|
77 |
<a class="xref" href="conc-datatypes.html" title="3.3. PHPlot Data Types">Section 3.3, “PHPlot Data Types”</a>.
|
|
|
78 |
</p><p>
|
|
|
79 |
</p><pre class="programlisting">$plot->SetXDataLabelPos('none');
|
|
|
80 |
$plot->SetLineWidths(3);
|
|
|
81 |
$plot->SetDrawXGrid(True);
|
|
|
82 |
</pre><p>
|
|
|
83 |
These three functions illustrate how to change the appearance of the plot.
|
|
|
84 |
</p><p>
|
|
|
85 |
</p><pre class="programlisting">$plot->DrawGraph();
|
|
|
86 |
</pre><p>
|
|
|
87 |
This final function call outputs the plot.
|
|
|
88 |
More accurately,
|
|
|
89 |
this function creates the plot using all the data and settings
|
|
|
90 |
which were established by previous functions, and then outputs the plot.
|
|
|
91 |
This is a crucial point when using PHPlot: Until you call DrawGraph,
|
|
|
92 |
PHPlot is simply recording all the settings resulting from the functions
|
|
|
93 |
you call, and saving a copy of your data array. Nothing really happens
|
|
|
94 |
until you complete the plot with DrawGraph.
|
|
|
95 |
</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="concepts.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-datatypes.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 3. PHPlot Concepts </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3.3. PHPlot Data Types</td></tr></table></div></body></html>
|