Jason Chen 28 , Sep, 2006
作者博客http://okchina.cublog.cn/
欢迎转载,但务必在转载时注明出处.
1. Jpgraph是一个能与PHP 4.1以上版本完美结合的图形方面的库,我使用的版本是1.13,最新的版本请到这里下载:http://www.aditus.nu/jpgraph/
2. 它支持很多图形的生成 ,X-Y Plot ,Error plots,Scatter plots Box and Stock charts,Radar
plots ,Pie plots 等等. 不能一一列出,只列出了些常见的.
 
3. 安装
只需要讲graph目前下的所有文件复制到你的库中,然后在你的Application中根
据路径来调用相关的文件就行了.
4. 使用举例:
4.1 首先要在设定一些相关的配置:
需要在/graph/jpgraph.php文件中的这段中把相应的路径改成你当前的路径:
if (!defined('TTF_DIR')) {
if (strstr( PHP_OS, 'WIN') ) {
if( empty($_SERVER['SystemRoot']) ) {
die('JpGraph Error: No path specified for TTF_DIR. Please specify a path for that
DEFINE in jpgraph.php');
}
else {
DEFINE('TTF_DIR', $_SERVER['SystemRoot'] . '/fonts/');
}
} else {
DEFINE('TTF_DIR','/usr/local/apache/htdocs/fonts/');
}
}
4.2 应用程序关键部分:
< ?php
///////////////////////////////////////////////////// GRAPH GENERATING ////////////////////////////////////////////////
include_once($_SERVER["DOCUMENT_ROOT"]."/inc_class/graph/jpgraph.php");
include_once($_SERVER["DOCUMENT_ROOT"]."/inc_class/graph/jpgraph_line.php");
// **************************** Trend Chart **************************
//将要显示的点,组成数组传输过去
$subtitle = unserialize(stripslashes($gpcc_subtitle));
$datax = unserialize(stripslashes($inputdatax));
$datay= unserialize(stripslashes($inputdatay));
$num= unserialize(stripslashes($list_num));
$show_type= unserialize(stripslashes($type));
$basic_value = 98;
$basic_line = array();
$count_plot = count($datax);
for($i=0;$i<$num ;$i++ ){
$basic_line[]=$basic_value;
}
################# Create Basic Chart ###########
// Create the graph. These two calls are always required
$graph = new Graph(665,450,"auto"); //生成图片,参数:宽度,长度,”自动”
$graph->SetScale("textlin"); //设定刻度
$graph->SetBackgroundGradient('#EEF2FA','#CED9F0',GRAD_HOR,BGRAD_MARGIN); //设
定背景斜线
$graph->img->SetAntiAliasing();
$graph->legend->SetLayout(LEGEND_HOR); //设定布局
$graph->legend->Pos(0.5,0.95,"center","bottom"); //设定分布位置
$graph->title->Set("GPCC Trend Chart");
$graph ->title->SetFont( FF_FONT2, FS_BOLD);
$graph->title->SetColor("#cc0033");
$graph->subtitle->Set($subtitle); //设定标题
$graph ->subtitle->SetFont( FF_FONT1, FS_BOLD);
$graph->subtitle->SetColor("#3300FF");
##############################################3
$lineplot_b=new LinePlot($basic_line);
$lineplot_b->SetColor('#cc0033');
$lineplot_b->SetWeight(1); //设定宽度
$lineplot_b->SetBarCenter();
##############################################
// Create the linear plot
//´创建点的基本信息
$lineplot=new LinePlot($datay); //按点数创造点
$lineplot->SetColor('#3300ff');
$lineplot->SetBarCenter();
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("#339900");
$lineplot->mark->SetWidth(2);
$lineplot->value->show(); //显示每个点的值
$lineplot->value->SetColor('#339900');
$lineplot->value->SetFont(FF_FONT1,FS_BOLD);
$lineplot->SetCSIMTargets($targ,$x_alt);
$lineplot->value->SetFormat('%0.2f'); //设置点的值的格式
//$lineplot->SetLegend( "GPCC Trend Plot"); //设置提示
//$lineplot->SetWeight(2); //设置宽度
$lineplot->SetCenter();
#############################################
//创建X轴的基本信息
$graph->xaxis->title->Set("By Date"); //设置X轴的标题
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetColor("#3300ff");
//$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->SetColor("#993300");
$graph->xaxis->SetTickLabels($datax);
$graph->xgrid->Show();
##############################################
//创建Y轴的基本信息
$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->SetColor("#339900");
$graph->ygrid->SetFill(true,'#EFEFEF@0.5','#FFFFCC@0.5'); //设置每隔行填充一个颜色
$graph->ygrid->Show();
##############################################33
//生成图形
$graph->Add($lineplot);
switch ($show_type) {
case "all":
$graph->Add($lineplot_b);
break;
}
$im=$graph->Stroke(); //成生图形
?>
4.3 生成:
4.4其它Picture:
==============================
<完>
Jason Chen 28, Sep, 2006 14:00 最后更新
.我的声明:
1,欢迎转载,但在务必在转载时注明出处.
2,作者贴出此贴仅供大家学习参考,任何人因按本文操作而造成任何损失均与作者无
关。
3,作者博客:http://okchina.cublog.cn/
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()