php報表的使用:
1、到官網(http://jpgraph.net/)下載,建議下載jpgraph-3.0.7.tar.gz版本
2、解壓后有兩個文件夾
docportal:使用手冊
src:報表核心文件
3、配置核心文件(對於src文件夾內容的操作)
1)創建一個jpgraph文件夾
2)將和Examples同級目錄下的其它內容放在jpgraph文件夾下
3)將jpgraph放在Examples里
4)查找效果圖。Examples里的每個php文件對應一個報表,可以在jpgraph11-3.0.7\docportal\chunkhtml\images查看報表的效果圖,效果圖的文件名和對應php文件的文件名相同
(如果你打開Examples文件夾里的某一個文件,你會發現有這樣一句代碼:require_once ('jpgraph/jpgraph.php')。但你卻在該文件夾里沒有找到jpgraph文件夾。其實jpgraph文件夾里的內容都放在和Examples同一目錄下。因此你只要在Examples里創建一個jpgraph文件夾,並把和Examples同級目錄的其它內容放到該文件夾里即可。)
4、引用方式:
1)直接訪問php文件,如報表的效果圖文件名為example27.1.png,則直接訪問example27.1.php即可
2)作為圖片引用,如<img alt="報表" src="example27.1.php" />
5、中文亂碼處理
當你的文件為utf8時,會出現中文亂碼,這時可使用iconv()將中文字符串轉成gb2312。
例如:
$graph->title->Set(iconv("utf-8", "gb2312//ignore", "處理情況統計"));
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
具體詳見:http://blog.csdn.net/ms_x0828/article/details/5555864
6、案例分析
1)柱形圖1
代碼:

1 <?php // content="text/plain; charset=utf-8" 2 require_once ('jpgraph/jpgraph.php'); 3 require_once ('jpgraph/jpgraph_bar.php'); 4 5 6 $datay1=array(13,8,19); 7 $datay2=array(3,0,0);//多增加的數據 8 9 // Create the graph. 10 $graph = new Graph(650,450);//畫布大小 11 //$graph->SetScale('textlin'); 12 $graph->SetScale('textlin',-10,25);//設置y軸范圍為5-75 13 $graph->yaxis->scale->ticks->Set(5);//設置y軸刻度為10 14 //$graph->xaxis->scale->ticks->Set(5); 15 $graph->xaxis->title->Set("X軸"); 16 $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,14); 17 $graph->yaxis->title->Set("Y軸"); 18 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD,14); 19 20 $graph->SetMarginColor('white');//設置邊框背景顏色 21 $graph->SetMargin(40,40,10,10);//設置圖在邊框中的位置 22 23 // Setup title 24 $graph->title->Set('Acc bar with gradient呵呵');//設置標題,默認的標題不支持中文 25 $graph->title->SetFont(FF_SIMSUN,FS_BOLD,14); //設置字體類型和大小。第一個參數決定是否能顯示中文。參數值可參考jpgraph_ttf.inc.php文件 26 27 // Create the first bar 28 $bplot = new BarPlot($datay1); 29 $bplot->SetFillGradient('AntiqueWhite2','AntiqueWhite4:0.8',GRAD_VERT);//設置柱體顏色 30 //$bplot->SetFillgradient('orange','darkred',GRAD_VER); //設置柱體顏色 31 $bplot->SetColor('orange');//柱體邊界的顏色 32 33 // Create the second bar 34 $bplot2 = new BarPlot($datay2); 35 $bplot2->SetFillGradient('olivedrab1','olivedrab4',GRAD_VERT);//柱體中增加部分的顏色 36 $bplot2->SetColor('red');//柱體中增加部分的邊框顏色 37 38 39 // And join them in an accumulated bar 40 $accbplot = new AccBarPlot(array($bplot,$bplot2)); 41 $graph->Add($accbplot); 42 43 $graph->Stroke(); 44 ?>
2)柱形圖2
代碼:

