1、jpgraph官网地址
http://jpgraph.net/
2、下载jpgraph库
![]()

3、jpgraph的简单使用,以下是最基本的图形的生成代码
3.1、柱状图
<?php
// 引入类库
require_once '../lib/jpgraph/jpgraph.php';
require_once '../lib/jpgraph/jpgraph_bar.php';
//柱形图模拟数据
$data=array(0=>-21,1=>-3,2=>12,3=>19,4=>23,5=>29,6=>30,7=>22,8=>26,9=>18,10=>5,11=>-10);
//创建背景图
$graph=new Graph(400,300);
//设置刻度样式
$graph->SetScale("textlin");
//设置边界范围
$graph->img->SetMargin(30,30,80,30);
//设置标题
$graph->title->Set("BarPlot test");
//得到柱形图对象
$barPlot=new BarPlot($data);
//设置柱形图图例
$barPlot->SetLegend("beijing");
//显示柱形图代表数据的值
$barPlot->value->show();
//将柱形图加入到背景图
$graph->Add($barPlot);
//设置柱形图填充颜色
$barPlot->SetFillColor('yellow');
//设置边框颜色
$barPlot->SetColor('red');
//将柱形图输出到浏览器
$graph->Stroke();
//保存图片
$graph->Stroke('../pic/barPlot.png');

3.2、3D饼状图
<?php
require_once "../lib/jpgraph/jpgraph.php";
require_once "../lib/jpgraph/jpgraph_pie.php";
require_once "../lib/jpgraph/jpgraph_pie3d.php";
$data=array(0=>3.5,1=>4.6,2=>9.1,3=>21.9,4=>42.3,5=>90.7,6=>183.5,7=>127.5,8=>61.4,9=>33.5,10=>11.5,11=>4.4);
//创建画布
$graph=new pieGraph(500,500);
//设置图像边界范围
$graph->img->SetMargin(30,30,80,30);
//设置标题
$graph->title->Set("piePlot3d Test");
//得到3D饼图对象
$piePlot3d=new piePlot3d($data);
//设置图例
$piePlot3d->SetLegends(array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"));
//设置图例位置
$graph->legend->Pos(0.1,0.15,"left","center");
//将绘制好的3D饼图加入到画布中
$graph->Add($piePlot3d);
//输出
$graph->Stroke();
//保存图片
$graph->Stroke('../pic/piePlot3d.png');

3.3、饼状图
<?php
require_once "../lib/jpgraph/jpgraph.php";
require_once "../lib/jpgraph/jpgraph_pie.php";
//模拟数据
$data=array(0=>3.5,1=>4.6,2=>9.1,3=>21.9,4=>42.3,5=>90.7,6=>183.5,7=>127.5,8=>61.4,9=>33.5,10=>11.5,11=>4.4);
//创建画布
$graph=new PieGraph(800,500);
//设置图像边界范围
$graph->img->SetMargin(30,30,80,30);
//设置标题
$graph->title->Set("PiePlot Test");
//得到饼图对象
$piePlot=new PiePlot($data);
//设置图例
$piePlot->SetLegends(array(1,2,3,4,5,6,7,8,9,11,12));
//设置图例位置
$graph->legend->Pos(0.01,0.45,"left","top");
//添加到画布中
$graph->Add($piePlot);
//输出
$graph->Stroke();
//保存图片
$graph->Stroke('../pic/PiePlot.png');

3.4、折线图
<?php
//引入gpgraph库,创建XY坐标图
require_once '../lib/jpgraph/jpgraph.php';
require_once '../lib/jpgraph/jpgraph_line.php';
//1、创建画布
$graph = new Graph(600,800);
//2、设置横纵坐标刻度样式
/*
* lin直线
* text文本
* int整数
* log对数
* textint
*/
$aAxisType = 'textint';
$graph->SetScale($aAxisType);
//标题
$graph->title->Set('this is a test');
//数据
$data = array(
20, 30, 40, 14, 20, 60, 34, 62, 48, 120, 54
);
//创建LinePlot对象
$linePlot = new LinePlot($data);
//设置图例
$linePlot->SetLegend('tuli');
//将统计图添加到画布上
$graph->Add($linePlot);
//设置统计图颜色,一定放在添加画布之后
$linePlot->SetColor('#F59927');
//输出到浏览器
$graph->Stroke();
//保存图片
$graph->Stroke('../pic/xy.png');
//支持中文,需要配置
/*
* 1、在jpgraph_ttf.inc.php中,配置宏定义CHINESE_TTF_FONT
* 比如可以改为支持中文的字体,如宋体
* define('CHINESE_TTF_FONT','SIMYOU.TTF');
* 还需设置支持标题中文的字体
* $graph->title->SetFont(FF_CHINESE)
*
* 2、支持图例中文,修改jpgraph_legend.inc.php
* 把public $font_family=FF_DEFAULT,$font_style=FS_NORMAL,$font_size=8;这个里面的
* $font_family 改为支持中文
* $font_family=FF_CHINESE,$font_style=FS_NORMAL,$font_size=8;
*
*/

3.5、复合折线图
<?php
// 引入类库
require_once '../lib/jpgraph/jpgraph.php';
require_once '../lib/jpgraph/jpgraph_line.php';
$data = array(
0 => - 21,
1 => - 3,
2 => 12,
3 => 19,
4 => 23,
5 => 29,
6 => 30,
7 => 22,
8 => 26,
9 => 18,
10 => 5,
11 => - 10
); // 第一条数据
$data2y = array(
0 => 3,
1 => 12,
2 => 18,
3 => 30,
4 => 28,
5 => 33,
6 => 43,
7 => 39,
8 => 36,
9 => 29,
10 => 15,
11 => 10
); // 第二条数据
// 得到Graph对象
$graph = new Graph(400, 400);
// 设置X和Y轴样式及Y轴的最大值最小值
$graph->SetScale("textint", - 30, 50);
// 设置右侧Y轴样式及其最大值最小值
$graph->SetY2Scale("int", - 30, 50);
// 设置图像样式,加入阴影
$graph->SetShadow();
// 设置图像边界范围
$graph->img->setMargin(40, 30, 50, 70);
// 设置标题
$graph->title->Set("this is a test X-Y-Y");
// 得到曲线实例
$linePlot = new LinePlot($data);
// 得到第二条曲线
$linePlot2y = new LinePlot($data2y);
// 将曲线加入到图像中
$graph->Add($linePlot);
$graph->Add($linePlot2y);
// 设置三个坐标轴名称
$graph->xaxis->title->Set("Month");
$graph->yaxis->title->Set("beijing");
$graph->y2axis->title->Set("ShangHai");
// 设置两条曲线的颜色
$linePlot->SetColor('red');
$linePlot2y->SetColor('black');
// 设置两条曲线的图例
$linePlot->SetLegend("Beijing");
$linePlot2y->SetLegend("Shanghai");
// 设置图例样式
$graph->legend->setlayout(LEGEND_HOR);
$graph->legend->Pos(0.45, 0.9, "center", "bottom");
// 将图像输出到浏览器
$graph->Stroke();
//保存图片
$graph->Stroke('../pic/xy2.png');
