下载:http://jpgraph.net/download/download.php?p=5 如:jpgraph-3.5.0
解压到如:D:\dev\jpgraph-3.5.0\src
配置php: D:\Program Files\PHP\php.ini
修改 include_path和date.time.zone
如:
include_path = ".;D:\dev\jpgraph"
date.timezone = UTC
重启apache
曲线:
<?php // content="text/plain; charset=utf-8" require_once ('src/jpgraph.php'); require_once ('src/jpgraph_line.php'); // 输入的数据 $ydata = array(11,3,8,12,5,1,9,13,5,7); // 创建图形 $graph = new Graph(350,250); $graph->SetScale('textlin'); // 创建折线图 $lineplot=new LinePlot($ydata); $lineplot->SetColor('blue'); // 在图上创建测量点 $graph->Add($lineplot); // 显示图形 $graph->Stroke(); ?>柱图:
<?php require_once ("src/jpgraph.php"); require_once ("src/jpgraph_pie.php"); require_once ("src/jpgraph_pie3d.php"); $data=array("1"=>100,"2"=>30,"3" =>21,4=>'165',5=>'182'); createImage($data,40,40,300); /* * author http://www.phpernote.com/ * php生成柱状统计图程序 * $data 二维数组形式的数据 * $twidth 柱形宽度 * $tspace 两个柱形之间的距离 * $height 柱状图的高度 */ function createImage($data,$twidth,$tspace,$height){ header("Content-Type:image/jpeg"); $dataname=array(); $datavalue=array();//data里面的值 $i=0; $j=0; $k=0; $num=sizeof($data); foreach($data as $key=>$val){ $dataname[]=$key; $datavalue[]=$val; } $width=$num*($twidth+$tspace)+100 ;//获取图像的宽度 $im=imagecreate($width,$height);//创建图像 $bgcolor=imagecolorallocate($im,255,255,255);//背景色 $jcolor=imagecolorallocate($im,255,0,0);//矩形的背景色 $acolor=imagecolorallocate($im,0,0,0);//线的颜色 imageline($im,25,$height-20,$width-5,$height-20,$acolor);//X轴 imageline($im,25,$height-20,25,2,$acolor);//Y轴 while($i<$num){ imagefilledrectangle($im,$i*($tspace+$twidth)+40,$height-$datavalue[$i]-20,$i*($twidth+$tspace)+$tspace+40,$height-21,$jcolor);//画矩形 imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-$datavalue[$i]-35,$datavalue[$i],$acolor);//在柱子上面写出值 imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-15,$dataname[$i],$acolor);//在柱子下面写出值 $i++; } while($j<($height)/10){ imageline($im,25,($height-20)-$j*10,28,($height-20)-$j*10,$acolor);//画出刻度 imagestring($im,2,5,($height-30)-$j*10,$j*10,$acolor);//标出刻度值 $j=$j+10; } imagejpeg($im); }
参考: http://www.alixixi.com/program/a/2011101775468.shtml
配置参考:
<html><body><h1> <?php phpinfo(); ?> </h1></body></html>