該類可以產生餅狀圖(2D/3D),柱狀圖(2D/3D),以及折線圖,可控制輸出至瀏覽器或檔案,控制輸出的jpeg映像的品質。
所有的設定工作只有5個函數:
1。圖表背景色設定setbgcolor()
2。圖表參數設定setchartdata()
2。圖表標題設定settitle()
3。圖表注釋設定setdesc()
4。圖表圖形設定setgraphics()
詳細的設定參數請參考文檔注釋或是後面的測試頁面demo.php
array(192, 192, 192), "darkgray" => array(144, 144, 144), "lightred" => array(255, 0, 0), "darkred" => array(128, 0, 0), "lightgreen" => array(0, 255, 0), "darkgreen" => array(0, 80, 0), "lightblue" => array(0, 0, 255), "darkblue" => array(0, 0, 80), "lightyellow" => array(255, 255, 0), "darkyellow" => array(200, 200, 50), "lightcyan" => array(0, 255, 255), "darkcyan" => array(0, 90, 90), ); // ============================ 設定函數 - setting functions ============================ /* * @todo 設定映像背景色 - set the backgroundcolor of the chart image * * @param int $red * @param int $green * @param int $blue * */function setbgcolor($red = 0, $green = 0, $blue = 0) { $this->bgcolor[0] = $red; $this->bgcolor[1] = $green; $this->bgcolor[2] = $blue; } /* * @todo 設定映像參數 - set the parameters of the chart image * * @param string $type * @param int $d3d * @param int $width * @param int $height * @param int $quality * @param array $data * @param string $savefile * */function setchartdata($type = "pie", $d3d = 1, $width = 400, $height = 400, $quality = 70, $data = array(), $savefile = '') { $this->type = $type; // 圖表類型 - type of the chart : pie, column, line $this->d3d = $d3d; // 3D效果 - $this->width = $width; // 圖表寬度 $this->height = $height; // 圖表高度 $this->quality = $quality; // 圖表顯示品質 $this->param = $data; // 來源資料數組 $this->savefile = $savefile; // 儲存檔案路徑 } /* * @todo 設定映像標題 - set the title of the chart * * @param string $tstring * @param float $tfontsize * @param float $tangle * @param int $tposx * @param int $tposy * @param string $tfontfile * @param int $tfred * @param int $tfgreen * @param int $tfblue * */function settitle($tstring = '', $tfontsize = 20, $tangle = 0, $tposx = 10, $tposy = 20, $tfontfile = 'c:/windows/fonts/simhei.ttf', $tfred = 0, $tfgreen = 0, $tfblue = 0) { $this->title = array($tfontsize, $tangle, $tposx, $tposy, $tfontfile, $tstring, $tfred, $tfgreen, $tfblue); } /* * @todo 設定映像注釋 - set the decsription of the chart * * @param int $dposx * @param int $dposy * @param int $dwidth * @param int $dheight * @param int $dmargin * @param int $dsize * @param int $dangle * @param string $dfontfile * @param int $dfred * @param int $dfgreen * @param int $dfblue * */function setdesc($dposx = 0, $dposy = 0, $dwidth = 100, $dheight = 300, $dmargin = 10, $dsize = 10, $dangle = 0, $dfontfile = 'c:/windows/fonts/simhei.ttf', $dfred = 0, $dfgreen = 0, $dfblue = 0) { $this->desc = array($dposx, $dposy, $dwidth, $dheight, $dmargin, $dsize, $dangle, $dfontfile, $dfred, $dfgreen, $dfblue); } /* * @todo 設定圖形 - set the graphics of the chart * * @param int $gstartx * @param int $gstarty * @param int $gwidth * @param int $gheight * @param int $gmargin * @param float $shadow * */function setgraphics($gstartx = 0, $gstarty = 0, $gwidth = 100, $gheight = 100, $gmargin = 10, $shadow = 0.1) { $this->graphics = array($gstartx, $gstarty, $gwidth, $gheight, $gmargin, $shadow); } // ============================ 產生函數 - build functions ============================ /* * @todo 組建圖表執行個體 - build chart object * */ function buildchart() { // 映像頭資訊 - header of the image file header("Content-type: image/jpeg"); // 建立映像 - create image $this->chart = imagecreatetruecolor($this->width, $this->height); // 填充背景色 - set backgroundcolor $bgc = imagecolorallocate($this->chart, $this->bgcolor[0], $this->bgcolor[1], $this->bgcolor[2]); imagefill($this->chart, 0, 0, $bgc); // 定義顏色 - allocate colors in the graphics foreach($this->colordefine as $key => $value) { $$key = imagecolorallocate($this->chart, $value[0], $value[1], $value[2]); array_push($this->colors, $$key); // 顏色變數入棧 - add color variables into array } } /* * @todo 組建圖表標題 - build title of the chart * */ function buildtitle() { // 設定標題顏色 - set title color $titlecolor = imagecolorallocate($this->chart, $this->title[6], $this->title[7], $this->title[8]); // 寫標題 - write title imagettftext($this->chart, $this->title[0], $this->title[1], $this->title[2], $this->title[3], $titlecolor, $this->title[4], $this->title[5]); } /* * @todo 組建圖表說明 - build description of the chart * */ function builddesc() { // 注釋位置變數 - image position variables $dposx = $this->desc[0]; $dposy = $this->desc[1]; // 注釋起始座標 - the start position of the description $w = $this->desc[2]; // 注釋部分寬度 - width of all description $h = $this->desc[3]; // 注釋部分高度 - height of all description $num = count($this->param); // 注釋數量 - number of description lines $perh = round($h / $num); // 每行注釋的平均高度 - height of each description line $margin = $this->desc[4]; // 注釋的間距 - margin between square and font $dsize = $this->desc[5]; // 注釋的字型大小 - font size of description $dangle = $this->desc[6]; // 注釋的字型角度 - font display angle of description $dfontfile = $this->desc[7]; // 注釋的字型檔 - font file of description $dfcolor = imagecolorallocate($this->chart, $this->desc[8], $this->desc[9], $this->desc[10]); // 注釋的字型顏色 - font color of description // 寫注釋 - write description $dstring = array_keys($this->param); for($i = 0; $i < $num; $i++) { // 矩形色塊 - colorful square imagefilledrectangle($this->chart, $dposx, $dposy, $dposx + $dsize, $dposy + $dsize, $this->colors[$i * 2]); // 寫文字 - write string imagettftext($this->chart, $dsize, $dangle, $dposx + $dsize + $margin, $dposy + $dsize, $dfcolor, $dfontfile, $dstring[$i] . " - " . $this->param[$dstring[$i]]); // 下移 - move down to write next description $dposy += $perh; } } /* * @todo 產生圖形 - build graphics of the chart * * @param source $chart * @param int $d3d * @param array $param * @param array $colordefine * */ function buildgraphics() { // 定義產生圖形函數 - define make graphics function // ==================== 餅狀圖 - pie ==================== if($this->type == "pie") { // 取得圖形位置 - get the positoin of the graphics $posx = $this->graphics[0]; $posy = $this->graphics[1]; // 取得圖形寬度和高度 - get width and height of the graphics $w = $this->graphics[2]; $h = $this->graphics[3]; // 圖形邊距 - graphics margin $margin = $this->graphics[4]; // 3D陰影高度對於橢圓高度的比例 - percent of 3D effect shadow height as the height of the ellipse $shadow = $this->graphics[5]; // 圖形位置變數 - image position variables $centerx = round($posx + $w / 2 + $margin); $centery = round($posy + $h / 2 + $margin); // 橢圓中心座標 - the center of the ellipse // 資料處理 - data process $total = array_sum($this->param); // 取得總數 - get total $percent = array(0); // 儲存比例 - save each percent $temp = 0; foreach($this->param as $v) { $temp += 360 * ($v / $total); array_push($percent, $temp); // 儲存角度 - save angle } // 產生餅狀圖 - make pie chart // 產生3D餅狀圖 - make 3D pie chart if($this->d3d == 1) { // 3D陰影 - make 3D shadow for ($j = ($centery * (1 + $shadow)); $j > $centery; $j--) { for ($k = 0; $k < count($percent)-1; $k++) { imagefilledarc($this->chart, $centerx, $j, $w, $h, $percent[$k], $percent[$k + 1], $this->colors[$k * 2 + 1], IMG_ARC_NOFILL); } } } // 產生平麵餅狀圖 - make 2D pie chart for ($i = 0; $i < count($percent)-1; $i++) { imagefilledarc($this->chart, $centerx, $centery, $w, $h, $percent[$i], $percent[$i + 1], $this->colors[$i * 2], IMG_ARC_PIE); } } // ==================== 柱狀圖 - column ==================== elseif($this->type == "column") { // 取得圖形位置 - get the positoin of the graphics $posx = $this->graphics[0]; $posy = $this->graphics[1]; // 取得圖形寬度和高度 - get width and height of the graphics $w = $this->graphics[2]; $h = $this->graphics[3]; // 圖形邊距 - graphics margin $margin = $this->graphics[4]; // 3D陰影高度對於柱體寬度的比例 - percent of 3D effect shadow height as the width of the column $shadow = $this->graphics[5]; // 圖形位置變數 - image position variables $startx = round($posx + $margin); $starty = round($posy + $h - $margin); // 圖形左下角座標 - the left-bottom position of the graphics // 資料處理 - data process $maxvalue = max($this->param); // 取得最大值 - get max value $num = count($this->param); // 取得條柱個數 - get number of columns $multiple = floor(log10($maxvalue)); // 取得數值區間 - get data field $field = floor($maxvalue / pow(10, $multiple)) + 1; // 區間數 - number of unit fields $fieldnumber = $field > 5 ? $field : 5; // 資料區域數量 - number of data fields $topvalue = $field * pow(10, $multiple); // 圖表最高點數值 - value of the top $unitx = ($w - $margin * 2) / $num; // 取得x單位長度 - get x unit length $unity = ($h - $margin * 2) / $fieldnumber; // 取得y單位長度 - get y unit length $shadowheight = $unitx / 2 * $shadow; // 陰影寬度 - shadow height // 初始化座標系 - initialize reference frame if($this->d3d == 1) // 3D效果 - 3D effect { imagefilledpolygon($this->chart, array($startx, $starty, $startx + $shadowheight, $starty - $shadowheight, $startx + $shadowheight, $posy - $shadowheight, $startx, $posy), 4, $this->colors[0]); imageline($this->chart, $startx + $shadowheight, $starty - $shadowheight, $startx + $shadowheight, $posy - $shadowheight, $this->colors[1]); } imageline($this->chart, $startx, $starty, $posx + $w, $starty, $this->colors[1]); // x imageline($this->chart, $startx, $starty, $startx, $posy, $this->colors[1]); // y // 區間標識 - declare fields for($i = 0; $i <= $fieldnumber; $i++) { // 區間標識線 - lines declaring fields // 3D效果 - 3D effect if($this->d3d == 1) { imageline($this->chart, $startx, $starty - $unity * $i, $startx + $shadowheight, $starty - $unity * $i - $shadowheight, $this->colors[1]); imageline($this->chart, $startx + $shadowheight, $starty - $unity * $i - $shadowheight, $posx + $w + $shadowheight, $starty - $unity * $i - $shadowheight, $this->colors[1]); } // 2D - 2D else { imageline($this->chart, $startx, $starty - $unity * $i, $posx + $w, $starty - $unity * $i, $this->colors[0]); } // 區間說明 - field description imagettftext($this->chart, $this->desc[5], $this->desc[6], $posx, $starty - $unity * $i, $this->colors[0], $this->desc[7], $topvalue / $fieldnumber * $i); } // 產生條柱 - make columns $paramkeys = array_keys($this->param); for($j = 0; $j < $num; $j++) { $columnheight = ($h - $margin * 2) * ($this->param[$paramkeys[$j]] / $topvalue); // 條柱高度 - column height $columnx = $startx + $unitx / 4 + $unitx * $j; // 條柱起點x座標 - x coordinate of column imagefilledrectangle($this->chart, $columnx, $starty - $columnheight, $columnx + $unitx / 2, $starty - 1, $this->colors[$j * 2]); // 畫條柱 - draw columns // 3D效果 - 3D effect if($this->d3d == 1) { // 輪廓線 - contour line imagerectangle($this->chart, $columnx, $starty - $columnheight, $columnx + $unitx / 2, $starty - 1, $this->colors[$j * 2 + 1]); // 3D表面 - 3D top imagefilledpolygon($this->chart, array($columnx, $starty - $columnheight, $columnx + $unitx / 2, $starty - $columnheight, $columnx + $unitx / 2 + $shadowheight, $starty - $columnheight - $shadowheight, $columnx + $shadowheight, $starty - $columnheight - $shadowheight), 4, $this->colors[$j * 2]); imagepolygon($this->chart, array($columnx, $starty - $columnheight, $columnx + $unitx / 2, $starty - $columnheight, $columnx + $unitx / 2 + $shadowheight, $starty - $columnheight - $shadowheight, $columnx + $shadowheight, $starty - $columnheight - $shadowheight), 4, $this->colors[$j * 2 + 1]); // 3D陰影 - 3D shadow imagefilledpolygon($this->chart, array($columnx + $unitx / 2, $starty, $columnx + $unitx / 2 + $shadowheight, $starty - $shadowheight, $columnx + $unitx / 2 + $shadowheight, $starty - $columnheight - $shadowheight, $columnx + $unitx / 2, $starty - $columnheight), 4, $this->colors[$j * 2 + 1]); imagepolygon($this->chart, array($columnx + $unitx / 2, $starty, $columnx + $unitx / 2 + $shadowheight, $starty - $shadowheight, $columnx + $unitx / 2 + $shadowheight, $starty - $columnheight - $shadowheight, $columnx + $unitx / 2, $starty - $columnheight), 4, $this->colors[$j * 2 + 1]); } } } // ==================== 曲線圖 - line ==================== else { // 取得圖形位置 - get the positoin of the graphics $posx = $this->graphics[0]; $posy = $this->graphics[1]; // 取得圖形寬度和高度 - get width and height of the graphics $w = $this->graphics[2]; $h = $this->graphics[3]; // 圖形邊距 - graphics margin $margin = $this->graphics[4]; // 每個點的直徑 - diameter of each point $pointsize = $this->graphics[5] * 20; // 圖形位置變數 - image position variables $startx = round($posx + $margin); $starty = round($posy + $h - $margin); // 圖形左下角座標 - the left-bottom position of the graphics // 資料處理 - data process $maxvalue = max($this->param); // 取得最大值 - get max value $minvalue = min($this->param); // 取得最小值 - get min value $num = count($this->param); // 取得點個數 - get number of points $fieldnumber = $num; // 資料區域數量 - number of data fields $fielddata = $maxvalue - $minvalue; // 取得資料區間 - get data field $unitdata = $fielddata / $fieldnumber; // 取得單位區間數值 - get unit field data value $unitx = ($w - $margin * 2) / $num; // 取得x單位長度 - get x unit length $unity = ($h - $margin * 2) / $fieldnumber; // 取得y單位長度 - get y unit length // 初始化座標系 - initialize reference frame imageline($this->chart, $startx, $starty, $posx + $w, $starty, $this->colors[1]); // x imageline($this->chart, $startx, $starty, $startx, $posy, $this->colors[1]); // y // 區間標識 - declare fields for($i = 0; $i <= $fieldnumber; $i++) { // 標識線 - declaring fields line imageline($this->chart, $startx, $starty - $unity * $i, $posx + $w, $starty - $unity * $i, $this->colors[0]); // 區間說明 - field description imagettftext($this->chart, $this->desc[5], $this->desc[6], $posx, $starty - $unity * $i, $this->colors[0], $this->desc[7], $minvalue + $unitdata * $i); } // 產生線條 - make line $paramkeys = array_keys($this->param); $loca = array(); // 儲存軌跡的數組 - array to save locas for($i = 0; $i < $num; $i++) // 得到軌跡數組 - get loca array { // 點x座標 - x coordinate of the point $pointx = $startx + $unitx * $i; // 點y座標 - y coordinate of the point $pointy = $starty - $unity * $fieldnumber * (($this->param[$paramkeys[$i]] - $minvalue) / $fielddata); // 座標資料入棧 - push coordinates into array $loca[$i * 2] = $pointx; $loca[$i * 2 + 1] = $pointy; // 畫點 - draw point imagefilledellipse($this->chart, $pointx, $pointy, $pointsize, $pointsize, $this->colors[$i * 2]); } $linecolor = imagecolorallocate($this->chart, $this->title[6], $this->title[7], $this->title[8]); // 定義線條顏色 - define line color for($i = 0; $i < $num + 3; ) { // 畫線條 - draw line imageline($this->chart, $loca[$i], $loca[$i + 1], $loca[$i + 2], $loca[$i + 3], $linecolor); $i += 2; } } } /* * @todo 輸出映像至瀏覽器或檔案 - output the chart image to browser or file */ function outputchart() { // 建立表徵圖 - build chart $this->buildchart(); // 寫入圖形 - build graphics $this->buildgraphics(); // 寫入注釋 - build description $this->builddesc(); // 寫入標題 - build title $this->buildtitle(); // 輸出映像 - flush image imagejpeg($this->chart, $this->savefile, $this->quality); imagedestroy($this->chart); } } ?>
*********************************************************************************
示範檔案demo.php:
**********************************************************************************
20, "2月份:16%" => 23, "3月份:19%" => 27, "4月份:13%" => 18, "5月份:18%" => 24, "6月份:12%" => 25); // 執行個體化 $chart = new plchart(); // 設定背景色 // 三個參數為0-255 $chart->setbgcolor(255,255,255); // 設定標題文字 $title = <<< string圖表測試 TEST PIE CHART string; // 設定圖表資料 /* * 第一個參數為圖表類型,只能是三個值:pie, column, line * 第二個參數為是否啟用3D顯示,0代表不啟用,1代表啟動 * 第三個參數為圖表的寬度 * 第四個參數為圖表的高度 * 第五個參數為圖表的圖片品質,0-100,100為最佳品質,但檔案相對也最大 * 第六個參數為原資料數組 */$chart->setchartdata("line", 0, 800, 600, 100, $data); // 設定標題 /* * 第一個參數為標題的字串 * 第二個參數為標題字型大小 * 第三個參數為標題傾斜角度 * 第四個參數為標題位於圖表的起始橫座標 * 第五個參數為標題文字的基準縱座標 * 第六個參數為標題字型檔 * 最後三個參數為標題字型的RGB顏色 */$chart->settitle($title, 20, 0, 10, 20, 'c:/windows/fonts/simhei.ttf', 0, 0, 0); // 設定注釋 /* * 第一個參數為注釋位於圖表的起始橫座標 * 第二個參數為注釋位於圖表的起始縱座標 * 第三個參數為注釋部分的寬度 * 第四個參數為注釋部分的高度 * 第五個參數為注釋內容與注釋部分邊框的間距 * 第六個參數為注釋字型大小 * 第七個參數為注釋字型傾斜角度 * 第六個參數為注釋字型檔 * 最後三個參數為注釋字型的RGB顏色 */$chart->setdesc(500, 100, 200, 300, 10, 18, 0, 'c:/windows/fonts/simhei.ttf', 0, 0, 0); // 設定圖形 /* * 第一個參數為圖形部分位於整個圖表的左上方橫座標 * 第二個參數為圖形部分位於整個圖表的左上方縱座標 * 第三個參數為圖形部分的寬度 * 第四個參數為圖形部分的高度 * 第五個參數為圖形與圖形部分邊框的間距(縱座標標記的寬度) * 第六個參數為3D陰影的相對比例或是軌跡圖中圓點的直徑 */$chart->setgraphics(10, 100, 500, 500, 50, 0.3); // 輸出圖表 $chart->outputchart(); // 銷毀執行個體 unset($chart); ?>