PHP 画饼形图表的类(非常好)

发布时间:2009年02月04日      浏览次数:1450 次
<?
/*
//调用实例
$data_source = array(10,30,40,20);//统计数据源
$color_data = array("#ff0000","#cccccc","#0022ff","#33ff33");//对应块的颜色值
$text_data = array("Internet Explorer","Mozilla Firefox","Opera","Netscape");//对应块的文本说明
//创建类实例
$pie = new PieGraph( $data_source, 400, 200, "1.jpg" );
$pie->setColors($color_data);
$pie->setLegends($text_data);
//$pie->DisplayCreationTime(); //是否显示生成图片所用的时间
$pie->set3dHeight(20);//设置3D效果的高度
$pie->display();//显示图形
*/
/////////////////////////////////////////////////////////////////////////////
// 画饼形图表类
/////////////////////////////////////////////////////////////////////////////
class PieGraph{
      var $pie;
var $data;
var $legends;
var $img_width;
var $img_height;
var $pie_width;
var $pie_height;
var $pie_colors;
var $pie_color_bg;
var $pie_color_text;
var $threedee_height;
      var $colors_set;
      var $pre_colors;
      var $create_img_file;
      
      //主函数,参数说明:$data数据源,$w图形宽度,$h图片高度,$create_img_file生成图片的路径及文件名
function PieGraph( $data, $w=200, $h=150, $create_img_file="" ){
            if(!function_exists("imagecreatetruecolor")){
                  die("Error. GD Library >= 2 needed.");
            }
            $this->starttime = microtime();
            $this->display_creation_time = false;
            asort($data);
$this->data             = array_reverse($data, TRUE);
            $this->pie_width       = $w;
            $this->pie_height      = $h;
            $this->create_img_file = $create_img_file;
            $this->pre_colors      = array("#DA3600", "#0F84D4", "#F9A308", "#62D038", "#FE670F", "#2C9232", "#7F0B80", "#DFDE29", "#9F9F9F", "#EDEDED", "#BAE700");
}
      function hex2rgb($hex)
      {
            $color = ereg_replace('^#','',$hex);
            $rgb = array(
                               (16 * hexdec(substr($color,0,1)) + hexdec(substr($color,1,1)) ),
             (16 * hexdec(substr($color,2,1)) + hexdec(substr($color,3,1)) ),
             (16 * hexdec(substr($color,4,1)) + hexdec(substr($color,5,1)) )
                              );
            return $rgb;
      }
      //设置颜色
      function setColors($colors, $bg="FFFFFF", $text="000000", $border="999999"){
$this->pie_colors            = ($colors)                   ? $colors                               : $this->pre_colors;
            $this->pie_color_bg            = (strlen($bg)>5)             ? $this->hex2rgb($bg)             : $this->hex2rgb("FFFFFF");
            $this->pie_color_text      = (strlen($text)>5)            ? $this->hex2rgb($text)            : $this->hex2rgb("000000");
            $this->pie_color_border      = (strlen($border)>5)      ? $this->hex2rgb($border)      : $this->hex2rgb("666666");
            $this->pie_color_text_bg      = $this->hex2rgb("F3F3F3");
            $this->colors_set = true;
      }
      function setLegends($l){
            $this->legends = $l;
      }
      function set3dHeight($tdh){
            $this->threedee_height = $tdh;
      }
      //是否显示生成图片所用的时间
      function DisplayCreationTime()
      {
            $this->display_creation_time = true;
      }
      
