本文实例讲述了php使用jpgraph绘制折线图操作。分享给大家供大家参考,具体如下:

下载jpgraph类库,使用的是src目录下的类文件。

require_once './src/jpgraph.php';
require_once './src/jpgraph_line.php';
//创建统计图对象,宽,高
$graph = new graph(1993, 766);
//设置背景,注意要把主题给换掉
$graph->setbackgroundimage('./bg.jpg',2);
//设置背景图片使用百分比1-100
$graph->setbackgroundimagemix(100);
//设置边距,空余四角边距(左右上下)
$graph->img->setmargin(0,0,0,0);
//设置x和y的刻度类型,设置比例 (x 文本比例、y 线比例)
//lin直线、text文本、int整数、log对数
$graph->setscale('linlin',50,100);//y轴的最小值、最大值
//设置统计图标题
$graph->title->set(iconv('utf-8', 'gb2312//ignore', '折线图'));
//隐藏x轴上的刻度线
$graph->xaxis->hideticks(true,true);
//隐藏x轴线
$graph->xaxis->hideline(true);
//隐藏x轴线的刻度标注数字
$graph->xaxis->hidelabels(true);
//隐藏x轴上的刻度线
$graph->yaxis->hideticks(true,true);
//折线图数据
$data1 = array(89, 78, 99, 65, 92, 85, 85, 55, 64, 79, 85);
//建立lineplot对象
$lineplot = new lineplot($data1);
// //将统计图添加到画布上
$graph->add($lineplot);
//设置折线的线条颜色
$lineplot->setcolor('red');
//两个点之间的连线样式,true表示台阶折线型,false表示直线连接型
$lineplot->setstepstyle(false);
// 设置【折线与x轴之间的区域】是否填充颜色
$lineplot->setfilled(false);
//设置【折线与x轴之间的区域】的【颜色渐变样式】
//setfillgradient($afromcolor,$atocolor,$anumcolors=100,$afilled=true) 
// $lineplot->setfillgradient('red','silver',100,false);
//设置【折线与x轴之间的区域】的【颜色】
//setfillcolor($acolor,$afilled=true)
// $lineplot->setfillcolor('red',true);
// addarea($amin=0,$amax=0,$afilled=lp_area_not_filled,$acolor="gray9",$aborder=lp_area_border)
// $lineplot->addarea(0,$amax=500,false,"gray9",true);
//如果要绘制第二条线
//$data2 = array(68, 70, 69, 80, 50, 60, 75, 65, 75, 65, 80, 89);
//$lineplot2=new lineplot($data2); 
//$graph->add($lineplot2);
//... 第二条线的其他设置
//输出画布
$graph->stroke();
//保存成图片
//$graph->stroke('./test.png');

运行效果:

附:完整实例代码点击此处。