1 <?php // content="text/plain; charset=utf-8" 2 require_once ('jpgraph/jpgraph.php'); 3 require_once ('jpgraph/jpgraph_bar.php'); 4 5 // Some data 6 $datay=array(7,19,11,4,20); 7 8 // Create the graph and setup the basic parameters 9 $graph = new Graph(600,500,'auto'); 10 $graph->img->SetMargin(40,30,40,50); 11 $graph->SetScale("textint"); 12 $graph->SetFrame(true,'blue',1); //邊框邊界的顏色 13 $graph->SetColor('lightblue');//柱體圖背景顏色 14 $graph->SetMarginColor('lightblue');//邊框背景顏色 15 16 // Setup X-axis labels 17 $a = $gDateLocale->GetShortMonth();//x軸用月份顯示 18 $graph->xaxis->SetTickLabels($a); 19 $graph->xaxis->SetFont(FF_FONT1); 20 $graph->xaxis->SetColor('darkblue','black');//x軸的顏色 21 22 // Setup "hidden" y-axis by given it the same color 23 // as the background (this could also be done by setting the weight 24 // to zero) 25 $graph->yaxis->SetColor('lightblue','darkblue');//y軸顏色 26 $graph->ygrid->SetColor('white');//y軸分割線的顏色 27 28 // Setup graph title ands fonts 29 $graph->title->Set('Using grace = 0測試'); 30 $graph->title->SetFont(FF_SIMSUN,FS_BOLD);//FF_SIMSUN顯示中文 31 $graph->xaxis->SetTitle('Year 2002','center'); 32 $graph->xaxis->SetTitleMargin(10);//x軸標題顯示的位置 33 $graph->xaxis->title->SetFont(FF_FONT2,FS_BOLD); 34 35 // Add some grace to the top so that the scale doesn't 36 // end exactly at the max value. 37 $graph->yaxis->scale->SetGrace(0);//修改y軸顯示的最大的值 38 39 40 // Create a bar pot 41 $bplot = new BarPlot($datay); 42 $bplot->SetFillColor('darkblue');//柱體填充的顏色 43 $bplot->SetColor('darkblue');//柱體邊框顏色 44 $bplot->SetWidth(0.5);//設置柱體的寬度,取值:0~1 45 $bplot->SetShadow('darkgray');//柱體陰影顏色 46 47 48 /*顯示和設置柱體上數字的樣式*/ 49 $bplot->value->Show(); 50 $bplot->value->SetFont(FF_ARIAL,FS_NORMAL,9);//柱體上數字的字體和大小 51 $bplot->value->SetFormat('$%d');//柱體上數字的格式 52 $bplot->value->SetColor('darkred');//柱體上數字的顏色 53 $bplot->value->SetAngle(45);//柱體上數字的傾斜度 54 $graph->Add($bplot); 55 56 // Finally stroke the graph 57 $graph->Stroke(); 58 ?>
3)折線圖
代碼:

