基于GD库和JPgraph库
说明:对于具体的Jpgraph类库的用法,请参照官网的文档:http://jpgraph.net/download/manuals/classref/index.html
对于下面涉及到的代码,里面的JPgraph类文件可能路径不一样,我是按照我自己文件存放的路径引入的。你们可自行更改
1、使用柱形图统计月销量额
<?php /* 具体的Jpgraph类库的用法,请参照官网的文档: http://jpgraph.net/download/manuals/classref/index.html */ /*****************************使用柱形图统计月销量额**************************/ include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph.php"); include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_bar.php"); //引用柱形图对象所在的文件 $datay = array(160,180,203,289,405,488,489,408,299,166,187,105); //定义数组 $graph = new Graph(600,300,"auto"); //创建画布 --要JPgraph库的支持 $graph->SetScale("textlin"); $graph->yaxis->scale->SetGrace(20); $graph->SetShadow(); //创建画布阴影 //设置统计图所在画布的位置,左边距40、右边距30、上边距30、下边距40,单位为像素 $graph->img->SetMargin(40,30,30,40); $bplot = new BarPlot($datay); //创建一个矩形的对象 $bplot->SetFillColor('orange'); //设置柱形图的颜色 $bplot->value->Show(); //设置显示数字 $bplot->value->SetFormat('%d'); //在柱形图中显示格式化的图书销量 $graph->Add($bplot); //将矩形图添加到图像中 $graph->SetMarginColor("lightblue"); //设置画布背景色为浅蓝色 $graph->title->Set("<<PHP for Introduction 2009>> Annual sales statistics"); //创建标题 //设置X轴坐标文字 $a=array("1 月","2 月","3 月","4 月","5 月","6 月","7 月","8 月","9 月","10 月","11 月","12 月"); $graph->xaxis->SetTickLabels($a); //设置X轴 $graph->title->SetFont(FF_SIMSUN); //设置标题字体 $graph->xaxis->SetFont(FF_SIMSUN); //设置X轴的字体 $graph->Stroke(); //输出图像 ?>
2、使用折线图统计月销量额
<?php /***************************使用折线图统计月销量额*******************************/ include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph.php"); include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_line.php"); //引用折线图LinePlot类文件 //定义数组 $datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460); $graph = new Graph(600,300,"auto"); //设置统计图所在画布的位置,左边距50,右边距40,上边距30,下边距40,单位为像素 $graph->img->SetMargin(50,40,30,40); $graph->img->SetAntiAliasing(); //设置折线的平滑状态 $graph->SetScale("textlin"); //设置刻度样式 $graph->SetShadow(); //创建画布阴影 $graph->title->Set("<<PHP for Introduction 2009>> Annual sales statistics"); //设置标题 $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体 $graph->SetMarginColor("lightblue"); //设置画布背景色为淡蓝色 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体 $graph->xaxis->SetPos("min"); // $graph->yaxis->HideZeroLabel(); $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#BBCCFF@0.5'); //X轴 $a=array("1 月","2 月","3 月","4 月","5 月","6 月","7 月","8 月","9 月","10 月","11 月","12 月"); $graph->xaxis->SetTickLabels($a); //设置X轴 $graph->xaxis->SetFont(FF_SIMSUN); //设置X坐轴的字体 $graph->yscale->SetGrace(20); $p1 = new LinePlot($datay); //创建折线图对象 $p1->mark->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记 $p1->mark->SetFillColor("red"); //设置填充颜色 $p1->mark->SetWidth(4); //设置圆形标记的直径为4像素 $p1->SetColor("blue"); //设置折线颜色为蓝色 $p1->SetCenter(); //在X轴的各坐标点中心位置绘制折线 $graph->Add($p1); //在统计图上绘制折线 $graph->Stroke(); //输出图像 ?>
3、使用饼形图(扇形图)统计月销量额
<?php /*************************使用3D饼图进行统计***************************/ include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph.php"); include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_pie.php"); //引用 3D 饼形图 PiePlot3D对象所在的类文件 include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_pie3d.php"); //定义数组 $data = array(266036,295621,335851,254256,254254,685425); $graph = new PieGraph(540,260,'auto'); //创建画布 $graph->SetShadow(); //设置画布阴影 //创建标题 $graph->title->Set("<<PHP for Introduction 2009>> Annual sales statistics -3DPie"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体 $graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //设置图例字体 $p1 = new PiePlot3D($data); //创建3D饼图对象 $p1->SetLegends(array("IT","Appliance","Daily","Clothing","Health","Food")); $targ = array("D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=1","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=2","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=3","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=4","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=5","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=6"); $alts = array("val=%d","val=%d","val=%d","val=%d","val=%d","val=%d"); $p1->SetCSIMTargets($targ,$alts); $p1->SetCenter(0.4,0.5); //设置饼图所在画布的位置 $graph->Add($p1); //将3D饼形图添加到图像中 $graph->StrokeCSIM(); //将图像输出到游览器 ?>