Heim  >  Artikel  >  Backend-Entwicklung  >  php柱状图(JpGraph类库)的生成方法分享

php柱状图(JpGraph类库)的生成方法分享

WBOY
WBOYOriginal
2016-07-25 08:55:34861Durchsuche
  1. DEFINE("CACHE_DIR","/tmp/jpgraph_cache/");'
  2.   DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/");
复制代码

Windows系统改为:  

  1.  DEFINE("CACHE_DIR","c:/apache/htdocs/ jpgraph_cache/");'
  2.   DEFINE("TTF_DIR","c:/windows/fonts");
复制代码

注意事项:   (1)cache(缓存)文件夹路径可以自己定义,而TTF(字体)文件夹必须是%system%/Fonts。   (2)确保PHP对cache(缓存)文件夹有写的权限。 5、   完成上述设置后就可以使用JpGraph了,可以先将JpGraph的例子copy到htdocs文件夹中,运行一下看看。

从数据库中读取数据到jpgraph图表中 1、 将./src/Examples目录中的文件example16.2.php以及./src目录中的文件jpgraph_bar.php、jpgraph_gradient.php、jpgraph_line.php、jpgraph_plotmark.inc、jpgraph.php拷贝到同一目录下。

2、   建立数据库jpg,数据库表test   建立2个字段:   id(主键):int   number:int   并添加一些数据 3、   修改example16.2.php   修改后的代码

  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; // bbs.it-home.org
  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 t'he 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. ?>
复制代码

说明: JpGraph的最新版本为 Version: 3.5.0b1。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn