欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php打造智能化的柱状图程序,用于报表等

程序员文章站 2022-06-18 17:11:32
php打造智能化的柱状图程序,用于报表等

php打造智能化的柱状图程序,用于报表等

<?php 
/*** 
 * @project bar graph program 
 * @license gpl 
 * @package 
 * @file grapbar.php 
 * @date 2007-4-3 
 * @version 1.0 
 * @last modified 
  
 * 定义 柱状图(柱形图) 类 
 * 
 * 注意,使用前请确保字体路径存在并允许访问,如果出错,还要检查在php.ini配置中的open_basedir项,如果没此路径请添加,或在程序中设置包含 
 * 
 * 智能化的柱状图程序,用于报表等 
 * 
 ***/ 
  
define("default_font_path", "c:/windows/fonts/simhei.ttf"); 
class singlebar 
{ 
 private $_x; 
 private $_y; 
 private $_h; 
 public $_l = 50; 
 private $_w = null; 
 private $_srcpoints = array(); 
 private $_points = array(); 
  
 public function __construct($x, $y, $h, $l = 50, $w = null) 
 { 
 $this->_x = $x; 
 $this->_y = $y; 
 $this->_h = $h; 
 $this->_l = $l; 
 $this->_w = $w; 
 $this->_srcpoints = $this->getsrcpoints(); 
 $this->_points = $this->getpoints(); 
 } 
  
 public function getsrcpoints() 
 { 
 return array( 
  array($this->_x         , $this->_y), 
  array($this->_x+$this->_l    , $this->_y), 
  array($this->_x+(1.35*$this->_l), $this->_y-(0.35*$this->_l)), 
  array($this->_x+(0.35*$this->_l), $this->_y-(0.35*$this->_l)), 
  array($this->_x         , $this->_y+$this->_h), 
  array($this->_x+$this->_l    , $this->_y+$this->_h), 
  array($this->_x+(1.35*$this->_l), $this->_y+$this->_h-(0.35*$this->_l)) 
 ); 
 } 
  
 public function getpoints() 
 { 
 $points = array(); 
 foreach($this->_srcpoints as $key => $val) 
 { 
  $points[] = $val[0]; 
  $points[] = $val[1]; 
 } 
 return $points; 
 } 
  
 public function gettoppoints() 
 { 
 return array_slice($this->_points, 0, 8); //顶坐标 
 } 
  
 public function getbelowpoints() 
 { 
 return array_merge(array_slice($this->_points, 0, 2), array_slice($this->_points, 8, 4), array_slice($this->_points, 2, 2)); //下坐标 
 } 
  
 public function getrightsidepoints() 
 { 
 return array_merge(array_slice($this->_points, 2, 2), array_slice($this->_points, 10, 4), array_slice($this->_points, 4, 2)); //右侧坐标 
 } 
  
 public function draw($image, $topcolor, $belowcolor, $sidecolor, $bordercolor = null, $type = 'left') 
 { 
 if (is_null($bordercolor)) 
 { 
  $bordercolor = 0xcccccc; 
 } 
   
 $top_rgb = $this->getrgb($topcolor); 
 $below_rgb = $this->getrgb($belowcolor); 
 $side_rgb = $this->getrgb($sidecolor); 
 $top_color = imagecolorallocate($image, $top_rgb['r'], $top_rgb['g'], $top_rgb['b']); 
 $below_color = imagecolorallocate($image, $below_rgb['r'], $below_rgb['g'], $below_rgb['b']); 
 $side_color = imagecolorallocate($image, $side_rgb['r'], $side_rgb['g'], $side_rgb['b']); 
   
 imagefilledpolygon($image, $this->gettoppoints(), 4, $top_color); //画顶面 
 imagepolygon($image, $this->gettoppoints(), 4, $bordercolor); //画顶面边线 
   
 imagefilledpolygon($image, $this->getbelowpoints(), 4, $below_color); //画下面 
 imagepolygon($image, $this->getbelowpoints(), 4, $bordercolor); //画下面边线 
   
 if ($type == 'left') 
 { 
  imagefilledpolygon($image, $this->getrightsidepoints(), 4, $side_color); //画右侧面 
  imagepolygon($image, $this->getrightsidepoints(), 4, $bordercolor); //画侧面边线 
 }  
 } 
  
 public function getrgb($color) 
 { 
 $ar = array(); 
 $color = hexdec($color); 
 $ar['r'] = ($color>>16) & 0xff; 
 $ar['g'] = ($color>>8) & 0xff; 
 $ar['b'] = ($color) & 0xff; 
 return $ar; 
 } 
} 
  
class bar 
{ 
 private $_w; 
 private $_h; 
 private $_bgcolor = "ffffff"; 
 private $_barheights = array(); 
 private $_bartexts = array(); 
 private $_barcolors = array(); 
 public $_title; 
 public $_paddingtop = 30; 
 public $_paddingbottom = 100; 
 public $_paddingleft = 45; 
 public $_paddingright = 2; 
 public $_barl = 50; 
 public $image; 
  
 public function __construct($imgw, $imgh, $barheights, $bartexts = null, $barcolors = null) 
 { 
 $this->_w = $imgw; 
 $this->_h = $imgh; 
 $this->_barheights = $barheights; 
 $this->_bartexts  = $bartexts; 
 $this->_barcolors = $barcolors; 
 $this->_paddingbottom = $this->resetpaddingbottom(); 
 $this->_h = $this->resetheight(); 
 $this->image = imagecreatetruecolor($this->_w, $this->_h); 
 } 
  
 public function stroke() 
 { 
 $this->drawbg(); 
 $this->drawbars(); 
 $this->drawtitle(); 
 $this->drawlables(); 
 ob_start(); 
 //header("content-type: image/png"); 
 //imagepng($this->image); 
 header("content-type: " . image_type_to_mime_type(imagetype_jpeg)); 
    imagejpeg($this->image); 
 imagedestroy($this->image); 
 } 
  
 public function drawbg() 
 { 
 $img_w = $this->_w; 
 $img_h = $this->_h; 
 $paddingtop  = $this->_paddingtop; 
 $paddingbottom = $this->_paddingbottom; 
 $paddingleft  = $this->_paddingleft; 
 $paddingright = $this->_paddingright; 
 $rgb = $this->getrgb($this->_bgcolor); 
 $bg = imagecolorallocate($this->image,$rgb['r'], $rgb['g'], $rgb['b']); 
 imagefilledrectangle($this->image, 0, 0, $img_w, $img_h, $bg); 
 $side_bg = imagecolorallocatealpha($this->image, 220, 220, 220, 75); 
 $side_bg2 = imagecolorallocate($this->image, 220, 220, 220); 
 $border_color = imagecolorallocate($this->image, 190, 190, 190); 
 $line_color = imagecolorallocate($this->image, 236, 236, 236); 
 $dial_color = imagecolorallocate($this->image, 131, 131, 131); 
   
 $x1 = $paddingleft; 
 $y1 = $paddingtop; 
 $x2 = $img_w - $paddingright; 
 $y2 = $img_h - $paddingbottom; 
 imagerectangle($this->image, $x1, $y1, $x2, $y2, $border_color); 
 imagefilledpolygon($this->image, array($x1-5,$y1+10, $x1-5,$y2+10, $x1,$y2, $x1,$y1), 4, $side_bg); 
    imagepolygon($this->image, array($x1-5,$y1+10, $x1-5,$y2+10, $x1,$y2, $x1,$y1), 4, $border_color); 
 imagefilledpolygon($this->image, array($x1-5,$y2+10, $x2-5,$y2+10, $x2,$y2, $x1,$y2), 4, $side_bg); 
    imagepolygon($this->image, array($x1-5,$y2+10, $x2-5,$y2+10, $x2,$y2, $x1,$y2), 4, $border_color); 
 imageline($this->image, $x1, $y2, $x2, $y2, $side_bg2); 
   
 $total_h = $img_h - $paddingtop - $paddingbottom; 
 $every_h = $total_h/11; 
 for($i=1; $i<=10; $i++) 
 { 
  imageline($this->image, $x1, $y1+($every_h*$i), $x2, $y1+($every_h*$i), $line_color); //画网线 
 } 
   
 $max_h = max($this->_barheights); 
 for($i=1; $i<=10; $i++) 
 { 
  $value = $max_h - (($max_h/10)*($i-1)); 
  $value = strval($value); 
  $str_w = strlen($value)*5; 
  imageline($this->image, $x1-5-3, $y1+10+($every_h*$i), $x1-3+1, $y1+10+($every_h*$i), $dial_color); //画刻度线 
  imagestring($this->image, 3, $x1-5-3-$str_w-1, $y1+10+($every_h*$i)-5, $value, 0x000000); 
 } 
 } 
  
  
 public function drawbars() 
 { 
 $counts = count($this->_barheights); 
 if (empty($this->_barcolors)) 
 { 
  $color = $this->setcolor(); 
  $this->_barcolors = array_slice($color, 0, $counts); 
  //shuffle($this->_barcolors); 
 } 
 $every_w = ($this->_w - $this->_paddingleft - $this->_paddingright)/$counts; //每一段宽 
 $barl = $every_w; 
 $barl = ($barl > $this->_barl*1.35+6) ? $this->_barl : $barl/1.35 - 6; 
 $max_h = max($this->_barheights); 
 $ruler_h = $this->_h - $this->_paddingtop - $this->_paddingbottom; //标尺总高度 
 $stander_h = $ruler_h - ($ruler_h/11); //标尺10等分的高度 
 $i = 0; 
 foreach ($this->_barheights as $val) 
 { 
  $h = ($stander_h/$max_h)*$val; 
  $x = $this->_paddingleft + ($every_w*$i) + (($every_w - ($barl*1.35))/2);; 
  $y = $this->_h - $this->_paddingbottom + 10 - $h; 
  //$t_color = $this->_barcolors[$i]; 
  $b_color = $this->_barcolors[$i]; 
  //$s_color = $this->_barcolors[$i]; 
  
   
  $rgb = $this->getrgb($this->_barcolors[$i]); 
  $r = $rgb['r'] * 0.7; 
  $g = $rgb['g'] * 0.7; 
  $b = $rgb['b'] * 0.7; 
   
  $c1 = $r > 0 ? dechex($r) : '00'; 
  $c2 = $g > 0 ? dechex($g) : '00'; 
  $c3 = $b > 0 ? dechex($b) : '00'; 
  
  $t_color = $b_color; 
  $s_color = $c1. $c2 . $c3; 
  
  $singlebar = new singlebar($x, $y, $h, $barl); 
  $singlebar->draw($this->image, $t_color, $b_color, $s_color); 
  $i++; 
 } 
 } 
  
