Background Knowledge
Jpgraph: Before using PHP, you must master the complex abstract drawing function, or the use of some online download style chart, pie chart class to achieve. There is no unified chart class to implement the rapid development of charts.
Now we have a new choice: Jpgraph. A class library dedicated to charting. It makes the drawing a very simple thing, you just take the data from the database, define the title, the chart type, then the matter to the jpgraph, only a few jpgraph built-in functions (can refer to jpgraph with example learning), You can draw a very dazzling chart!
First, Jpgraph installation method:
1, first to http://www.aditus.nu/jpgraph/download the latest version.
2. Make sure that your PHP version is at least 4.04 (preferably 4.1.1) and supports the GD library. You must make sure that the GD library is functioning properly and can be judged by running Phpinfo () to see if the information for the GD library exists. Also need to have the GD library version should be 2.0, instead of 1.0.
3. Extract the downloaded Jpgraph compressed package to any folder.
4. Set jpgraph.php (Master profile for Jpgraph). Set the cache (cache) folder for Jpgraph, and the TTF (font) folder.
In lines 35 and 38, respectively.
//DEFINE ("Cache_dir", "/tmp/jpgraph_cache/");
+//DEFINE ("Ttf_dir", "/usr/x11r6/lib/x11/fonts/truetype/");
The Linux system changes to:
DEFINE ("Cache_dir", "/tmp/jpgraph_cache/");
DEFINE ("Ttf_dir", "/usr/x11r6/lib/x11/fonts/truetype/");
Windows system instead:
DEFINE ("Cache_dir", "c:/apache/htdocs/jpgraph_cache/");
DEFINE ("Ttf_dir", "c:/windows/fonts");
Precautions:
(1) The cache folder path can be defined by itself, and the TTF (font) folder must be%system%/fonts.
(2) Make sure that PHP has write permissions to the cache folder.
5, the completion of the above settings can be used Jpgraph, you can first copy the example of Jpgraph to Htdocs folder, run a look. Hehe, more than 200 examples, including all kinds of charts, enough to learn for a while.
In the actual use, if there are some do not understand or do not understand the place, you can refer to Bkjia Chinese published Bkjia video Tutorial 85:php Chart class Jpgraph get started configuration and application
Reading data from the database into the jpgraph chart
1. File example16.2.php in the./src/examples directory and files in the./src directory jpgraph_bar.php, jpgraph_gradient.php, jpgraph_line.php, Jpgraph_plotmark.inc, jpgraph.php copy to the same directory.
2. Set up database JPG, database table test
Create 2 fields:
ID (primary key): int
Number:int
and add some data
3, modify the example16.2.php
The modified code
-
- include ("jpgraph.php");
- include ("jpgraph_line.php");
- include ("jpgraph_bar.php");
- $connect =mysql_connect ("localhost","root","");
- mysql_select_db ("jpg",$connect);
- $query =mysql_query ("SELECT * from Test",$connect);
- $i = 0;
- while ($array=mysql_fetch_array ($query)) {
- $l 2datay [$i]= $array ["number"];
- $i ++;
- }
- mysql_close ($connect);
- //Create the graph.
- $graph = New Graph (400,200,"Auto");
- $graph ->setscale ("Textlin");
- $graph ->img->setmargin (40,130,20,40);
- $graph ->setshadow ();
- //Create the bar plot
- $bplot = New Barplot ($l 2datay);
- $bplot ->setfillcolor ("Orange");
- $bplot ->setlegend ("Result");
- //Add The plots to the graph
- $graph ->add ($bplot);
- $graph ->title->set ("Adding a line plot to a bar graph v1");
- $graph ->xaxis->title->set ("X-title");
- $graph ->yaxis->title->set ("Y-title");
- $graph ->title->setfont (Ff_font1,fs_bold);
- $graph ->yaxis->title->setfont (Ff_font1,fs_bold);
- $graph ->xaxis->title->setfont (Ff_font1,fs_bold);
- //$graph->xaxis->setticklabels ($datax);
- //$graph->xaxis->settexttickinterval (2);
- //Display the graph
- $graph ->stroke ();
- ?>
4. Refresh the page to see the results
http://www.bkjia.com/PHPjc/486314.html www.bkjia.com true http://www.bkjia.com/PHPjc/486314.html techarticle background Knowledge Jpgraph: Before using PHP to draw a complex abstract drawing function must be mastered, or with some Web-based download style chart, pie chart class to achieve. Without a unified ...