Blame | Letzte Änderung | Log anzeigen | RSS feed
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1"><LINK REL="Start" HREF="index.html"><LINK REL="Contents" HREF="toc.html"><LINK REL="Prev" HREF="43UsingPHPdirectly.html"><LINK REL="Next" HREF="45ChoosingtheimageformatforJpGraph.html"><STYLE TYPE="text/css"><!--BODY { font-family: serif }H1 { font-family: sans-serif }H2 { font-family: sans-serif }H3 { font-family: sans-serif }H4 { font-family: sans-serif }H5 { font-family: sans-serif }H6 { font-family: sans-serif }SUB { font-size: smaller }SUP { font-size: smaller }PRE { font-family: monospace }A { text-decoration: none }--></STYLE></HEAD><BODY><A HREF="toc.html">Contents</A><A HREF="43UsingPHPdirectly.html">Previous</A><A HREF="45ChoosingtheimageformatforJpGraph.html">Next</A><HR NOSHADE><H2><A NAME="4_4">4.4 The basic principle of JpGraph and the creation ofimages</A></H2><P> The common pattern for creating graphs is</P><OL><LI> Create a script that constructs the image, type, colors size and soon.</LI><LI> A wrapper script which contains one or more <IMG> tags to positionthe graphs on the proper HTML page.</LI></OL><P> Of course it is of perfectly possible to call the image scriptdirectly in the browser to just display the generated image in thebrowser.</P><P> You should remember that it is also possible to pass arguments tothe image script via the normal HTTP GET/POST arguments. For example<DIVclass="phpscript"><CODE><FONT color="#000000"> <FONT color="#0000BB"> </FONT><FONTcolor="#007700"><</FONT><FONT color="#0000BB">img src</FONT><FONT color="#007700">=</FONT><FONT color="#DD0000">"showgraph.php?a=1&b=2"</FONT><FONT color="#007700">></FONT><FONT color="#0000BB"></FONT></FONT></CODE></DIV></P><P> This could for example be used to control the appearance of theimage or perhaps send data to the image which will be displayed. Notethat this is probably not the best way to send large amount of data toplot. Instead the only practical way, for large data sizes, is to getall the data in the image script, perhaps from a DB. Anotheralternative for large amount of data to be sent to the image script isby creating a POST request to the image script.<DIV class="note"><B>Note:</B><B> Forcing the browser to update your image</B> Some browsermay not send back a request to the web browser unless the user presses"Refresh" (F5 - in most browsers). This can lead to problems that theuser is seeing old data. A simple trick is to add a dummy time argumentwhich is not used in the script. For example<PRE>echo '<img src="myimagescript.php?dummy='.now().'">';</PRE>It is also important to be aware of any internal caching the browsermight do. The general problem with dynamically generated images is thatthe image generating script (file) remains the same. This makes thebrowser believe that the data hasn't changed and if the browser alreadyhas issues a previous GET request and has the data cached it will notsend a new GET if the timestamp on the file is the same since it thenbelieves it my use the old cached version.</DIV></P><P> When it comes to the structure of your imaging script they willgenerally have the structure<DIV class="phpscript"><CODE><FONT color="#000000"><FONT color="#0000BB"> </FONT><FONT color="#FF8000">// ... Include necessary headers<BR><BR></FONT><FONT color="#0000BB">$graph </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">Graph</FONT><FONT color="#007700">(</FONT><FONTcolor="#0000BB">$width</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">$height</FONT><FONT color="#007700">, ...);<BR><BR></FONT><FONT color="#FF8000">// ... code to construct the graph details<BR><BR></FONT><FONT color="#0000BB">$graph</FONT><FONT color="#007700">-></FONT><FONTcolor="#0000BB">Stroke</FONT><FONT color="#007700">();<BR></FONT><FONT color="#0000BB"></FONT></FONT></CODE></DIV></P><P> JpGraph is completely Object oriented so all calls will be action onspecific instances of classes. One of the fundamental classes is theGraph() class which represents the entire graph.</P><P> After the creation of the Graph() object all the code lines toconstruct the details of the graph are added.</P><P> The final method called in an image script will most likely be the<I>Graph::Stroke()</I> method. This will send the constructed image backto the browser. A variation of this is used if the graph are supposedto have image maps. In that case the final method will be<I>Graph::StrokeCSIM()</I></P><P> In addition to this standard usage pattern you can also choose to</P><UL><LI> ... send the graph directly to a file</LI><LI> ... access the GD image handler for further image processing (alsoneeded to include the image in an PDF file)</LI><LI> ... make use of the builtin cache system to send back a previouslygenerated image</LI></UL><P> The cache system, which lessens the burden of the PHP server, worksby avoiding o run all the code that follows the initial Graph() call bychecking if the image has already been created and in that casedirectly send back the previously created (and stored in a file) imageto the browser. When using the cache system a filename must bespecified in the initial Graph() call which is used to store the imagein the cache system and possibly also a timeout value to indicate howlong the image in the cache directory should be valid.</P><P> In many of the examples in this manual the following pattern will beused<DIV class="phpscript"><CODE><FONT color="#000000"> <FONT color="#0000BB"> $graph </FONT><FONT color="#007700">= new </FONT><FONT color="#0000BB">Graph</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">300</FONT><FONTcolor="#007700">,</FONT><FONT color="#0000BB">200</FONT><FONT color="#007700">,</FONT><FONT color="#DD0000">"auto"</FONT><FONT color="#007700">);</FONT><FONTcolor="#0000BB"></FONT></FONT></CODE></DIV></P><P> The two first parameters specify the width and height of the graphand the third parameter the name of the image file in the cachedirectory. The special name<I> 'auto'</I> indicates that the image filewill be given the same name as the image script but with the extensionchanged to indicate the graphic format used, i.e '.jpg', '.png' and soon.</P><P> Please note that the cache system by default is disabled and must beenabled by setting the proper define in the file "jpg-config.inc.php"</P><HR NOSHADE><A HREF="toc.html">Contents</A><A HREF="43UsingPHPdirectly.html">Previous</A><A HREF="45ChoosingtheimageformatforJpGraph.html">Next</A></BODY></HTML>