一、jpgraph介紹
JPGraph是一個基於PHP的開源圖形庫,可以生成各種類型的二維圖形,比如條形圖、折線圖、餅圖等等。它使用GD和TrueType字體,能夠輕鬆地呈現優美、可讀性強和高質量圖表,並且可以輕鬆導出為PNG、JPEG、GIF和PDF格式。
JPGraph使用非常簡單,具有高可擴展性。其主要目的是幫助PHP開發人員創建各種動態圖形,尤其是Web應用程序以及系統程序。如果您有興趣在您的PHP應用程序中使用圖形呈現數據,那麼JPGraph是一個必不可少的圖形庫。
下面,我們就逐步深入學習JPGraph – PHP圖形庫的使用。
二、基礎入門
首先,我們需要安裝JPGraph。請先下載最新版本。在您的PHP項目中,可以將JPGraph直接引入您的代碼中。
以下是一個簡單的PHP文件示例:
<?php
require_once ('../src/jpgraph.php');
require_once ('../src/jpgraph_line.php');
$data = array(10,20,30,40,50,60);
$graph = new Graph(300,200);
$graph->SetScale('textlin');
$lineplot = new LinePlot($data);
$graph->Add($lineplot);
$graph->Stroke();
?>
這個示例將生成一條簡單折線圖。首先我們要引入jpgraph.php文件和我們要使用的類文件,這裡是jpgraph_line.php。$data是用於繪製圖表的數據,Graph是我們的主要對象,用於實例化和設置圖形。在這裡我們創造了一個具有遞增值的數組,然後與LinePlot類一起添加到Graph對象上。畫圖是通過使用Stroke方法完成的。
三、常用圖表類型
1. 條形圖
下面是一個簡單的條形圖示例:
<?php
require_once ('../src/jpgraph.php');
require_once ('../src/jpgraph_bar.php');
$data = array(20,30,25,45);
$graph = new Graph(350,250);
$graph->SetScale('textlin');
$graph->title->Set('Bar Plot');
$graph->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->Set('Y-axis');
$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->xaxis->title->Set('X-axis');
$plot = new BarPlot($data);
$plot->SetFillColor('orange');
$plot->value->Show();
$graph->Add($plot);
$graph->Stroke();
?>
在這個示例中,我們設置了一些圖表屬性,如標題、字體和軸的標題。使用Orange顏色來填充條形圖,最後添加到Graph對象上。最後,我們畫出圖形。
2. 折線圖
下面是一個簡單的折線圖示例:
<?php
require_once ('../src/jpgraph.php');
require_once ('../src/jpgraph_line.php');
$data = array(10,20,30,40,50,60);
$graph = new Graph(400,300);
$graph->SetScale('intint');
$graph->yscale->SetAutoMin(0);
$graph->title->Set('Line Plot');
$graph->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->Set('Y-axis');
$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->xaxis->title->Set('X-axis');
$lineplot = new LinePlot($data);
$lineplot->value->SetColor('red');
$lineplot->value->SetFormat('%d');
$lineplot->value->Show();
$graph->Add($lineplot);
$graph->Stroke();
?>
在這個示例中,我們設置了一些圖表屬性,如標題、字體和軸的標題。使用紅色來顯示線條和數值,最後添加到Graph對象上。最後,我們畫出圖形。
3. 餅圖
下面是一個簡單的餅圖示例:
<?php
require_once ('../src/jpgraph.php');
require_once ('../src/jpgraph_pie.php');
require_once ('../src/jpgraph_pie3d.php');
$data = array(30,60,90,125,175);
$graph = new PieGraph(350,250);
$graph->SetShadow();
$graph->title->Set('Pie Plot');
$graph->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->legend->SetFont(FF_ARIAL,FS_NORMAL);
$graph->legend->Pos(0.5,0.7);
$graph->legend->SetColumns(1);
$p1 = new PiePlot($data);
$p1->SetLegends(array('Jan','Feb','Mar','Apr','May'));
$p1->SetSliceColors(array('red','orange','yellow','green','blue'));
$p1->SetCenter(0.5,0.45);
$p1->value->SetFont(FF_ARIAL,FS_NORMAL);
$p1->value->SetColor('black');
$graph->Add($p1);
$graph->Stroke();
?>
在這個示例中,我們設置了一些圖表屬性,如標題、字體和圖例。使用多種顏色來顯示餅圖,最後添加到PieGraph對象上。最後,我們畫出圖形。
四、高級應用
1. 多個數據集合併到同一個圖形中
下面是一個簡單的示例:
<?php
require_once ('../src/jpgraph.php');
require_once ('../src/jpgraph_line.php');
$data = array(
array(10,20,30,40,50,60),
array(5,15,25,35,45,55),
array(30,35,40,45,50,55)
);
$graph = new Graph(400,300);
$graph->SetScale('textlin');
$graph->title->Set('Multiple Line Plot');
$graph->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->Set('Y-axis');
$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->xaxis->title->Set('X-axis');
$graph->yaxis->SetTitleMargin(45);
for($i=0; $i < count($data); $i++){
$lineplot = new LinePlot($data[$i]);
$lineplot->SetColor('#'.dechex(rand(0,255)).dechex(rand(0,255)).dechex(rand(0,255)));
$graph->Add($lineplot);
}
$graph->Stroke();
?>
在這個示例中,我們在同一個圖形中集中多個線路。我們可以使用隨機顏色來代表它們。
2. 生成圖表到不同文件格式
下面是一個簡單的示例:
<?php
require_once ('../src/jpgraph.php');
require_once ('../src/jpgraph_pie.php');
$data = array(30,60,90,125,175);
$graph = new PieGraph(350,250);
$graph->SetShadow();
$graph->legend->Pos(0.5,0.7);
$graph->legend->SetColumns(1);
$p1 = new PiePlot($data);
$p1->SetLegends(array('Jan','Feb','Mar','Apr','May'));
$p1->SetSliceColors(array('red','orange','yellow','green','blue'));
$p1->SetCenter(0.5,0.45);
$p1->value->SetFont(FF_ARIAL,FS_NORMAL);
$p1->value->SetColor('black');
$graph->Add($p1);
$name = "pie";
$graph->Stroke($name.".png");
$graph->Stroke($name.".jpeg");
$graph->Stroke($name.".gif");
$graph->Stroke($name.".pdf");
?>
在這個示例中,我們將生成的餅圖導出到不同文件格式,包括PNG、JPEG、GIF和PDF。
3. 動態數據繪圖
下面是一個簡單的示例:
HTML文件:
<?php
require_once ('../src/jpgraph.php');
require_once ('../src/jpgraph_bar.php');
$data = array();
for($i=0; $i<10; $i++){
$data[] = rand(10,100);
}
$graph = new Graph(600,400);
$graph->SetScale('textlin');
$graph->yscale->SetAutoMin(0);
$graph->title->Set('Dynamic Bar Plot');
$graph->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->yaxis->title->Set('Y-axis');
$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->xaxis->title->Set('X-axis');
$graph->yaxis->SetTitleMargin(45);
$plot = new BarPlot($data);
$plot->SetFillColor('orange');
$plot->value->Show();
$graph->Add($plot);
$graph->Stroke();
?>
JavaScript文件:
<script type="text/javascript">
setInterval(function(){
var img = document.getElementById('myGraph');
img.src = 'graph.php?x='+Math.random();
}, 5000);
</script>
在這個示例中,我們創建了一個隨機數據源來繪製動態圖表。在JavaScript文件中,我們使用setInterval方法在每5秒鐘生成一個新的隨機值,然後更新圖像元素。我們可以將graph.php替換為我們的動態數據源。
五、總結
JPGraph是生成動態圖形和圖表的強大工具。它可以幫助開發人員創建各種類型的圖形,如條形圖、折線圖、餅圖等,而且非常容易使用。在本文中,我們詳細介紹了JPGraph的基本概念,包括如何繪製不同類型的圖表以及如何將它們導出到不同的文件格式中。我們還介紹了一些高級應用,如多個數據集合併到同一個圖形中、動態數據繪圖等。現在,您可以開始使用JPGraph來創建自己的令人驚嘆的圖形啦!
原創文章,作者:NGXAL,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/360314.html
微信掃一掃
支付寶掃一掃