PHP image library JpGraph_PHP tutorial with super powerful features recommended

Source: Internet
Author: User
Tags php graph
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

 
 
  1. include ("jpgraph.php");
  2. include ("jpgraph_line.php");
  3. include ("jpgraph_bar.php");
  4. $connect=mysql_connect("localhost","root","");
  5. mysql_select_db("jpg",$connect);
  6. $query=mysql_query("select * from test",$connect);
  7. $i=0;
  8. while ($array=mysql_fetch_array($query)) {
  9. $l2datay[$i]=$array["number"];
  10. $i++;
  11. }
  12. mysql_close($connect);
  13. // Create the graph. 
  14. $graph = new Graph(400,200,"auto"); 
  15. $graph->SetScale("textlin");
  16. $graph->img->SetMargin(40,130,20,40);
  17. $graph->SetShadow();
  18. // Create the bar plot
  19. $bplot = new BarPlot($l2datay);
  20. $bplot->SetFillColor("orange");
  21. $bplot->SetLegend("Result");
  22. // Add the plots to the graph
  23. $graph->Add($bplot);
  24. $graph->title->Set("Adding a line plot to a bar graph v1");
  25. $graph->xaxis->title->Set("X-title");
  26. $graph->yaxis->title->Set("Y-title");
  27. $graph->title->SetFont(FF_FONT1,FS_BOLD);
  28. $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
  29. $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
  30. //$graph->xaxis->SetTickLabels($datax);
  31. //$graph->xaxis->SetTextTickInterval(2);
  32. // Display the graph
  33. $graph->Stroke();
  34. ?>

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.