php數(shù)據(jù)統(tǒng)計(jì)圖 php訪問(wèn)統(tǒng)計(jì)

php 產(chǎn)生數(shù)據(jù)統(tǒng)計(jì)圖

首先,PHP生成數(shù)據(jù)統(tǒng)計(jì)圖可以用jpgraph這個(gè)類庫(kù),很方面也很容易使用,官方網(wǎng)站是:

我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、哈巴河ssl等。為超過(guò)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的哈巴河網(wǎng)站制作公司

jpgraph生成的的統(tǒng)計(jì)圖其實(shí)就是圖片格式,當(dāng)然也可以放到Drupal中,直接在模板中引入這個(gè)圖片鏈接或者在編輯器中插入圖片都可以。Drupal的模板機(jī)制可以參考這篇文章:

希望可以幫助到你。

PHP中圖像處理怎么寫一個(gè)折線統(tǒng)計(jì)圖

在PHP中,有一些簡(jiǎn)單的圖像函數(shù)是可以直接使用的,但大多數(shù)要處理的圖像,都需要在編譯PHP時(shí)加上GD庫(kù)。除了安裝GD庫(kù)之外,在PHP中還可能需要其他的庫(kù),這可以根據(jù)需要支持哪些圖像格式而定。GD庫(kù)可以網(wǎng)上免費(fèi)下載,不同的GD版本支持的圖像格式不完全一樣,最新的GD庫(kù)版本支持GIF、JPEG、PNG、WBMP、XBM等格式的圖像文件,此外還支持一些如FreeType、Type 1等字體庫(kù)。通過(guò)GD庫(kù)中的函數(shù)可以完成各種點(diǎn)、線、幾何圖形、文本及顏色的操作和處理,也可以創(chuàng)建或讀取多種格式的圖像文件。

在PHP中,通過(guò)GD庫(kù)處理圖像的操作,都是先在內(nèi)存中處理,操作完成以后再以文件流的方式,輸出到瀏覽器或保存在服務(wù)器的磁盤中。創(chuàng)建一個(gè)圖像應(yīng)該完成如下所示的4個(gè)基本步驟。

(1)創(chuàng)建畫(huà)布:所有的繪圖設(shè)計(jì)都需要在一個(gè)背景圖片上完成,而畫(huà)布實(shí)際上就是在內(nèi)存中開(kāi)辟的一塊臨時(shí)區(qū)域,用于存儲(chǔ)圖像的信息。以后的圖像操作都將基于這個(gè)背景畫(huà)布,該畫(huà)布的管理就類似于我們?cè)诋?huà)畫(huà)時(shí)使用的畫(huà)布。

(2)繪制圖像:畫(huà)布創(chuàng)建完成以后,就可以通過(guò)這個(gè)畫(huà)布資源,使用各種畫(huà)像函數(shù)設(shè)置圖像的顏色、填充畫(huà)布、畫(huà)點(diǎn)、線段、各種幾何圖形,以及向圖像中添加文本等。

(3)輸出圖像:完成整個(gè)圖像的繪制以后,需要將圖像以某種格式保存到服務(wù)器指定的文件中,或?qū)D像直接輸出到瀏覽器上顯示給用戶。但在圖像輸出之前,一定要使用header()函數(shù)發(fā)送Content-type通知瀏覽器,這次發(fā)送的是圖片不是文本。

(4)釋放資源:圖像被輸出以后,畫(huà)布中的內(nèi)容也不再有用。出于節(jié)約系統(tǒng)資源的考慮,需要及時(shí)清除畫(huà)布占用的所有內(nèi)存資源。

php中用GD繪制折線圖,代碼如下:

