一、繪制線性圖表
<?php
//引入類庫
require_once "/data/webroot/resource/php/src/jpgraph.php";
require_once "/data/webroot/resource/php/src/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->SetLegend("Beijing");
$linePlot2y->SetLegend("Shanghai");
//設定圖例樣式
$graph->legend->setlayout(LEGEND_HOR);
$graph->legend->Pos(0.45,0.9,"center","bottom");
//将圖像輸出到浏覽器
$graph->Stroke();
二、繪制柱狀圖表
<?php
//引入類庫
require_once "/data/webroot/resource/php/src/jpgraph.php";
require_once "/data/webroot/resource/php/src/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);
//設定柱形圖填充顔色
//設定邊框顔色
//将柱形圖輸出到浏覽器
$graph->Stroke();
三、繪制餅圖
<?php
require_once "/data/webroot/resource/php/src/jpgraph.php";
require_once "/data/webroot/resource/php/src/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);
//設定圖例
//設定圖例位置
$graph->legend->Pos(0.01,0.45,"left","top");
//添加到畫布中
$graph->Add($piePlot);
//輸出
$graph->Stroke();
<?php
require_once "/data/webroot/resource/php/src/jpgraph.php";
require_once "/data/webroot/resource/php/src/jpgraph_pie.php";
require_once "/data/webroot/resource/php/src/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->Stroke();