博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP轻量级图表类PHP Light Chart:PLChart
阅读量:4316 次
发布时间:2019-06-06

本文共 16658 字,大约阅读时间需要 55 分钟。

 

该类可以生成饼状图(2D/3D),柱状图(2D/3D),以及折线图,可控制输出至浏览器或文件,控制输出的jpeg图像的质量。

所有的设置工作只有5个函数:
1。图表背景色设置setbgcolor()
2。图表参数设置setchartdata()
2。图表标题设置settitle()
3。图表注释设置setdesc()
4。图表图形设置setgraphics()

详细的设置参数请参考文档注释或是后面的测试页面demo.php

(192, 192, 192), "darkgray" => (144, 144, 144), "lightred" => (255, 0, 0), "darkred" => (128, 0, 0), "lightgreen" => (0, 255, 0), "darkgreen" => (0, 80, 0), "lightblue" => (0, 0, 255), "darkblue" => (0, 0, 80), "lightyellow" => (255, 255, 0), "darkyellow" => (200, 200, 50), "lightcyan" => (0, 255, 255), "darkcyan" => (0, 90, 90), ); // ============================ 设置函数 - setting functions ============================ /* * @todo 设置图像背景色 - set the backgroundcolor of the chart image * * @param int $red * @param int $green * @param int $blue * */ 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 * */ setchartdata($type = "pie", $d3d = 1, $width = 400, $height = 400, $quality = 70, $data = (), $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 * */ settitle($tstring = '', $tfontsize = 20, $tangle = 0, $tposx = 10, $tposy = 20, $tfontfile = 'c:/windows/fonts/simhei.ttf', $tfred = 0, $tfgreen = 0, $tfblue = 0) { $this->title = ($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 * */ 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 = ($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 * */ setgraphics($gstartx = 0, $gstarty = 0, $gwidth = 100, $gheight = 100, $gmargin = 10, $shadow = 0.1) { $this->graphics = ($gstartx, $gstarty, $gwidth, $gheight, $gmargin, $shadow); } // ============================ 生成函数 - build functions ============================ /* * @todo 生成图表实例 - build chart object * */ buildchart() { // 图像头信息 - header of the image file ("Content-type: image/jpeg"); // 建立图像 - create image $this->chart = imagecreatetruecolor($this->width, $this->height); // 填充背景色 - set backgroundcolor $bgc = ($this->chart, $this->bgcolor[0], $this->bgcolor[1], $this->bgcolor[2]); ($this->chart, 0, 0, $bgc); // 定义颜色 - allocate colors in the graphics ($this->colordefine $ => $value) { $$ = ($this->chart, $value[0], $value[1], $value[2]); ($this->colors, $$); // 颜色变量入栈 - add color variables into array } } /* * @todo 生成图表标题 - build title of the chart * */ buildtitle() { // 设置标题颜色 - set title color $titlecolor = ($this->chart, $this->title[6], $this->title[7], $this->title[8]); // 写标题 - write title ($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 * */ 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 = ($this->param); // 注释数量 - number of description lines $perh = ($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 = ($this->chart, $this->desc[8], $this->desc[9], $this->desc[10]); // 注释的字体颜色 - font color of description // 写注释 - write description $dstring = ($this->param); ($i = 0; $i < $num; $i++) { // 矩形色块 - colorful square ($this->chart, $dposx, $dposy, $dposx + $dsize, $dposy + $dsize, $this->colors[$i * 2]); // 写文字 - write string ($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 * */ buildgraphics() { // 定义生成图形函数 - define make graphics function // ==================== 饼状图 - pie ==================== ($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 = ($posx + $w / 2 + $margin); $centery = ($posy + $h / 2 + $margin); // 椭圆中心坐标 - the center of the ellipse // 数据处理 - data process $total = ($this->param); // 取得总数 - get total $percent = (0); // 保存比例 - save each percent $temp = 0; ($this->param $v) { $temp += 360 * ($v / $total); ($percent, $temp); // 保存角度 - save angle } // 生成饼状图 - make pie chart // 生成3D饼状图 - make 3D pie chart ($this->d3d == 1) { // 3D阴影 - make 3D shadow ($j = ($centery * (1 + $shadow)); $j > $centery; $j--) { ($k = 0; $k < ($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 ($i = 0; $i < ($percent)-1; $i++) { imagefilledarc($this->chart, $centerx, $centery, $w, $h, $percent[$i], $percent[$i + 1], $this->colors[$i * 2], IMG_ARC_PIE); } } // ==================== 柱状图 - column ==================== ($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 = ($posx + $margin); $starty = ($posy + $h - $margin); // 图形左下角坐标 - the left-bottom position of the graphics // 数据处理 - data process $maxvalue = ($this->param); // 取得最大值 - get max value $num = ($this->param); // 取得条柱个数 - get number of columns $multiple = (10($maxvalue)); // 取得数值区间 - get data field $field = ($maxvalue / (10, $multiple)) + 1; // 区间数 - number of unit fields $fieldnumber = $field > 5 ? $field : 5; // 数据区域数量 - number of data fields $topvalue = $field * (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 ($this->d3d == 1) // 3D效果 - 3D effect { ($this->chart, ($startx, $starty, $startx + $shadowheight, $starty - $shadowheight, $startx + $shadowheight, $posy - $shadowheight, $startx, $posy), 4, $this->colors[0]); ($this->chart, $startx + $shadowheight, $starty - $shadowheight, $startx + $shadowheight, $posy - $shadowheight, $this->colors[1]); } ($this->chart, $startx, $starty, $posx + $w, $starty, $this->colors[1]); // x ($this->chart, $startx, $starty, $startx, $posy, $this->colors[1]); // y // 区间标识 - declare fields ($i = 0; $i <= $fieldnumber; $i++) { // 区间标识线 - lines declaring fields // 3D效果 - 3D effect ($this->d3d == 1) { ($this->chart, $startx, $starty - $unity * $i, $startx + $shadowheight, $starty - $unity * $i - $shadowheight, $this->colors[1]); ($this->chart, $startx + $shadowheight, $starty - $unity * $i - $shadowheight, $posx + $w + $shadowheight, $starty - $unity * $i - $shadowheight, $this->colors[1]); } // 2D - 2D { ($this->chart, $startx, $starty - $unity * $i, $posx + $w, $starty - $unity * $i, $this->colors[0]); } // 区间说明 - field description ($this->chart, $this->desc[5], $this->desc[6], $posx, $starty - $unity * $i, $this->colors[0], $this->desc[7], $topvalue / $fieldnumber * $i); } // 生成条柱 - make columns $paramkeys = ($this->param); ($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 ($this->chart, $columnx, $starty - $columnheight, $columnx + $unitx / 2, $starty - 1, $this->colors[$j * 2]); // 画条柱 - draw columns // 3D效果 - 3D effect ($this->d3d == 1) { // 轮廓线 - contour line ($this->chart, $columnx, $starty - $columnheight, $columnx + $unitx / 2, $starty - 1, $this->colors[$j * 2 + 1]); // 3D表面 - 3D top ($this->chart, ($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]); ($this->chart, ($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 ($this->chart, ($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]); ($this->chart, ($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 ==================== { // 取得图形位置 - 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 = ($posx + $margin); $starty = ($posy + $h - $margin); // 图形左下角坐标 - the left-bottom position of the graphics // 数据处理 - data process $maxvalue = ($this->param); // 取得最大值 - get max value $minvalue = ($this->param); // 取得最小值 - get min value $num = ($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 ($this->chart, $startx, $starty, $posx + $w, $starty, $this->colors[1]); // x ($this->chart, $startx, $starty, $startx, $posy, $this->colors[1]); // y // 区间标识 - declare fields ($i = 0; $i <= $fieldnumber; $i++) { // 标识线 - declaring fields line ($this->chart, $startx, $starty - $unity * $i, $posx + $w, $starty - $unity * $i, $this->colors[0]); // 区间说明 - field description ($this->chart, $this->desc[5], $this->desc[6], $posx, $starty - $unity * $i, $this->colors[0], $this->desc[7], $minvalue + $unitdata * $i); } // 生成线条 - make line $paramkeys = ($this->param); $loca = (); // 保存轨迹的数组 - array to save locas ($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 = ($this->chart, $this->title[6], $this->title[7], $this->title[8]); // 定义线条颜色 - define line color ($i = 0; $i < $num + 3; ) { // 画线条 - draw line ($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 */ outputchart() { // 建立图标 - build chart $this->buildchart(); // 写入图形 - build graphics $this->buildgraphics(); // 写入注释 - build description $this->builddesc(); // 写入标题 - build title $this->buildtitle(); // 输出图像 - flush image ($this->chart, $this->savefile, $this->quality); ($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(); // 销毁实例 ($chart); ?>

转载于:https://www.cnblogs.com/masky5310/archive/2010/08/25/1807961.html

你可能感兴趣的文章
高效掌握C#第五回---猜单词游戏
查看>>
07-Java 中的IO操作
查看>>
uclibc,eglibc,glibc之间的区别和联系【转】
查看>>
Java魔法堂:找外援的利器——Runtime.exec详解
查看>>
mysql数据库存放路径
查看>>
TestNG(五)常用元素的操作
查看>>
解决 Visual Studio 点击添加引用无反应的问题
查看>>
通过镜像下载Android系统源码
查看>>
python字符串格式化 %操作符 {}操作符---总结
查看>>
windows 不能在 本地计算机 启动 Apache
查看>>
iOS开发报duplicate symbols for architecture x86_64错误的问题
查看>>
Chap-6 6.4.2 堆和栈
查看>>
【Java学习笔记之九】java二维数组及其多维数组的内存应用拓展延伸
查看>>
C# MySql 连接
查看>>
网络抓包分析方法大全
查看>>
sql 数据类型
查看>>
android 截图
查看>>
WebServicer接口类生成方法。
查看>>
POJ 1740
查看>>
【翻译】火影忍者鸣人 疾风传 终级风暴2 制作介绍
查看>>