日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php绘制饼图,php怎么绘制饼图?

發(fā)布時間:2023/12/4 php 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php绘制饼图,php怎么绘制饼图? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

php怎么繪制餅圖?

在php中,可以使用GD繪制餅圖。

GD庫是php處理圖形的擴展庫,GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片,也可以給圖片加水印。

PHP中用GD繪制餅圖,繪制的類見代碼:Class Chart{

private $image; // 定義圖像

private $title; // 定義標題

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

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

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

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

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

private $height; // 定義圖片的長

/*

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

* String title 圖片標題

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

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

*/

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

$this->title = $title;

$this->xdata = $xdata;

$this->ydata = $ydata;

$this->color = array('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4');

}

/*

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

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

*/

function setBarColor($color){

$this->color = $color;

}

/*

* 繪制餅圖

*/

function mkPieChart() {

$sum = array_sum($this->ydata); // 獲取ydata所有元素之和

$start = 0; // 弧的開始角度

$end = 0; // 弧的結(jié)束角度

$pieWidth = 300; // 橢圓的長軸

$pieHeight = 220; // 橢圓的短軸

$space = 40; // 橢圓與小矩形的間距

$margin = 20; // 圖片的邊距

$recWidth = 20; // 小矩形的寬

$recHeight = 15; // 小矩形的高

$titleHeight = 50; // 標題區(qū)域的高

// 圖片自適應(yīng)寬與高

$this->width = $pieWidth + $this->arrayLengthMax($this->xdata)*10*4/3 + $space + $recWidth +$margin;

$this->height = (($pieHeight > count($this->xdata)*25 ) ? $pieHeight : count($this->xdata)*25) + $titleHeight;

// 橢圓中心的坐標

$cx = $pieWidth/2+$margin;

$cy = $pieHeight/2+$titleHeight;

$this->image = imagecreatetruecolor($this->width ,$this->height); // 準備畫布

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

imagefill($this->image,0,0,$this->bgcolor); // 填充背景

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

$color = array();

foreach($this->color as $col) {

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

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

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

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

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

}

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

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

$fontColor = imagecolorallocate($this->image, 0x95,0x8f,0x8f);

$fontPath = 'font/simsun.ttc';

// 繪制扇形弧

for($i = 0; $i < 10; $i++) {

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

$end += 360*$val/$sum;

imagefilledarc($this->image,$cx,$cy-$i,$pieWidth,$pieHeight, $start,$end,$color[$key%count($this->color)],IMG_ARC_PIE);

$start = $end;

}

}

// 繪制小矩形及之后文字說明

$x1 = $pieWidth+$space;

$y1 = $titleHeight ;

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

imagefilledrectangle($this->image,$x1,$y1,$x1+$recWidth,$y1+$recHeight,$color[$key%count($this->color)]);

imagettftext($this->image,10,0,$x1+$recWidth+5,$y1+$recHeight-2,$fontColor,$fontPath,$this->xdata[$key]);

$y1 += $recHeight + 10;

}

// 繪畫標題

$titleStart = ($this->width - 5.5*strlen($this->title))/2;

imagettftext($this->image,11,0,$titleStart,20,$fontColor,$fontPath,$this->title);

// 輸出圖片

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

imagepng($this->image);

}

/*

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

* Array arr 字符串數(shù)組,必須是漢字

*/

private function arrayLengthMax($arr) {

$length = 0;

foreach($arr as $val) {

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

}

return $length/3;

}

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

function __destruct(){

imagedestroy($this->image);

}

}

測試代碼如下:$xdata = array('測試一','測試二','測試三','測試四','測試五','測試六','測試七','測試八','測試九');

$ydata = array(89,90,90,23,35,45,56,23,56);

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

$Img->mkPieChart();

效果圖如下:

GD庫的主要用途

在網(wǎng)站上GD庫通常用來生成縮略圖,或者用來對圖片加水印,或者用來生成漢字驗證碼,或者對網(wǎng)站數(shù)據(jù)生成報表等。在PHP處理圖像,可使用GD庫,而GD庫開始時是支持GIF的,但由于GIF使用了有版權(quán)爭議的LZW算法,會引起法律問題,于是從 GD 庫 1.6 版起所有的 GIF 支持都移除了,但是又在 GD 庫 2.0.28 版起又加了回來。如果使用二者之間版本的 GD 庫時 GIF 相關(guān)函數(shù)不可用。

更多相關(guān)知識,請訪問 PHP中文網(wǎng)!!

相關(guān)標簽:php 餅圖

本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請注明出處,感謝您的尊重!

總結(jié)

以上是生活随笔為你收集整理的php绘制饼图,php怎么绘制饼图?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。