Home >Backend Development >PHP Tutorial >Sharing how to generate php histogram (JpGraph class library)

Sharing how to generate php histogram (JpGraph class library)

WBOY
WBOYOriginal
2016-07-25 08:55:34927browse
  1. DEFINE("CACHE_DIR","/tmp/jpgraph_cache/");'
  2.  DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/");
Copy code

Windows system changed to:

  1. DEFINE("CACHE_DIR","c:/apache/htdocs/ jpgraph_cache/");
  2. Notes: (1) The cache folder path can be defined by yourself, while the TTF (font) folder must be %system%/Fonts. (2) Make sure that PHP has write permissions on the cache folder. 5. After completing the above settings, you can use JpGraph. You can first copy the JpGraph example to the htdocs folder and run it to take a look.
Read data from database into jpgraph chart 1, Copy the file example16.2.php in the ./src/Examples directory and the files jpgraph_bar.php, jpgraph_gradient.php, jpgraph_line.php, jpgraph_plotmark.inc, and jpgraph.php in the ./src directory to the same directory. 2. Establish database jpg, database table test Create 2 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"); "root","");
     mysql_select_db("jpg",$connect);
  1.  $query=mysql_query("select * from test",$connect);
  2.  $i=0; // bbs.it-home. org
  3.  while ($array=mysql_fetch_array($query)) {
  4.  $l2datay[$i]=$array["number"];
  5.  $i++;
  6.  }
  7.  mysql_close($connect);
  8.   // Create the graph.
  9.  $graph = new Graph(400,200,"auto");
  10.  $graph->SetScale("textlin");

  11.  $graph->img->SetMargin(40,130,20,40);
  12.  $graph- >SetShadow();
  13.   // Create the bar plot
  14.   $bplot = new BarPlot($l2datay);
  15.   $bplot->SetFillColor("orange");
  16.   $bplot->SetLegend("Result");
  17.   // Add the plots to t'he graph
  18.   $graph->Add($bplot);
  19.   $graph->title->Set("Adding a line plot to a bar graph v1");
  20.  $ graph->xaxis->title->Set("X-title");
  21.  $graph->yaxis->title->Set("Y-title");
  22.  $graph-> title->SetFont(FF_FONT1,FS_BOLD);
  23.   $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
  24.  $graph->xaxis->title->SetFont(FF_FONT1, FS_BOLD);
  25.    //$graph->xaxis->SetTickLabels($datax);
  26.    //$graph->xaxis->SetTextTickInterval(2);
  27.   // Display the graph
  28.   $graph-> Stroke();
  29. ?>
  30. Copy code
  31. Instructions: The latest version of JpGraph is Version: 3.5.0b1.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn