1 ClassChart{2 private $image; //定义图像3 private $title; //定义标题4 private $ydata; //定义Y轴数据5 private $xdata; //定义X轴数据6 private $seriesName; //定义每个系列数据的名称7 private $color; //定义条形图颜色8 private $bgcolor; //定义图片背景颜色9 private $width; //定义图片的宽10 private $height; //定义图片的长11 12 /*13 * 构造函数14 * String title 图片标题15 * Array xdata 索引数组,X轴数据16 * Array ydata 索引数组,数字数组,Y轴数据17 * Array series_name 索引数组,数据系列名称18 */19 function __construct($title,$xdata,$ydata,$seriesName) {20 $this->title = $title;21 $this->xdata = $xdata;22 $this->ydata = $ydata;23 $this->seriesName = $seriesName;24 $this->color = array('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4');25 }26 27 /*28 * 公有方法,设置条形图的颜色29 * Array color 颜色数组,元素取值为'#058DC7'这种形式30 */31 function setBarColor($color){32 $this->color = $color;33 }34 /*35 * 绘制折线图36 */37 public functionpaintLineChart() {38 $ydataNum = $this->arrayNum($this->ydata); //取得数据分组的个数39 $max = $this->arrayMax($this->ydata); //取得所有呈现数据的最大值40 $max = ($max > 100)? $max : 100;41 $multi = $max/100; //如果最大数据是大于100的则进行缩小处理42 $barHeightMulti = 2.2; //条形高缩放的比例43 $lineWidth = 50;44 $chartLeft = (1+strlen($max))*12; //设置图片左边的margin45 46 $lineY = 250; //初始化条形图的Y的坐标47 // 设置图片的宽、高48 //$this->width = $lineWidth*count($this->xdata) + $chartLeft - $lineWidth/1.6;49 50 $margin = 10; //小矩形描述右边margin51 $recWidth = 20; //小矩形的宽52 $recHeight = 15; //小矩形的高53 $space = 20; //小矩形与条形图的间距54 $tmpWidth = 0;55 //设置图片的宽、高56 $lineChartWidth = $lineWidth*count($this->xdata) + $chartLeft - $lineWidth/1.6;57 //两个系列数据以上的加上小矩形的宽58 if($ydataNum > 1) {59 $tmpWidth = $this->arrayLengthMax($this->seriesName)*10*4/3 + $space + $recWidth + + $margin;60 }61 $this->width = $lineChartWidth + $tmpWidth;62 63 $this->height = 300;64 $this->image = imagecreatetruecolor($this->width ,$this->height); //准备画布65 $this->bgcolor = imagecolorallocate($this->image,255,255,255); //图片的背景颜色66 67 // 设置条形图的颜色68 $color = array();69 foreach($this->color as $col) {70 $col = substr($col,1,strlen($col)-1);71 $red = hexdec(substr($col,0,2));72 $green = hexdec(substr($col,2,2));73 $blue = hexdec(substr($col,4,2));74 $color[] = imagecolorallocate($this->image ,$red, $green, $blue);75 }76 77 //设置线段的颜色、字体的颜色、字体的路径78 $lineColor = imagecolorallocate($this->image ,0xcc,0xcc,0xcc);79 $fontColor = imagecolorallocate($this->image, 0x95,0x8f,0x8f);80 $fontPath = 'font/simsun.ttc';81 82 imagefill($this->image,0,0,$this->bgcolor); //绘画背景83 84 // 绘画图的分短线与左右边线85 for($i = 0; $i < 6; $i++) {86 imageline($this->image,$chartLeft-10,$lineY-$barHeightMulti*$max/5/$multi*$i,$lineChartWidth,$lineY-$barHeightMulti*$max/5/$multi*$i,$lineColor);87 imagestring($this->image,4,5,$lineY-$barHeightMulti*$max/5/$multi*$i-8,floor($max/5*$i),$fontColor);88 }89 imageline($this->image,$chartLeft-10,30,$chartLeft-10,$lineY,$lineColor);90 imageline($this->image,$lineChartWidth-1,30,$lineChartWidth-1,$lineY,$lineColor);91 $style = array($lineColor,$lineColor,$lineColor,$lineColor,$lineColor,$this->bgcolor,$this->bgcolor,$this->bgcolor,$this->bgcolor,$this->bgcolor);92 imagesetstyle($this->image,$style);93 94 //绘制折线图的分隔线(虚线)95 foreach($this->xdata as $key => $val) {96 $lineX = $chartLeft + 3 + $lineWidth*$key;97 imageline($this->image,$lineX,30,$lineX,$lineY,IMG_COLOR_STYLED);98 }99 100 //绘画图的折线101 foreach($this->ydata as $key => $val) {102 if($ydataNum == 1) {103 //一个系列数据时104 if($key == count($this->ydata) - 1 ) break;105 $lineX = $chartLeft + 3 + $lineWidth*$key;106 $lineY2 = $lineY-$barHeightMulti*($this->ydata[$key+1])/$multi;107 108 //画折线109 if($key == count($this->ydata) - 2) {110 imagefilledellipse($this->image,$lineX+$lineWidth,$lineY2,10,10,$color[0]);111 }112 imageline($this->image,$lineX,$lineY-$barHeightMulti*$val/$multi,$lineX+$lineWidth,$lineY2,$color[0]);113 imagefilledellipse($this->image,$lineX,$lineY-$barHeightMulti*$val/$multi,10,10,$color[0]);114 }elseif($ydataNum > 1) {115 //多个系列的数据时116 foreach($val as $ckey => $cval) {117 118 if($ckey == count($val) - 1 ) break;119 $lineX = $chartLeft + 3 + $lineWidth*$ckey;120 $lineY2 = $lineY-$barHeightMulti*($val[$ckey+1])/$multi;121 //画折线122 if($ckey == count($val) - 2) {123 imagefilledellipse($this->image,$lineX+$lineWidth,$lineY2,10,10,$color[$key%count($this->color)]);124 }125 imageline($this->image,$lineX,$lineY-$barHeightMulti*$cval/$multi,$lineX+$lineWidth,$lineY2,$color[$key%count($this->color)]);126 imagefilledellipse($this->image,$lineX,$lineY-$barHeightMulti*$cval/$multi,10,10,$color[$key%count($this->color)]);127 }128 }129 130 }131 132 //绘画条形图的x坐标的值133 foreach($this->xdata as $key => $val) {134 $lineX = $chartLeft + $lineWidth*$key + $lineWidth/3 - 20;135 imagettftext($this->image,10,-65,$lineX,$lineY+15,$fontColor,$fontPath,$this->xdata[$key]);136 }137 138 //两个系列数据以上时绘制小矩形及之后文字说明139 if($ydataNum > 1) {140 $x1 = $lineChartWidth + $space;141 $y1 = 20;142 foreach($this->seriesName as $key => $val) {143 imagefilledrectangle($this->image,$x1,$y1,$x1+$recWidth,$y1+$recHeight,$color[$key%count($this->color)]);144 imagettftext($this->image,10,0,$x1+$recWidth+5,$y1+$recHeight-2,$fontColor,$fontPath,$this->seriesName[$key]);145 $y1 += $recHeight + 10;146 }147 }148 149 //绘画标题150 $titleStart = ($this->width - 5.5*strlen($this->title))/2;151 imagettftext($this->image,11,0,$titleStart,20,$fontColor,$fontPath,$this->title);152 153 //输出图片154 header("Content-Type:image/png");155 imagepng ( $this->image );156 }157 158 159 /*160 * 私有方法,当数组为二元数组时,统计数组的长度161 * Array arr 要做统计的数组162 */163 private function arrayNum($arr) {164 $num = 0;165 if(is_array($arr)) {166 $num++;167 for($i = 0; $i < count($arr); $i++){168 if(is_array($arr[$i])) {169 $num = count($arr);170 break;171 }172 }173 }174 return $num;175 }176 177 /*178 * 私有方法,计算数组的深度179 * Array arr 数组180 */181 private function arrayDepth($arr) {182 $num = 0;183 if(is_array($arr)) {184 $num++;185 for($i = 0; $i < count($arr); $i++){186 if(is_array($arr[$i])) {187 $num += $this->arrayDepth($arr[$i]);188 break;189 }190 }191 }192 return $num;193 }194 195 /*196 * 私有方法,找到一组中的最大值197 * Array arr 数字数组198 */199 private function arrayMax($arr) {200 $depth = $this->arrayDepth($arr);201 $max = 0;202 if($depth == 1) {203 rsort($arr);204 $max = $arr[0];205 }elseif($depth > 1) {206 foreach($arr as $val) {207 if(is_array($val)) {208 if($this->arrayMax($val) > $max) {209 $max = $this->arrayMax($val);210 }211 }else{212 if($val > $max){213 $max = $val;214 }215 }216 }217 }218 return $max;219 }220 221 /*222 * 私有方法,求数组的平均值223 * Array arr 数字数组224 */225 function arrayAver($arr) {226 $aver = array();227 foreach($arr as $val) {228 if(is_array($val)) {229 $aver = array_merge($aver,$val);230 }else{231 $aver[] = $val;232 }233 }234 return array_sum($aver)/count($aver);235 }236 237 /*238 * 私有方法,求数组中元素长度最大的值239 * Array arr 字符串数组,必须是汉字240 */241 private function arrayLengthMax($arr) {242 $length = 0;243 foreach($arr as $val) {244 $length = strlen($val) > $length ? strlen($val) : $length;245 }246 return $length/3;247 }248 249 //析构函数250 function__destruct(){251 imagedestroy($this->image);252 }253 }

gd动态曲线 php_php顶用GD绘制折线图相关推荐

  1. gd动态曲线 php_php中用GD绘制折线图

    1 ClassChart{2 private $image; //定义图像 3 private $title; //定义标题 4 private $ydata; //定义Y轴数据 5 private ...

  2. gd动态曲线 php_PHP用GD实现折线图

    private $title; //定义标题 private $ydata; //定义Y轴数据 private $xdata; //定义X轴数据 private $seriesName; //定义每个 ...

  3. python画曲线图例-python使用matplotlib绘制折线图教程

    matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并 ...

  4. gd动态曲线 php_PHP 高级编程之多线程

    PHP 高级编程之多线程 目录 1. 多线程环境安装 1.1. PHP 5.5.9 1.2. 安装 pthreads 扩展 2. Thread 3. Worker 与 Stackable 4. 互斥锁 ...

  5. gd动态曲线 php_PHP GD库动态生成折线图的实例代码

    array_push ($p_x, $zuo+$i*$jiange); array_push ($p_y, $shang+round(($img_gao-$shang-$xia)*(1-$shuju[ ...

  6. 微信小程序:wx-charts动态绘制折线图

    微信小程序:wx-charts动态绘制折线图 wx-charts是基于 Canvas的微信小程序主流图表工具,体积小易操作,支持多种图表的绘制,这里主要就动态绘制折线图做出详解,所谓动态,指的是表格的 ...

  7. ECharts动态加载数据绘制折线图

    Echarts动态加载数据绘制折线图 ECharts 引入ECharts 步骤 连接数据接口,动态加载图表 动态加载数据,整体代码 折线图绘制 总结 绘制多个图表的方法 ECharts 纯Javasc ...

  8. python matplotlib绘制折线图

    前言 众所周知,matplotlib 是一款功能强大开源的数据可视化模块,凭借着强大的扩展性构建出更高级别的绘图工具接口如seaborn.ggplot.我们来看看往期学习章节内容概述吧~ 接下来,我们 ...

  9. python如何绘制折线图-python如何画折线图

    python画折线图利用的是matplotlib.pyplot.plot的工具来绘制折线图,这里先给出一个段代码和结果图:# -*- coding: UTF-8 -*- import numpy as ...

  10. python 绘制折线图-怎样用python绘制折线图

    今天教大家用python绘制一些线性图案,需要的朋友可以借鉴参考一下. 画最简单的直线图 代码如下:import numpy as np import matplotlib.pyplot as plt ...

最新文章

  1. jQuery Tools:Web开发必备的 jQuery UI 库
  2. OpenLayers 3+Geoserver+PostGIS实现点击查询
  3. Flink从入门到精通100篇(四)-基于 Flink 和 Drools 的实时日志处理
  4. 黑马Python + 人工智能学习笔记
  5. 单目视觉机器人的循迹_机器人视觉系统传感器的关键技术盘点
  6. 一眼看穿的最佳图像标记工具!
  7. step3 . day4 数据结构之线性表 栈和队
  8. ARM中ROM,RAM,FLASH区别
  9. 炸裂!万字长文拿下HTTP!
  10. android文件管理器项目,浅析Android文件管理器(项目一)
  11. 锤子手机(smartisan t1)如何查看mac地址
  12. android输入法剪贴板,手机写作利器:输入法剪贴板
  13. 程序人生:程序员如何和老板谈升职加薪
  14. list_for_each_entry解析
  15. Sublime Text介绍
  16. 通过插画理解kubernetes基本概念
  17. JavaMai——邮箱验证用户注册
  18. 【不忘初心】 Windows11_22H2_22621.105_X64_可更新[纯净精简版][2.28G](2022.6.16)
  19. 编译原理——词法分析(3)有穷自动机中DFA与NFA的理解
  20. 注册按钮php,JavaScript_javascript实现十秒钟后注册按钮可点击的方法,本文实例讲述了javascript实现十 - phpStudy...

热门文章

  1. 关于一级域名和二级域名跨域的解决方案
  2. Android百度浏览器深色模式,深色模式适配指南
  3. osgEarth的Rex引擎原理分析(一二二)着色器程序的opengl过程
  4. php excel 下拉菜单,Laravel Excel库(Maatwebsite):如何在导出中创建下拉列表 - php
  5. Lerx开源网站内容管理CMS系统源码
  6. 微软产品界面配色方案分析
  7. 用java操作MySQL编写的高校水电费管理系统
  8. 计算机运行慢 卡是什么原因是什么原因,电脑反应慢是怎么回事?五招让你的电脑快得飞起!...
  9. 如何提升串口响应速度
  10. 关于安卓打包脚本aab