Class Chart{

private $image; // 定義圖像

private $title; // 定義標(biāo)題

private $ydata; // 定義Y軸數(shù)據(jù)

private $xdata; // 定義X軸數(shù)據(jù)

private $seriesName; // 定義每個(gè)系列數(shù)據(jù)的名稱

private $color; // 定義條形圖顏色

private $bgcolor; // 定義圖片背景顏色

private $width; // 定義圖片的寬

private $height; // 定義圖片的長(zhǎng)

/*

* 構(gòu)造函數(shù)

* String title 圖片標(biāo)題

* Array xdata 索引數(shù)組,X軸數(shù)據(jù)

* Array ydata 索引數(shù)組,數(shù)字?jǐn)?shù)組,Y軸數(shù)據(jù)

* Array series_name 索引數(shù)組,數(shù)據(jù)系列名稱

*/

function __construct($title,$xdata,$ydata,$seriesName) {

$this-title = $title;

$this-xdata = $xdata;

$this-ydata = $ydata;

$this-seriesName = $seriesName;

$this-color = array('#DC', '#B', '#EDB', '#DDDF', '#CBE', '#E', '#FF', '#FFF', '#AFC');

}

/*

* 公有方法,設(shè)置條形圖的顏色

* Array color 顏色數(shù)組,元素取值為'#DC'這種形式

*/

function setBarColor($color){

$this-color = $color;

}

/*

* 繪制折線圖

*/

public function paintLineChart() {

$ydataNum = $this-arrayNum($this-ydata); // 取得數(shù)據(jù)分組的個(gè)數(shù)

$max = $this-arrayMax($this-ydata); // 取得所有呈現(xiàn)數(shù)據(jù)的最大值

$max = ($max )? $max : ;

$multi = $max/; // 如果最大數(shù)據(jù)是大于的則進(jìn)行縮小處理

$barHeightMulti = .; // 條形高縮放的比例

$lineWidth = ;

$chartLeft = (+strlen($max))*; // 設(shè)置圖片左邊的margin

$lineY = ; // 初始化條形圖的Y的坐標(biāo)

// 設(shè)置圖片的寬、高

//$this-width = $lineWidth*count($this-xdata) + $chartLeft - $lineWidth/.;

$margin = ; // 小矩形描述右邊margin

$recWidth = ; // 小矩形的寬

$recHeight = ; // 小矩形的高

$space = ; // 小矩形與條形圖的間距

$tmpWidth = ;

// 設(shè)置圖片的寬、高

$lineChartWidth = $lineWidth*count($this-xdata) + $chartLeft - $lineWidth/. ;

// 兩個(gè)系列數(shù)據(jù)以上的加上小矩形的寬

if($ydataNum ) {

$tmpWidth = $this-arrayLengthMax($this-seriesName)**/ + $space + $recWidth + + $margin;

}

$this-width = $lineChartWidth + $tmpWidth;

$this-height = ;

$this-image = imagecreatetruecolor($this-width ,$this-height); // 準(zhǔn)備畫(huà)布

$this-bgcolor = imagecolorallocate($this-image,,,); // 圖片的背景顏色

// 設(shè)置條形圖的顏色

$color = array();

foreach($this-color as $col) {

$col = substr($col,,strlen($col)-);

$red = hexdec(substr($col,,));

$green = hexdec(substr($col,,));

$blue = hexdec(substr($col,,));

$color[] = imagecolorallocate($this-image ,$red, $green, $blue);

}

// 設(shè)置線段的顏色、字體的顏色、字體的路徑

$lineColor = imagecolorallocate($this-image ,xcc,xcc,xcc);

$fontColor = imagecolorallocate($this-image, x,xf,xf);

$fontPath = 'font/simsun.ttc';

imagefill($this-image,,,$this-bgcolor); // 繪畫(huà)背景

// 繪畫(huà)圖的分短線與左右邊線

for($i = ; $i ; $i++ ) {

imageline($this-image,$chartLeft-,$lineY-$barHeightMulti*$max//$multi*$i,$lineChartWidth,$lineY-$barHeightMulti*$max//$multi*$i,$lineColor);

imagestring($this-image,,,$lineY-$barHeightMulti*$max//$multi*$i-,floor($max/*$i),$fontColor);

}

imageline($this-image,$chartLeft-,,$chartLeft-,$lineY,$lineColor);

imageline($this-image,$lineChartWidth-,,$lineChartWidth-,$lineY,$lineColor);

$style = array($lineColor,$lineColor,$lineColor,$lineColor,$lineColor,$this-bgcolor,$this-bgcolor,$this-bgcolor,$this-bgcolor,$this-bgcolor);

imagesetstyle($this-image,$style);

// 繪制折線圖的分隔線(虛線)

foreach($this-xdata as $key = $val) {

$lineX = $chartLeft + + $lineWidth*$key;

imageline($this-image,$lineX,,$lineX,$lineY,IMG_COLOR_STYLED);

}

// 繪畫(huà)圖的折線

foreach($this-ydata as $key = $val) {

if($ydataNum == ) {

// 一個(gè)系列數(shù)據(jù)時(shí)

if($key == count($this-ydata) - ) break;

$lineX = $chartLeft + + $lineWidth*$key;

$lineY = $lineY-$barHeightMulti*($this-ydata[$key+])/$multi;

// 畫(huà)折線

if($key == count($this-ydata) - ) {

imagefilledellipse($this-image,$lineX+$lineWidth,$lineY,,,$color[]);

}

imageline($this-image,$lineX,$lineY-$barHeightMulti*$val/$multi,$lineX+$lineWidth,$lineY,$color[]);

imagefilledellipse($this-image,$lineX,$lineY-$barHeightMulti*$val/$multi,,,$color[]);

}elseif($ydataNum ) {

// 多個(gè)系列的數(shù)據(jù)時(shí)

foreach($val as $ckey = $cval) {

if($ckey == count($val) - ) break;

$lineX = $chartLeft + + $lineWidth*$ckey;

$lineY = $lineY-$barHeightMulti*($val[$ckey+])/$multi;

// 畫(huà)折線

if($ckey == count($val) - ) {

imagefilledellipse($this-image,$lineX+$lineWidth,$lineY,,,$color[$key%count($this-color)]);

}

imageline($this-image,$lineX,$lineY-$barHeightMulti*$cval/$multi,$lineX+$lineWidth,$lineY,$color[$key%count($this-color)]);

imagefilledellipse($this-image,$lineX,$lineY-$barHeightMulti*$cval/$multi,,,$color[$key%count($this-color)]);

}

}

}

// 繪畫(huà)條形圖的x坐標(biāo)的值

foreach($this-xdata as $key = $val) {

$lineX = $chartLeft + $lineWidth*$key + $lineWidth/ - ;

imagettftext($this-image,,-,$lineX,$lineY+,$fontColor,$fontPath,$this-xdata[$key]);

}

// 兩個(gè)系列數(shù)據(jù)以上時(shí)繪制小矩形及之后文字說(shuō)明

if($ydataNum ) {

$x = $lineChartWidth + $space;

$y = ;

foreach($this-seriesName as $key = $val) {

imagefilledrectangle($this-image,$x,$y,$x+$recWidth,$y+$recHeight,$color[$key%count($this-color)]);

imagettftext($this-image,,,$x+$recWidth+,$y+$recHeight-,$fontColor,$fontPath,$this-seriesName[$key]);

$y += $recHeight + ;

}

}

// 繪畫(huà)標(biāo)題

$titleStart = ($this-width - .*strlen($this-title))/;

imagettftext($this-image,,,$titleStart,,$fontColor,$fontPath,$this-title);

// 輸出圖片

header("Content-Type:image/png");

imagepng ( $this-image );

}

/*

* 私有方法,當(dāng)數(shù)組為二元數(shù)組時(shí),統(tǒng)計(jì)數(shù)組的長(zhǎng)度

* Array arr 要做統(tǒng)計(jì)的數(shù)組

*/

private function arrayNum($arr) {

$num = ;

if(is_array($arr)) {

$num++;

for($i = ; $i count($arr); $i++){

if(is_array($arr[$i])) {

$num = count($arr);

break;

}

}

}

return $num;

}

/*

* 私有方法,計(jì)算數(shù)組的深度

* Array arr 數(shù)組

*/

private function arrayDepth($arr) {

$num = ;

if(is_array($arr)) {

$num++;

for($i = ; $i count($arr); $i++){

if(is_array($arr[$i])) {

$num += $this-arrayDepth($arr[$i]);

break;

}

}

}

return $num;

}

/*

* 私有方法,找到一組中的最大值

* Array arr 數(shù)字?jǐn)?shù)組

*/

private function arrayMax($arr) {

$depth = $this-arrayDepth($arr);

$max = ;

if($depth == ) {

rsort($arr);

$max = $arr[];

}elseif($depth ) {

foreach($arr as $val) {

if(is_array($val)) {

if($this-arrayMax($val) $max) {

$max = $this-arrayMax($val);

}

}else{

if($val $max){

$max = $val;

}

}

}

}

return $max;

}

/*

* 私有方法,求數(shù)組的平均值

* Array arr 數(shù)字?jǐn)?shù)組

*/

function arrayAver($arr) {

$aver = array();

foreach($arr as $val) {

if(is_array($val)) {

$aver = array_merge($aver,$val);

}else{

$aver[] = $val;

}

}

return array_sum($aver)/count($aver);

}

/*

* 私有方法,求數(shù)組中元素長(zhǎng)度最大的值

* Array arr 字符串?dāng)?shù)組,必須是漢字

*/

private function arrayLengthMax($arr) {

$length = ;

foreach($arr as $val) {

$length = strlen($val) $length ? strlen($val) : $length;

}

return $length/;

}

// 析構(gòu)函數(shù)

function __destruct(){

imagedestroy($this-image);

}

}

測(cè)試代碼如下:

$xdata = array('測(cè)試一','測(cè)試二','測(cè)試三','測(cè)試四','測(cè)試五','測(cè)試六','測(cè)試七','測(cè)試八','測(cè)試九');

$ydata = array(array(,,,,,,,,),array(,,,,,,,,));

$color = array();

$seriesName = array("七月","八月");

$title = "測(cè)試數(shù)據(jù)";

$Img = new Chart($title,$xdata,$ydata,$seriesName);

$Img-paintLineChart();

效果圖如下:

到此代碼結(jié)束。

下面給大家介紹php中GD庫(kù)的一些簡(jiǎn)單使用

今天了解了一些GD庫(kù)的簡(jiǎn)單使用,現(xiàn)在稍微做一下總結(jié)!

GD庫(kù)是什么?,graphic device,圖像工具庫(kù),gd庫(kù)是php處理圖形的擴(kuò)展庫(kù),gd庫(kù)提供了一系列用來(lái)處理圖片的API,使用GD庫(kù)可以處理圖片,或者生成圖片。 在網(wǎng)站上 GD庫(kù)通常用來(lái)生成縮略圖或者用來(lái)對(duì)圖片加水印或者對(duì)網(wǎng)站數(shù)據(jù)生成報(bào)表。

php并不局限于輸出HTML文本。php通過(guò)使用GD擴(kuò)展庫(kù)還能用來(lái)動(dòng)態(tài)輸出圖像,例如文字按鈕、驗(yàn)證碼、數(shù)據(jù)統(tǒng)計(jì)圖等。哈可以輕松地編輯圖像,力圖處理縮略圖和為圖片添加水印等,具有強(qiáng)大的圖像處理能力。

首先我們來(lái)說(shuō)下GD庫(kù),繪制個(gè)簡(jiǎn)單圖形的一些步驟:

1、首先是創(chuàng)建畫(huà)布,此處我們利用imagecreatetruecolor函數(shù),也可以利用imagecreate,區(qū)別在于前者創(chuàng)建了一個(gè)真彩圖像,后者創(chuàng)建了一個(gè)基于調(diào)色板的圖像

$img=imagecreatetruecolor(100,100),其中有兩個(gè)參數(shù)分別對(duì)應(yīng),我們創(chuàng)建的圖像的寬和高

2、設(shè)置一些必要的"染料盒"

其實(shí)就是定義一些之后會(huì)用到的填充顏色,此處我們統(tǒng)一定義在這個(gè)位置,此處我們利用imagecolorallocate函數(shù)

$white=imagecolorallocate($img,0xFF,0xFF,0xFF)或者可以使用RGB的顏色命名方式 如$white=imagecolorallocate($img,255,255,255);

$gray = imagecolorallocate($img, 0xC0, 0xC0, 0xC0);

$darkgray = imagecolorallocate($img, 0x90, 0x90, 0x90);

$navy = imagecolorallocate($img, 0x00, 0x00, 0x80);

$darknavy = imagecolorallocate($img, 0x00, 0x00, 0x50);

$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);

$darkred = imagecolorallocate($img, 0x90, 0x00, 0x00);

$black=imagecolorallocate($img,0x00,0x00,0x00);

此處我們定義多一些所需要的顏色

3、填充區(qū)域顏色,可以簡(jiǎn)單的理解為填充圖片的背景顏色,利用imagefill函數(shù)

imagefill($img,0,0,$white),此處的0 0表示從坐標(biāo)x y處開(kāi)始填充背景色

4、繪制圖形,例如繪制餅狀圖,所需要的是imagefilledarc函數(shù)

imagefilledarc()的參數(shù)相對(duì)來(lái)說(shuō)較多,形如imagefilledarc($img,50,$i,100,50,0,45,$red,IMG_ARC_PIE);

其中分別表示以red顏色字img圖像上繪制一個(gè)以50,$i為起點(diǎn),以0 45角度這個(gè)范圍內(nèi)繪制弧線

5、期間我們還可以添加一些說(shuō)明問(wèn)題,比如水平的添加一個(gè)字符串,利用 imagestring($img,1,20,40,"hello,world!",$red),表示在img圖片中以20 40為坐標(biāo),寫上一個(gè)紅色的hello,world!字樣

6、就是講圖像輸出

首先要告之瀏覽器要以何種圖片格式輸出,例如以png輸出,則使用header("Content-type:image/png");

其次 將圖片輸出到瀏覽器中,imagepng($img);

最后,銷毀圖片,即釋放該圖片存儲(chǔ)所占用的內(nèi)存 imagedestroy(img);,

PHP怎么做條形統(tǒng)計(jì)圖

以下的推薦使用的php圖形函數(shù)庫(kù):

1. JpGraph

是一個(gè)面向?qū)ο髨D形創(chuàng)建函數(shù)庫(kù)。可用它來(lái)生成柱狀圖,餅狀圖,甘特圖,網(wǎng)狀圖等常用到的一些圖形。支持的圖片格式有GIF,JPG和PNG。

下載地址:

2. pChart

pChart是一個(gè)基于GD library(圖形處理函數(shù)庫(kù))開(kāi)發(fā)的PHP圖表制作開(kāi)源項(xiàng)目。支持多種圖表類型。

下載地址:

3. Highcharts

Highcharts是一個(gè)純JavaScript編寫的圖表庫(kù),為您的網(wǎng)站或Web應(yīng)用程序提供直觀,互動(dòng)式圖表。

Highcharts目前支持線形圖、區(qū)塊圖、柱形圖、條形圖、餅圖和散點(diǎn)圖等類型。

下載地址:

本文標(biāo)題:php數(shù)據(jù)統(tǒng)計(jì)圖 php訪問(wèn)統(tǒng)計(jì)
鏈接URL:http://muchs.cn/article14/doooide.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、服務(wù)器托管商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站排名、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)