您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jpgraph+php+mysql生成柱状图(含附件)

2025/6/2 6:58:20发布15次查看
很详细的注释,希望对像我一样刚入门的朋友有帮助,附代码:
create database `jpgraph`; //创建数据库
use `jpgraph`;
//创建测试表
drop table if exists `jpg_temp`;
create table `jpg_temp` (
`year` int(11) not null,
`money` float not null,
`number` int(11) not null
) engine=innodb default charset=latin1;
//测试数据
insert into `jpg_temp`(`year`,`money`,`number`) values (2007,150.78,120),(2008,100,90),(2009,120,100),(2010,100,78),(2011,70.56,60),(2012,90,100),(2013,140,180),(2014,150,200),(2015,130,110),(2016,110.85,150),(2020,180,230),(2050,200,210);
//数据库链接文件
/*
* projectname:jpgraph
* createdate:2011-5-12
* author:wilr
* description:数据库链接
*/
$dbconn=mysql_connect(127.0.0.1,root,root);
if(!$dbconn)
echo '数据库通信失败';
mysql_select_db(jpgraph);
mysql_query(set names 'utf8',$dbconn);
$sql= select * from jpg_temp;
$result=mysql_query($sql,$dbconn);
$rowcount=mysql_num_rows($result);
$datay=array();
$datax=array();
$number=array();
while ($row=mysql_fetch_array($result)){
$datay[]=$row[money];
$datax[]=$row[year];
$number[]=$row[number];
}
//echo each($datay);
//print_r($datay);
mysql_close($dbconn);
?>
//生成图表类
require_once ('jpgraph/jpgraph.php'); //载入基本类
require_once ('jpgraph/jpgraph_bar.php'); //载入柱状图
include_once('db_config.php'); //载入数据处理文件
$graph=new graph(900,500); //创建一个图表 指定大小
$graph->setscale(textlin); //设置坐标刻度类型
$graph->img->setmargin(40,180,30,40);//设置统计图边距 左、右、上、下
//$graph->setmargincolor(lightblue);//设置画布背景色 淡蓝色
//$graph->setbackgroundimage('stship.jpg',bgimg_copy); //设置背景图片
//$graph->img->setangle(45); //设置图形在图像中的角度
//设置标题信息
$graph->title->set('wilr测试报表'); //设置统计图标题
$graph->title->setfont(ff_simsun,fs_bold,20); //设置标题字体
$graph->title->setmargin(3);//设置标题的边距
//设置x轴信息
$graph->xaxis->title->set('(单位:年)'); //标题
$graph->xaxis->title->setfont(ff_simsun,fs_bold,10); //标题字体 大小
$graph->xaxis->title->setcolor('black');//颜色
$graph->xaxis->setfont(ff_simsun,fs_bold,10);//x轴刻度字体 大小
$graph->xaxis->setcolor('black');//x轴刻度颜色
$graph->xaxis->setticklabels($datax); //设置x轴标记
$graph->xaxis->setlabelangle(0);//设置x轴的显示值的角度;
//设置y轴的信息
$graph->yaxis->setfont(ff_simsun,fs_bold,10);//标题
$graph->yaxis->setcolor('black');//颜色
$graph->ygrid->setcolor('black@0.9');//x,y交叉表格颜色和透明度 @为程度值
$graph->yaxis->scale->setgrace(0);//设置y轴显示值柔韧度(解释有点问题 呵呵 原谅)
//设置数据
$bplot1 = new barplot($datay);
$bplot2 = new barplot($number);
//设置柱状图柱颜色和透明度
$bplot1->setfillcolor('orange@0.4');
$bplot2->setfillcolor('brown@0.4');
//设置值显示
$bplot1->value->show(); //显示值
$bplot1->value->setfont(ff_simsun,fs_bold,10);//显示字体大小
$bplot1->value->setangle(90); //显示角度
$bplot1->value->setformat('%0.2f'); //显示格式 0.2f:精确到小属数点后2位
$bplot2->value->show();
$bplot2->value->setfont(ff_simsun,fs_bold,10);
$bplot2->value->setangle(90);
$bplot2->value->setformat('%0.0f');
//设置图列标签
$graph->legend->setfillcolor('lightblue@0.9');//设置图列标签背景颜色和透明度
$graph->legend->pos(0.01,0.12,right,center);//位置
$graph->legend->setfont(ff_simsun,fs_normal,10);//显示字体 大小
$bplot1->setlegend('消费金额(单位:万元)');
$bplot2->setlegend('人数(单位:万人次)');
//设置每个柱状图的颜色和阴影透明度
$bplot1->setshadow('black@0.4');
$bplot2->setshadow('black@0.4');
//生成图列
$gbarplot = new groupbarplot(array($bplot1,$bplot2));
$gbarplot->setwidth(0.5); //柱状的宽度
$graph->add($gbarplot);
$graph->stroke(); //输出图像
?>
附件:  效果图.zip (25 k)
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product