【作业】jpgraph的使用方法与例子
下载
在官方网站http://www.aditus.nu/jpgraph/下载jpgraph,其中1.X系列是用于PHP4的,2.X系列是用于PHP5的。
安装
将下载的得到的jpgraph压缩文件解压至相应的路径。
配置
首先需要注意的是:要想适用jpgraph,你的PHP必须开启了GD2扩展。
在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)
}
}
要支持中文需要用到simhei.ttf和simsun.ttc这两个字体,在使用中文的时候需要使用SetFont(FF_SIMSUN,FS_BOLD)设置字体。
如果你的文件编码为utf-8,修改方法如下:
代码:
方法一,在程序中修改
$title="流量图";
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);
方法二,修改源文件jpgraph_ttf.inc.php
在第99-106行,改成下面这样子
elseif( $aFF === FF_SIMSUN ) {
// Do Chinese conversion
return $aTxt;
}
jpgraph默认显示汉字时是把汉字编码认为gb2312,转化为utf-8以后再显示。
这样的话,如果你的文件编码是gb2312,SetFont方法的第一个参数为FF_SIMSUN即可。
如果你是utf-8编码你还需要先把汉字编码转化为gb2312,这样你的汉字才可以正常显示。
使用
可以参照jpgraph-2.3.4\src\Examples中的例子。下面是一些常用的:
$graph->title->Set(‘设置图表的标题’);
$graph->xaxis->title->Set("设置X轴的标题");
$graph->yaxis->title->Set("设置Y轴的标题");
//设置字体如果是中文,第一个参数一般设置为FF_SIMSUN
SetFont(FF_SIMSUN,FS_BOLD,14);
//如设置图表标题的字体
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);
//设置颜色
SetColor(’red’);
Example:
例1. php Jpgraph绘制简单的X-Y坐标图
SetScale("textlin");//设置刻度样式 $graph->img->SetMargin(30,30,80,30);//设置图表边界 $graph->title->Set("CDN Traffic Total");//设置图表标题 $graph->title->SetColor("blue"); $graph->title->SetMargin(20); // Create the linear plot $lineplot=new LinePlot($data);//创建新的LinePlot对象 $lineplot->SetLegend("Line(Mbits)");//设置图例文字 $lineplot->SetColor("red");//设置曲线的颜色 // Add the plot to the graph $graph->Add($lineplot);//在统计图上绘制曲线 // Display the graph $graph->Stroke();//输出图像 ?>
例2. php Jpgraph绘制复杂的X-Y坐标图
SetScale("textlin"); $graph->SetShadow();//设置图像的阴影样式 $graph->img->SetMargin(60,30,30,70);//设置图像边距 $graph->title->Set("CDN流量图");//设置图像标题 $graph->title->SetMargin(10); $lineplot1=new LinePlot($data1);//创建设置两条曲线对象 $lineplot2=new LinePlot($data2); $lineplot1->value->Show(); $lineplot1->value->SetColor("black"); $graph->Add($lineplot1);//将曲线放置到图像上 $graph->Add($lineplot2); $graph->xaxis->title->Set("月份");//设置坐标轴名称 $graph->yaxis->title->Set("流量(Gbits)"); $graph->xaxis->title->SetMargin(10); $graph->yaxis->title->SetMargin(10); $graph->title->SetFont(FF_SIMSUN,FS_BOLD);//设置字体 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth()); $lineplot1->SetColor("red");//设置颜色 $lineplot2->SetColor("blue"); $lineplot1->SetLegend("Max");//设置图例名称 $lineplot2->SetLegend("Min"); $graph->legend->SetLayout(LEGEND_HOR);//设置图例样式和位置 $graph->legend->Pos(0.5,0.96,"center","bottom"); $graph->Stroke();//输出图像 ?>
例3. php Jpgraph绘制柱形图
SetScale("textlin"); $graph->SetShadow();//设置阴影 $graph->img->SetMargin(40,30,40,50);//设置边距 $barplot = new BarPlot($data);//创建BarPlot对象 $barplot->SetFillColor(’blue’);//设置颜色 $barplot->value->Show();//设置显示数字 $graph->Add($barplot);//将柱形图添加到图像中 $graph->title->Set("CDN流量图");//设置标题和X-Y轴标题 $graph->title->SetColor("red"); $graph->title->SetMargin(10); $graph->xaxis->title->Set("月份"); $graph->xaxis->title->SetMargin(5); $graph->xaxis->SetTickLabels($ydata); $graph->yaxis->title->Set("流量(Mbits)"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD);//设置字体 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD); $graph->Stroke(); ?>
例4. php Jpgraph绘制饼图
SetShadow(); $graph->title->Set("CDN流量比例"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot($data); $pieplot->SetLegends($lable_data); $pieplot->SetCenter(0.4); $graph->Add($pieplot); $graph->Stroke(); ?>
例5. php Jpgraph绘制3D饼图
SetShadow(); $graph->title->Set("CDN流量比例"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot3D($data);//创建PiePlot3D对象 $pieplot->SetCenter(0.4);//设 置饼图中心的位置 $pieplot->SetLegends($gDateLocale->GetShortMonth());//设置图例 $graph->Add($pieplot); $graph->Stroke(); ?>
例6.
index.htmlCDN流量查询系统统计CDN流量查询系统统计
查询年份 | 20082009 |
起始月份 | 010203040506070809101112 |
终止月份 | 010203040506070809101112 |
统计图类别 | 线型图柱形图饼图3D饼图 |
result.php SetScale("textlin");//设置刻度样式 $graph->img->SetMargin(30,30,80,30);//设置图表边界 $graph->title->SetFont(FF_SIMSUN,FS_BOLD);//设置字体 $graph->title->Set("CDN流量查询");//设置图表标题 $lineplot=new LinePlot($data); $lineplot->SetLegend("Line"); $lineplot->SetColor("red"); $graph->Add($lineplot); break; case 2: $graph = new Graph(400,300); $graph->SetScale("textlin"); $graph->SetShadow(); $graph->img->SetMargin(40,30,20,40); $barplot = new BarPlot($data);//创建BarPlot对象 $barplot->SetFillColor(’blue’);//设置颜色 $barplot->value->Show();//设置显示数字 $graph->Add($barplot);//将柱形图添加到图像中 $graph->title->Set("CDN流量查询");//设置标题和X-Y轴标题 $graph->xaxis->title->Set("月份"); $graph->yaxis->title->Set("流量(Gbits)"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD);//设置字体 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); break; case 3: $graph = new PieGraph(400,300); $graph->SetShadow(); $graph->title->Set("CDN流量查询"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot($data); $pieplot->SetLegends($gDateLocale->GetShortMonth());//设置图例 $graph->Add($pieplot); break; case 4: $graph = new PieGraph(400,300); $graph->SetShadow(); $graph->title->Set("CDN流量查询"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot3D($data);//创建PiePlot3D对象 $pieplot->SetCenter(0.4);//设置饼图中心的位置 $pieplot->SetLegends($gDateLocale->GetShortMonth());//设置图例 $graph->Add($pieplot); break; default: echo "graph参数错误"; exit; } $graph->Stroke(); ?>
本文出自 “坏男孩” 博客,请务必保留此出处http://5ydycm.blog.51cto.com/115934/177498 https://blog.csdn.net/yanlintao1/article/details/26056685
__________以下来自文库
jpgraph_bar.php"); include ("../jpgraph_line.php"); 设置显示的数据数组; 调用类库 //设置图像的大小
$graph = new Graph(400,200,"auto"); $graph->SetScale("textlin"); //设置图形的边距
$graph->img->SetMargin(40,180,40,40);
//设置图形的背景图片,填充方式有:BGIMG_FILLPLOT, BGIMG_FILLFRAME, BGIMG_COPY
$graph->SetBackgroundImage("abc.jpg",BGIMG_FILLPLOT); $graph->img->SetAngle(45); //设置图形在图像中的角度 //设置背景图片的对比度,must be between -1 <= x <= 1, (0,0)=original image
$graph->AdjBackgroundImage(0,0); //设置投影;
//$graph->SetShadow();
设置标题
$graph->title->Set("test image"); //设置标题字体样式
$graph->title->SetFont(FF_FONT1,FS_BOLD); //设置标题的边距
$graph->title->SetMargin(3); //设置图列的位置
$graph->legend->Pos(0.05,0.5,"right","center"); //设置图列的投影,颜色
$graph->legend->SetShadow('darkgray@0.1'); $graph->legend->SetFillColor('lightblue@0.3'); //设置x轴的标记
$graph->xaxis->SetTickLabels($label_x); //设置X轴的显示值的角度; $graph->xaxis->SetLabelAngle(30); //设置x轴标题和字体颜色
$graph->xaxis->title->Set('Year 2006');
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD); $graph->xaxis->title->SetColor('white'); //设置x轴的字体和颜色
$graph->xaxis->SetFont(FF_FONT1,FS_BOLD); $graph->xaxis->SetColor('yellow');
设置y轴的字体和颜色
$graph->yaxis->SetFont(FF_FONT1,FS_BOLD); $graph->yaxis->SetColor('yellow'); //设置是否显示格子。默认为显示; //$graph->ygrid->Show(false);
//设置格子的颜色和粗细。值越小,格子越粗。 $graph->ygrid->SetColor('yellow@0.5'); //设置y轴更优美一些
$graph->yaxis->scale->SetGrace(20);
学习php jpgraph教程 2 2007-08-15 21:59 //设置图列的数据
$bplot1 = new BarPlot($datay1); $bplot2 = new BarPlot($datay2); //设置图列的填充颜色
$bplot1->SetFillColor('orange@0.4'); $bplot2->SetFillColor('brown@0.4'); 设置值的格式
$bplot1->value->SetFormat('%d'); //设置图列标签
$bplot1->SetLegend('Label 1');