 public function drawtitle() 
 { 
 if (empty($this->_title)) 
 { 
  return; 
 } 
 $font = 5; 
 $font_w = imagefontwidth($font); 
 $len = strlen($this->_title); 
 $x = ($this->_w + $this->_paddingleft - $this->_paddingright)/2; 
 $x -= ($len*$font_w)/2; 
 $y = ($this->_paddingtop - $font_w)/2 + 12; 
 //imagestring($this->image, $font, $x, $y, $title, 0x000000); 
 imagettftext($this->image, 12, 0, $x, $y, 0x000000, default_font_path, $this->_title); 
 } 
  
 public function drawlables() 
 { 
 $x1 = $this->_paddingleft - 5; 
 $y1 = $this->_h - $this->_paddingbottom + 20; 
 $x2 = $this->_w - $this->_paddingright; 
 $y2 = $this->_h - 10; 
 //imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, 0xffffff); 
 imagerectangle($this->image, $x1, $y1, $x2, $y2, 0x000000); 
 $space = 5; 
 $x = $x1 + 3; 
 $y = $y1 + 3; 
 foreach ($this->_bartexts as $key => $val) 
 { 
  $color = $this->_barcolors[$key]; 
  $rgb = $this->getrgb($color); 
  $bg = imagecolorallocate($this->image,$rgb['r'], $rgb['g'], $rgb['b']); 
  imagefilledrectangle($this->image, $x, $y, $x+12, $y+12, $bg); //绘12*12的矩形 
     imagerectangle($this->image, $x, $y, $x+12, $y+12, 0x000000); //绘12*12的矩形框 
  imagettftext($this->image, 12, 0, $x+12+3, $y+12, 0x000000, default_font_path, $val); 
  $x += 12 + $space + (strlen($val)*8) + $space; 
  if ($x + (strlen($val)*8) >= $this->_w - $this->_paddingleft - $this->_paddingright) 
  { 
  $x = $x1 + 3; 
  $y = $y + 12 + 3; 
  } 
 } 
 } 
  
 public function resetpaddingbottom() 
 { 
 $ruler_w = $this->_w - $this->_paddingleft - $this->_paddingright; 
 $label_w = $this->getlabletotalwidth(); 
 $lines = ceil($label_w / $ruler_w); 
 $h = 12 * $lines + (3 * ($lines + 1)) + 30; 
 return $h; 
 } 
  
 public function resetheight() 
 { 
 $padding_bottom = $this->resetpaddingbottom(); 
 if ($this->_h - $padding_bottom < 222) 
 { 
  return 222 + $padding_bottom; 
 } 
 return $this->_h; 
 } 
  
  
 public function getlabletotalwidth() 
 { 
 $counts = count($this->_bartexts); 
 $space = 5; 
 $total_len = 0; 
 foreach ($this->_bartexts as $val) 
 { 
  $total_len += strlen($val); 
 } 
   
 $tx_w = ($total_len * 9) + ((12 + 3 + $space) * $counts); 
 return $tx_w; 
 } 
  
 public function setbg($color) 
 { 
 $this->_bgcolor = $color; 
 } 
  
 public function settitle($title) 
 { 
 $this->_title = $title; 
 } 
  
 public function setcolor() 
 { 
 $ar = array('ff', '00', '33', '66', '99', 'cc'); 
 $color = array(); 
 for ($i=0; $i<6; $i++) 
 { 
  for ($j=0; $j<6; $j++) 
  { 
  for($k=0; $k<6; $k++) 
  { 
   $color[] = $ar[$i] . $ar[$j] . $ar[$k]; 
  } 
  } 
 } 
   
 $color2 = array(); 
 for ($i=1; $i<216; $i += 4) 
 { 
  $color2[] = $color[$i]; 
 } 
  
 return $color2; 
 } 
  
 public function getrgb($color) 
 { 
 $ar = array(); 
 $color = hexdec($color); 
 $ar['r'] = ($color>>16) & 0xff; 
 $ar['g'] = ($color>>8) & 0xff; 
 $ar['b'] = ($color) & 0xff; 
 return $ar; 
 } 
} 
  
/***/ 
$bar = new bar(500, 300, array(600, 300, 30, 500, 400, 250, 350, 360), array('aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeeee', 'ffffff', 'ggggggg', 'hhhhhhhhh')); 
$bar->settitle('打造完美柱状图!'); 
$bar->stroke(); 
/***/ 
?>

以上所述就是本文的全部内容了,希望大家能够喜欢。