<?php // content="text/plain; charset=utf-8" require_once ('jpgraph/jpgraph.php'); require_once ('jpgraph/jpgraph_line.php'); // Some (random) data $ydata = array(11.1,3,8,12,5,1,9,13,5,7); // Size of the overall graph $width=650; $height=550; // Create the graph and set a scale. // These two calls are always required $graph = new Graph($width,$height); $graph->SetScale('intlin'); $graph->SetShadow(); // Setup margin and titles $graph->SetMargin(40,20,20,40); $graph->title->Set('Calls per operator');//標題 $graph->subtitle->Set('(March 12, 2008)');//副標題 $graph->xaxis->title->Set('Operator x軸'); $graph->yaxis->title->Set('# of calls y軸'); $graph->yaxis->title->SetFont( FF_SIMSUN , FS_BOLD ); $graph->xaxis->title->SetFont( FF_SIMSUN , FS_BOLD ); $graph->yaxis->SetColor('blue'); // 折線 $lineplot=new LinePlot($ydata); $lineplot->SetColor( 'blue' ); $lineplot->SetWeight( 2 ); // 折線圖顏色 $lineplot->mark->SetType(MARK_UTRIANGLE); /*折線處的標記*/ $lineplot->mark->SetColor('blue'); $lineplot->mark->SetFillColor('red'); $lineplot->value->Show();//在折線處顯示數據 // Add the plot to the graph $graph->Add($lineplot); // Display the graph $graph->Stroke(); ?>
4)多條折線圖
<?php // content="text/plain; charset=utf-8" require_once ('jpgraph/jpgraph.php'); require_once ('jpgraph/jpgraph_line.php'); // $dateUtils = new DateScaleUtils(); // Some (random) data $ydata = array(11,3,8,12,5,1,9,13,5,7); $y2data = array(1,5,1,22,5,9,1,3,15,17); //x軸顯示日期 $xdata=array("2015-01-01","2015-01-02","2015-01-03","2015-01-04","2015-01-05","2015-01-06","2015-01-07","2015-01-08","2015-01-09","2015-01-10"); //報表長寬 $width=800; $height=400; // Create the graph and set a scale. // These two calls are always required $graph = new Graph($width,$height); $graph->SetScale('intlin'); // $graph->SetShadow(); $graph->SetFrame(false); // Setup margin and titles // $graph->SetMargin(40,20,10,40); $graph->img->SetMargin(60,140,70,80); //折線圖在框中的位置 $graph->title->Set(iconv("utf-8","gb2312","報警情況統計表")); $graph->title->SetFont(FF_SIMSUN,FS_BOLD,20); // $graph->subtitle->Set('(March 12, 2008)'); $graph->xaxis->title->Set(iconv("utf-8","gb2312","日期")); $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,15); $graph->yaxis->title->Set(iconv("utf-8","gb2312","每日消息數")); $graph->yaxis->title->SetFont( FF_SIMSUN , FS_BOLD,15 ); // $graph->yaxis->SetColor('blue');//y軸顏色 //*****************x軸數據設置******************** //x軸顯示日期 // $xdata=array("2015-01-01","2015-01-02","2015-01-03","2015-01-04","2015-01-05","2015-01-06","2015-01-07","2015-01-08","2015-01-09","2015-01-10"); // $xdata = $gDateLocale->GetShortMonth();//x軸用月份顯示 $graph->xaxis->SetTickLabels($xdata); // $graph->xaxis->SetLabelMargin(15);//$xdata數據離x軸的距離 $graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,10); $graph->xaxis->SetLabelAngle(45); // x軸數據傾斜的角度 $graph->xaxis->SetTitleMargin(45);//x軸標題顯示的位置 /********************************設置折線**************************************/ $lineplot=new LinePlot($ydata); $lineplot2=new LinePlot($y2data); $lineplot->SetColor( 'blue' ); $lineplot2->SetColor( 'green' ); $lineplot->SetWeight( 2 ); // Two pixel wide $lineplot2->SetWeight( 2 ); // Two pixel wide //設置圖例 $lineplot->SetLegend("Task"); $lineplot2->SetLegend(iconv("utf-8","gb2312","其它")); $graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //設置圖例字體 // $graph->legend->SetFont(FF_FONT2,FS_NORMAL); $graph->legend->SetLineSpacing(10); //圖例中的間距 $graph->legend->Pos(0.01,0.1,"right","top"); $graph->legend->SetLineWeight(2);//圖例這種線條寬度 $graph->legend->SetMarkAbsSize(20);//圖例大小 //折線圖標設置 // MARK_IMG_BEVEL,MARK_IMG_DIAMOND,MARK_IMG_STAR,MARK_IMAGE_SQUARE,MARK_IMAGE_LBALL,MARK_IMAGE_MBALL,MARK_IMG_LPUSHPIN,MARK_IMG_PUSHPIN,MARK_IMG_SPUSHPIN $lineplot->mark->SetType(MARK_IMG_STAR,'yellow',0.9); $lineplot2->mark->SetType(MARK_IMG_DIAMOND,'red',0.5); // Add the plot to the graph $graph->Add($lineplot); $graph->Add($lineplot2); // Display the graph $graph->Stroke(); ?>
5)餅狀圖
<?php // content="text/plain; charset=utf-8" require_once ('jpgraph/jpgraph.php'); require_once ('jpgraph/jpgraph_pie.php'); require_once ('jpgraph/jpgraph_pie3d.php'); $data = array(3,5,10); $graph = new PieGraph(600,400); // $graph->SetShadow(); //設置餅狀圖陰影 $graph->SetFrame(false);//設置餅狀圖邊框 $graph->title->Set(iconv("utf-8","gb2312","處理情況統計表")); $graph->title->SetFont(FF_SIMSUN,FS_BOLD,20); $p1 = new PiePlot3D($data); $p1->SetAngle(30); //餅狀圖的傾斜程度 $p1->SetSize(0.5); //餅狀圖的大小 $p1->SetCenter(0.5); //餅狀圖的位置 $legends = array(iconv("utf-8","gb2312","未接收"),iconv("utf-8","gb2312","已接收未解決"),iconv("utf-8","gb2312","已解決")); //圖例 $graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //設置圖例字體 $p1->SetLegends($legends); // $p1->SetLabelPos(0.5); //餅狀圖中數據的位置 $graph->Add($p1); $graph->Stroke();