利用JpGraph,可视化数据库中的数据

来源:互联网 发布:重庆网络审批平台 编辑:程序博客网 时间:2024/05/16 23:49

1. 数据库配置文件(创建数据库和表)dbconfig.php文件

<?phpmysql_connect('localhost', 'root', '');mysql_select_db('test');$createtb = "create table if not exists user(        id int(20) primary key auto_increment,        username varchar(50),        password varchar(50),        time int(11)    )";mysql_query($createtb);?>

2. 往user 数据表中添加两条数据
这里写图片描述

3. 读取数据表中的数据,利用JpGraph进行plot, plot.php文件

<?php // content="text/plain; charset=utf-8"require_once ('../test/jpgraph/jpgraph.php');require_once ('../test/jpgraph/jpgraph_bar.php');include "dbconfig.php";$sql="select * from user order by id";$rst=mysql_query($sql);//$data1y=array(1,2);$data2y=array();while($row=mysql_fetch_assoc($rst)){    array_push($data2y,$row['id']);}// Create the graph. These two calls are always required$graph = new Graph(700,600); $graph->SetScale("textlin");$graph->SetShadow();$graph->img->SetMargin(40,30,20,40);// Create the bar plots//$b1plot = new BarPlot($data1y);//$b1plot->SetFillColor("orange");//$b1plot->value->Show();$b2plot = new BarPlot($data2y);$b2plot->SetFillColor("blue");$b2plot->value->Show();// Create the grouped bar plot//$gbplot = new AccBarPlot(array($b1plot,$b2plot));$gbplot = new AccBarPlot(array($b2plot));// ...and add it to the graPH$graph->Add($gbplot);$graph->title->Set("Accumulated bar plots");$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);// Display the graph$graph->Stroke();?>

4. 运行plot.php文件,效果如下,
这里写图片描述

5. 具体细节在另一片博客上写得很详细http://blog.csdn.net/canhui_wang/article/details/78563745

参考
http://blog.csdn.net/canhui_wang/article/details/78563745

原创粉丝点击