      //处理图形的数据
function init(){
            if(!$this->colors_set){
                  $this->setColors($this->pre_colors);
            }
$this->threedee_height = ($this->threedee_height) ? $this->threedee_height : 15; //3d效果的高度
            $this->img_width = $this->pie_width;
            $this->img_height = $this->pie_height;
            $this->init_img_width = $this->pie_width * 3;
            $this->init_img_height = $this->pie_height * 3;
            $this->init_width = ($this->pie_width) * 3;
            $this->init_height = ($this->pie_height-$this->threedee_height) * 3;
            $this->init_3d_height = $this->threedee_height * 3;
            $this->cx = round ($this->init_width/2);
            $this->cy = round ($this->init_height/2);
            $this->pie_data = array();
            $this->total = 0;
            foreach($this->data as $key => $value){
                  $this->total += $value; //计算数据总和
            }
            reset($this->data);
            $num = count($this->data)-1;
            $start = 0;
            $c=0;
            foreach($this->data as $key => $value){
                  if ( $value > 0 ){ //当数值大于0时才显示
                        $percent = $value/$this->total*100;
                        $fill = ($percent*3.6);
                        $fill = ($fill>360) ? 360 : $fill;
                        $end = round($start+$fill);
                        $end = ($end >360) ? 360 : $end;
                        if($num==$c)
                        {
                              $end = ($end < 360) ? 360 : $end;
                        }
                        $this->pie_data[$key] = array($start, $end); //划分图形块的角度数组
                        $start = $end;
                        $c++;
                  }
            }
}
function get_color($num, $mode = "normal"){
            $tmp_color = $this->hex2rgb( $this->pie_colors[$num] );
            $tmp_color_3d = array(
                  ( ($tmp_color[0] > 79) ? $tmp_color[0]-80 : 0 ),
                  ( ($tmp_color[1] > 79) ? $tmp_color[1]-80 : 0 ),
                  ( ($tmp_color[2] > 79) ? $tmp_color[2]-80 : 0 )
            );
       if($mode=="3d"){
                   return imagecolorallocate($this->pie, $tmp_color_3d[0], $tmp_color_3d[1], $tmp_color_3d[2]);
            }else{
                  return imagecolorallocate($this->pie, $tmp_color[0], $tmp_color[1], $tmp_color[2]);
            }
      }
      //显示图形
function display(){
            $this->init();
$this->pie = @imagecreatetruecolor($this->init_img_width, $this->init_img_height);
$colBG = imagecolorallocate($this->pie, $this->pie_color_bg[0], $this->pie_color_bg[1], $this->pie_color_bg[2]);
imagefill($this->pie, 0, 0, $colBG);
//显示3D效果
            $this->start_3d = $this->cy + $this->init_3d_height;
            for($i=$this->start_3d;$i > $this->cy; $i--){
                  reset($this->pie_data);
                  $c=0;
                  foreach($this->pie_data as $k => $data){
                        $col = $this->get_color($k, "3d");
                        imagefilledarc($this->pie,
                                           $this->cx, $i,
                                           $this->init_width, $this->init_height,
                                           $data[0], $data[1],
                                           $col, IMG_ARC_NOFILL);
                        $c++;
                  }
            }
            reset($this->pie_data);
            $c=0;
            foreach($this->pie_data as $k => $data){
                  $col = $this->get_color($k, "normal");
                  imagefilledarc($this->pie,
                                     $this->cx, $this->cy,
                                     $this->init_width, $this->init_height,
                                     $data[0], $data[1],
                                     $col, IMG_ARC_PIE);
                  $c++;
            }
            
            $cellpadding=5;
            $max_str=0;
            $items=0;
            foreach($this->legends as $k => $legend){
                  if(strlen($legend) > $max_str){
                        $max_str = strlen($legend);
                  }
                  $items++;
            }
            $box_with = imagefontheight(2)-5;
            $box_height = imagefontheight(2)-5;
            $leg_height = ((imagefontheight(2)+2) * $items) + ($cellpadding * 2);
            $leg_width = (imagefontwidth(2) * ($max_str+7)) + ($cellpadding * 2) +($box_with * 2);
            $leg_img = imagecreatetruecolor($leg_width, $leg_height);
            imagefill($leg_img, 0, 0, $colBG);
            
            // text color
            $colTEXT = imagecolorallocate($leg_img, $this->pie_color_text[0], $this->pie_color_text[1], $this->pie_color_text[2]);
            // text / legends backgroundcolor
            $colTEXTBG = imagecolorallocate($leg_img, $this->pie_color_text_bg[0], $this->pie_color_text_bg[1], $this->pie_color_text_bg[2]);
            // border color for the legends
            $colTEXTBO = imagecolorallocate($leg_img, $this->pie_color_border[0], $this->pie_color_border[1], $this->pie_color_border[2]);
            // the table + border for the legend
            imagefilledrectangle($leg_img, 0, 0, $leg_width, $leg_height, $colTEXTBG);
            imagerectangle($leg_img, 0, 0, $leg_width-1, $leg_height-1, $colTEXTBO);
            reset($this->data);
            $c=0;
            $lx = $box_with + $cellpadding*2;
            $ly = $cellpadding;
            foreach($this->data as $k => $data){
                  // legend text item
                  $percent = round($data/$this->total*100, 2);
                  $text = $this->legends[$k]." ".$percent."%";
                  $col = $this->get_color($k, "normal");
                  imagefilledrectangle($leg_img, $cellpadding, $ly+2, $cellpadding+$box_with, $ly+$box_height+2, $col);
                  imagerectangle($leg_img, $cellpadding, $ly+2, $cellpadding+$box_with, $ly+$box_height+2, $colTEXTBO);
                  imagestring($leg_img, 2, $lx, $ly, $text, $colTEXT);
                  $ly += (2 + imagefontheight(2));
                  $c++;
            }
            // final setups an image creation
            $pie_width = ImageSX($this->pie);
            $pie_height = ImageSY($this->pie);
            if($this->img_height < $leg_height)
            {
                  $this->img_height = $leg_height;
            }
            $img = imagecreatetruecolor($this->img_width+$leg_width+$cellpadding, $this->img_height);
            $ly += (2 + imagefontheight(2));
            imagefill($img, 0, 0, $colBG);
            imagecopyresampled($img, $this->pie,
                                     0, 0,
                                     0, 0,
                                     $this->img_width, $this->img_height,
                                     $pie_width, $pie_height);
            imagecopyresampled($img, $leg_img,
                                     $this->img_width+$cellpadding, 0,
                                     0, 0,
                                     $leg_width, $leg_height,
                                     $leg_width, $leg_height);
            // 显示生成图片所用的时间
            if($this->display_creation_time){
                  $this->endtime = microtime();
                  list($susec, $ssec) = explode(" ",$this->starttime);
                  $this->starttime = $susec+$ssec;
                  list($eusec, $esec) = explode(" ",$this->endtime);
                  $this->endtime = $eusec+$esec;
                  $time = round ($this->endtime-$this->starttime, 2);
                  $time_text = "creation time: ".$time." sec";
                  imagestring($img, 2, ($this->pie_width+$cellpadding), ($this->img_height-imagefontheight(5)), $time_text, $colTEXT);
            }
            
            //生成图片----------------------------------------
            header('Content-type: image/jpeg');
            //$create_img_file = ""; //生成图片的路径及文件名(如:1.jpg),如果省略则直接输出显示
            $create_img_quality = 100; //生成图片的质量,值为:0到100
            //如果有文件名,则生成图片到指定的路径
            if ( $this->create_img_file <> "" ) imagejpeg($img, $this->create_img_file, $create_img_quality);
            //显示图片
            imagejpeg($img, "", $create_img_quality);
            imagedestroy($this->pie);
            imagedestroy($leg_img);
            imagedestroy($img);
}
}
?>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!