1:官网
2:配置
首先要保证PHP打开了Gd2的扩展:
打开PHP.ini,定位到extension=php_gd2.dll,把前面的分号删掉。
然后下载Jpgraph,http://www.aditus.nu/jpgraph/,解压到一个文件夹中。如 E:\Software\web\www\jpgraph。
打开PHP.ini,修改include_path参数,增加Jpgraph的路径,如include_path=",;E:\Software\web\www\jpgraph",重启Apache服务。
这样环境就配好了。
打开PHP.ini,定位到extension=php_gd2.dll,把前面的分号删掉。
然后下载Jpgraph,http://www.aditus.nu/jpgraph/,解压到一个文件夹中。如 E:\Software\web\www\jpgraph。
打开PHP.ini,修改include_path参数,增加Jpgraph的路径,如include_path=",;E:\Software\web\www\jpgraph",重启Apache服务。
这样环境就配好了。
3:列子
1):柱型图(未解决中文问题)
<?php
require_once ('Examples/jpgraph/jpgraph.php'); //载入基本类
require_once ('Examples/jpgraph/jpgraph_bar.php');//载入柱形图类
$datay=array(300,230,400);//你要显示的数据
$graph = new Graph(400,250);//创建一个图,参数为宽度和高度
$graph->SetScale("textlin");
$graph->img->SetMargin(25,15,25,15);//设置图标边距
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,24); //设置字体,类型,大小
$graph->title->Set('www.phpddt.com');//图片头部标题
$graph->title->SetColor('red');//标题字体颜色
$graph->xaxis->SetFont(FF_FONT1);//设置英文字体
$graph->yaxis->SetFont(FF_FONT1);
$bplot = new BarPlot($datay);
$bplot->SetWidth(0.4);//柱状的宽度
$bplot->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);
$bplot->SetColor("navy");
$graph->Add($bplot);
$graph->Stroke();
?>
2)折线图
<?php
include ("jpgraph.php");
include ("jpgraph_bar.php");
include ("jpgraph_line.php");
// y 轴数据,以数组形式赋值
$ydata = array(12,4,9,15,11,10,9,7,15,7);
// 创建 Graph 类,350 为宽度,250 长度,auto:表示生成的缓存文件名是该文件的文件名+扩展名(.jpg .png .gif ……)
$graph = new Graph(350,250,"auto");
// 设置刻度类型,x轴刻度可作为文本标注的直线刻度,y轴为直线刻度
$graph->SetScale("textlin");
// 创建坐标类,将y轴数据注入
$lineplot=new LinePlot($ydata);
// y 轴连线设定为蓝色
$lineplot->SetColor("blue");
// 坐标类注入图标类
$graph->Add($lineplot);
// 显示图
$graph->Stroke();
?>
3):流量图
include ('jpgraph/jpgraph.php');
include ("jpgraph/jpgraph_line.php");
include ("jpgraph/jpgraph_date.php");
$graph = new Graph(900, 150, "auto");
//$graph->title->Set("$title");
$graph->SetScale('datlin'); // 以时间来自适应X刻度
$graph->xgrid->Show(); // 显示网格
$graph->SetMarginColor('green@0.95');
$graph->SetMargin(45, 10, 5, 25);
$graph->xaxis->SetTickSide(SIDE_BOTTOM);
$graph->yaxis->SetTickSide(SIDE_LEFT);
$graph->yaxis->title->Set("$danwei");
$p_transmit = new LinePlot($data_arr);
$p_transmit->SetColor("#006600");
$p_transmit->SetFillColor('#006600');
$graph->xaxis->SetTickLabels($xdata);
$graph->Add($p_transmit);
$graph->xaxis->SetLabelAngle(90);
$graph->Stroke();
4:处理中文乱码
1):设置中文字体(在linux中)
在jpgraph.php中有以下这样一段代码是设置字体文件路径的
if (!defined('TTF_DIR')) {
if (strstr( PHP_OS, 'WIN') ) {
$sroot = getenv('SystemRoot');
if( empty($sroot) ) {
$t = new ErrMsgText();
$msg = $t->Get(12,$file,$lineno);
die($msg);
}
else {
define('TTF_DIR', $sroot.'/fonts/');
}
} else {
define('TTF_DIR','/usr/share/fonts/truetype/');ç (我的作法是将windows下的fonts文件夹下的字体全部COPY到/usr/local/fonts/truetype)
}
}
if (strstr( PHP_OS, 'WIN') ) {
$sroot = getenv('SystemRoot');
if( empty($sroot) ) {
$t = new ErrMsgText();
$msg = $t->Get(12,$file,$lineno);
die($msg);
}
else {
define('TTF_DIR', $sroot.'/fonts/');
}
} else {
define('TTF_DIR','/usr/share/fonts/truetype/');ç (我的作法是将windows下的fonts文件夹下的字体全部COPY到/usr/local/fonts/truetype)
}
}
2):php中显示中文
方法一:
$title="流量图";
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);
方法二:(未验证)
修改源文件jpgraph_ttf.inc.php
在第99-106行,改成下面这样子
elseif( $aFF === FF_SIMSUN ) {
// Do Chinese conversion
/*
if( $this->g2312 == null ) {
include_once 'jpgraph_gb2312.php' ;
$this->g2312 = new GB2312toUTF8();
}
return $this->g2312->gb2utf8($aTxt);
*/
return $aTxt;
}
在第99-106行,改成下面这样子
elseif( $aFF === FF_SIMSUN ) {
// Do Chinese conversion
/*
if( $this->g2312 == null ) {
include_once 'jpgraph_gb2312.php' ;
$this->g2312 = new GB2312toUTF8();
}
return $this->g2312->gb2utf8($aTxt);
*/
return $aTxt;
}
补充:
jpgraph默认显示汉字时是把汉字编码认为gb2312,转化为utf-8以后再显示。
这样的话,如果你的文件编码是gb2312,SetFont方法的第一个参数为FF_SIMSUN即可。
如果你是utf-8编码你还需要先把汉字编码转化为gb2312,这样你的汉字才可以正常显示。
3):代码
<?php
include_once("jpgraph.php");
include_once("jpgraph_line.php");
// y 轴数据,以数组形式赋值
$ydata = array(11,3,8,12,5,1,9,13,5,7);
// 创建 Graph 类,350 为宽度,250 长度,auto:表示生成的缓存文件名是该文件的文件名+扩展名(.jpg .png .gif ……)
$graph = new Graph(350,250,"auto");
$title="流量图";
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);
// 设置刻度类型,x轴刻度可作为文本标注的直线刻度,y轴为直线刻度
$graph->SetScale("textlin");
// 创建坐标类,将y轴数据注入
$lineplot=new LinePlot($ydata);
// y 轴连线设定为蓝色
$lineplot->SetColor("blue");
// 坐标类注入图标类
$graph->Add($lineplot);
// 显示图
$graph->Stroke();
?>
5:颜色处理
1:通过颜色名称来指定颜色
SetColor(’white’)
SetFillColor(’orange’)
2:通过RGB值来指定颜色
SetColor( array(0xff,0xff,0xff))
SetFillColor( array(0×44,0×54,0xa4))
3:使用HTML颜色指定
SetColor(’#12be7a’)
SetFillColor(’#99eff5′)
4:颜色的微调处理
SetFillColor(’red@0.2′), 稍透明的红色
SetFillColor(’red@0.8′), 几乎全透明的颜色
5:使用jpgraph调整亮度
SetColor(’red:1.5′), 明亮的hongse
SetColor(’#3485a9:1.8), 明亮的蓝绿色
6:SetColor(’red@0.7:1.2′)
SetFillColor(’#4545aa@0.3:1.5′)
@后的第一个数值代表透明度,第二个数值代表亮度
SetColor(’white’)
SetFillColor(’orange’)
2:通过RGB值来指定颜色
SetColor( array(0xff,0xff,0xff))
SetFillColor( array(0×44,0×54,0xa4))
3:使用HTML颜色指定
SetColor(’#12be7a’)
SetFillColor(’#99eff5′)
4:颜色的微调处理
SetFillColor(’red@0.2′), 稍透明的红色
SetFillColor(’red@0.8′), 几乎全透明的颜色
5:使用jpgraph调整亮度
SetColor(’red:1.5′), 明亮的hongse
SetColor(’#3485a9:1.8), 明亮的蓝绿色
6:SetColor(’red@0.7:1.2′)
SetFillColor(’#4545aa@0.3:1.5′)
@后的第一个数值代表透明度,第二个数值代表亮度