Recommended features super powerful PHP drawing Library jpgraph_php Tutorial

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

 
 
  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. $l 2datay [$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 ($l 2datay);
  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 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 ...

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