从数据库中读取数据到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 修改后的代码
<?php 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 t'he 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、刷新页面即可看到结果 |