We recommend a PHP image library JpGraph with super powerful functions. Background knowledge JpGraph: In the past, you must master complex and abstract drawing functions when using PHP for graph construction, or use the classes of bar charts and pie charts downloaded from the Internet. No unified
Background
JpGraph: In the past, you must master complex and abstract drawing functions when using PHP, or use the classes of bar chart and pie chart downloaded from the Internet. There is no unified chart class for quick chart development.
Now we have a new option: JpGraph. Class libraries dedicated to providing charts. It makes plotting a very simple task. you only need to extract the relevant data from the database, define the title, chart type, and then hand over the data to JpGraph, you only need to know a few built-in JpGraph functions (you can refer to the examples of JpGraph) to draw a very dazzling chart!
I. JpGraph installation method:
1. download the latest version at http://www.aditus.nu/jpgraph.
2. make sure that your PHP version is 4.04 (preferably 4.1.1) and supports the GD Library. Make sure that the GD library runs properly. you can run phpinfo () to check whether the GD library information exists. At the same time, the version of the GD library must be 2.0, instead of 1.0.
3. decompress the downloaded JpGraph package to any folder.
4. set jpgraph. php (main configuration file of jpgraph ). Set the cache folder and TTF folder of jpgraph.
Rows 35 and 38 respectively
35 // DEFINE ("CACHE_DIR", "/tmp/jpgraph_cache /");
38 // DEFINE ("TTF_DIR", "/usr/X11R6/lib/X11/fonts/truetype /");
For Linux:
DEFINE ("CACHE_DIR", "/tmp/jpgraph_cache /");
DEFINE ("TTF_DIR", "/usr/X11R6/lib/X11/fonts/truetype /");
For Windows:
DEFINE ("CACHE_DIR", "c:/apache/htdocs/jpgraph_cache /");
DEFINE ("TTF_DIR", "c:/windows/fonts ");
Note:
(1) the cache (cache) folder path can be customized, while the TTF (font) folder must be % system %/Fonts.
(2) make sure that PHP has the write permission on the cache folder.
5. after completing the preceding settings, you can use JpGraph. copy the JpGraph example to the htdocs folder and run it. Haha, more than 200 examples, including various charts, are enough to learn for a while.
For more information about bkJia, see bkJia video tutorial 85: PHP graph-type JPGraph Quick Start configuration and application.
Read data from the database to the jpgraph chart
1. copy example16.2.php files in the./src/Examples Directory, jpgraph_bar.php files in the./src Directory, jpgraph_line.php files, jpgraph_plotmark.inc, and jpgraph. php files to the same directory.
2. create a database jpg and a database table test.
Create two fields:
Id (primary key): int
Number: int
And add some data
3. modify example16.2.php
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)) {
- $l2datay[$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($l2datay);
- $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 view the result.
JpGraph: In the past, you must master complex and abstract drawing functions when using PHP, or use the classes of bar charts and pie charts downloaded from the Internet. There is no unified...