龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php实现chart 柱形图表代码

时间:2014-06-27 02:08来源:网络整理 作者:网络 点击:
分享到:
php实现chart 柱形图表代码 ```phplt;?php /* Copyright (C) 1998 Afan Ottenheimer, afan@jeonet.com 这个程序可无条件和在GNU GPL条款下传播的。 在以下情况下,你必须要有我们的许可证才能使用该程序:如
<?php 
/* Copyright (C) 1998  Afan Ottenheimer, afan@jeonet.com  

    这个程序可无条件和在GNU GPL条款下传播的。

    在以下情况下,你必须要有我们的许可证才能使用该程序:如果该程序是用于某个商业公司,
或是被用来买卖,或是提供给Microsoft的产品。

    你可以从 GNU GPL 的这个网址获取该程序的拷贝: http://www.gnu.org/copyleft/gpl.html   
*/ 

/* 一些有关该程序的说明:  
    它做了一个二维的Chart(图表),其中的X轴是标签轴,而Y轴则是数字轴。
    Y轴用到了一个叫n_max_y方法,该用于把数据转换成图表。
    X轴没有这种转换。每个X轴上的刻度可以由xlab[N]得到,这里N是一个整数。
*/ 

/*    4/14/2000    Mouse Chen( litmouse@km169.net   )    Translate it to Chinese   */

/* 读入数据*/ 
$maxy = $maxy_in; 
if ($maxy == 0) { 
  echo  "Error Max Y = 0 <p>"; 
  exit; 
} 
SetType($maxy, "double"); 

/* X轴是由一个叫 $maxx的变量分割成从 0 到 $maxx-1的部分的 */ 
$maxx = $maxx_in; 
if ($maxx <= 0) { 
  echo  "error maxx <= 0 <p>"; 
  exit; 
} 
SetType($maxx, "double"); 

$XSIZE = $XSIZE_in;   /*图片最大的宽度(单位:象素) */ 
SetType($XSIZE, "double"); 

$YSIZE = $YSIZE_in;  /*图片最大的高度(单位:象素) */ 
SetType($YSIZE, "double"); 

$num_vert_ticks = $num_vert_ticks_in; 
if ($num_vert_ticks <= 0) { 
  echo  "error num_vert_ticks <= 0 <p>"; 
  exit; 
} 
SetType($num_vert_ticks, "double"); 

$y_title = $y_title_in; 
$x_title = $x_title_in; 
/* *********读入数据完成********** */ 

/* 创建一个文件类型头 */ 
Header( "Content-Type: image/gif");  

$x_tot_margin = 100.0; 
SetType($x_tot_margin, "double"); 
$x_left_margin = 77.0;  /* 在X轴上左边的空白距离(单位:象素)*/ 
SetType($x_left_margin, "double"); 

$xscale = ($XSIZE - $x_tot_margin)/$maxx ;  
SetType($xscale, "double"); 

$y_top_margin = 14.0;  /* 必须是整型数 */ 
SetType($y_top_margin, "double"); 
$y_tot_margin = 80.0;  /* 必须是整型数 */ 
SetType($y_tot_margin, "double"); 
$yscale = ($YSIZE - $y_tot_margin)/$maxy;  /* 屏幕最大的高度 */ 
SetType($yscale, "double"); 
$y_bot_margin = $y_tot_margin - $y_top_margin; 
SetType($y_bot_margin, "double"); 

/* Y轴是由一个叫 $maxy变量分割成的 */ 
$y_tick_delta = $maxy / $num_vert_ticks; 
SetType($y_tick_delta, "double"); 

$si_units[0] =  "";   
$si_units[1] =  "k"; 
$si_units[2] =  "M"; 
$si_units[3] =  "G"; 
$si_units[4] =  "T"; 
$small_font = 2;  /* 字体大小:fonts = 1,2,3,4 或 5 */ 
$small_font_width = 6.0;  
$small_font_height = 10.0;
$tiny_font = 1; 
$c_blank[0]  =  "245"; 
$c_blank[1]  =  "245"; 
$c_blank[2]  =  "245"; 
$c_light[0] =  "194"; 
$c_light[1] =  "194"; 
$c_light[2] =  "194"; 
$c_dark[0] =  "100"; 
$c_dark[1] =  "100"; 
$c_dark[2] =  "100"; 
$c_major[0] =  "255"; 
$c_major[1] =  "0"; 
$c_major[2] =  "0"; 
$c_grid[0] =  "0"; 
$c_grid[1] =  "0"; 
$c_grid[2] =  "0"; 
$col_in[0] =  "0"; 
$col_in[1] =  "235"; 
$col_in[2] =  "12"; 
$col_inm[0] =  "0"; 
$col_inm[1] =  "166"; 
$col_inm[2] =  "33"; 
$col_out[0] =  "0"; 
$col_out[1] =  "94"; 
$col_out[2] =  "255"; 
$col_outm[0] =  "255"; 
$col_outm[1] =  "0"; 
$col_outm[2] =  "255"; 

     /* ################################################# */ 
     /* 图表的一些普通的定义*/ 
$XSIZE = (($maxx*$xscale)+100); 

     /* 图表的位置 */ 
     /* 把 x/y 的 coord 转换成 象素的 coord */ 
old_function ytr $y ( 
  SetType($y, "double"); 
  global $maxy,$yscale,$y_top_margin; 
  return ($maxy*$yscale+$y_top_margin-(($y)*$yscale)); 
); 
old_function xtr $x ( 
  SetType($x, "double"); 
  global $maxx,$xscale,$growright,$x_left_margin; 
  if ($growright) { 
   $tmpret = ($maxx*$xscale+$x_left_margin-(($x)*$xscale)) ; 
  } else { 
    $tmpret =  ($x_left_margin+(($x)*$xscale)); 
  } 
  return($tmpret); 
); 

    $graph = ImageCreate($XSIZE, $YSIZE); 
/* 要能正确让 brush_out和 brush_outm 为 ImageSetBrush 而使用,必须是GD 第三版 */ 
/*  
    $brush_out = ImageCreate(1,2); 
    $brush_outm = ImageCreate(1,2); 
    $i_outm = ImageColorAllocate($brush_outm,$col_outm[0], $col_outm[1], $col_outm[2]);       
    $i_out = ImageColorAllocate($brush_out,$col_out[0], $col_out[1], $col_out[2]); 
*/ 

     /* 先分配一个背景色. */ 
    $i_blank = ImageColorAllocate($graph,$c_blank[0],$c_blank[1],$c_blank[2]); 
    $i_light = ImageColorAllocate($graph,$c_light[0],$c_light[1],$c_light[2]); 
    $i_dark  = ImageColorAllocate($graph,$c_dark[0],$c_dark[1],$c_dark[2]); 

     /* ImageColorTransparent($graph, $i_blank); */ 
     /* ImageInterlace($graph, 1); */ 

    $i_grid = ImageColorAllocate($graph,$c_grid[0],$c_grid[1],$c_grid[2] ); 
    $i_major = ImageColorAllocate($graph,$c_major[0],$c_major_g,$c_major_b); 

    $i_in = ImageColorAllocate($graph,$col_in[0], $col_in[1], $col_in[2]); 
    $i_inm = ImageColorAllocate($graph,$col_inm[0], $col_inm[1], $col_inm[2]); 

     /* 为图片画个边框 */ 
    ImageLine($graph,0,0,$XSIZE-1,0,$i_light); 
    ImageLine($graph,1,1,$XSIZE-2,1,$i_light); 
    ImageLine($graph,0,0,0,$YSIZE-1,$i_light); 
    ImageLine($graph,1,1,1,$YSIZE-2,$i_light); 
    ImageLine($graph,$XSIZE-1,0,$XSIZE-1,$YSIZE-1,$i_dark); 
    ImageLine($graph,0,$YSIZE-1,$XSIZE-1,$YSIZE-1,$i_dark); 
    ImageLine($graph,$XSIZE-2,1,$XSIZE-2,$YSIZE-2,$i_dark); 
    ImageLine($graph,1,$YSIZE-2,$XSIZE-2,$YSIZE-2,$i_dark);                        

   /*为图表画个边框 */ 

    ImageRectangle($graph, xtr(0),ytr(0),xtr($maxx),ytr($maxy),$i_grid); 

     /*为网格线创建一个点的类型*/ 
/* 该类型不被 PHP/FI2所支持 ??? 
    $styleDotted[0] = $i_grid; 
    $styleDotted[1] = Transparent; 
    $styleDotted[2] = Transparent; 
    ImageSetStyle($graph, $styleDotted, 3);                                     
*/ 

     /* 画水平的网格线 */ 

    ImageStringUp($graph, $small_font,8, ($small_font_width*strlen($y_title)/2.0) + ytr($maxy/2.0), $y_title, $i_grid); 
    ImageString($graph, $small_font, -($small_font_width*strlen($x_title)/2.0) + xtr($maxx/2.0) , ($YSIZE - 3.0*$small_font_height), $x_title, $i_grid); 

    $i = 0; 
/* 画垂直的刻度 */ 
    while ($i<=$num_vert_ticks){ 

        $y_tmp = (DoubleVal($i)*$maxy/$num_vert_ticks); 
        SetType($y_tmp, "double"); 

        ImageLine($graph,(-10+xtr(0)),ytr($y_tmp),xtr(1),ytr($y_tmp),$i_grid); 

        ImageLine($graph,(xtr($maxx)+10),ytr($y_tmp),xtr($maxx-1),ytr($y_tmp),$i_grid); 

/* gdStyled 函数只被 GD v3 所支持*/ 
/*ImageLine($graph,xtr(0),ytr($y_tmp),xtr($maxx),ytr($y_tmp),gdStyled); */ 
        ImageLine($graph,xtr(0),ytr($y_tmp),xtr($maxx),ytr($y_tmp),$i_light); 

        $ylab = sprintf( "%6.1f %s",$y_tick_delta*$i,$si_units[0]); 

        ImageString($graph, $small_font,($x_left_margin-54),( -($small_font_height/2.0) + ytr($y_tmp)),$ylab, $i_grid); 
        $i++; 
    }  

     /* 画垂直的网格线和水平的标签*/ 
    $x = 0; 
    while ($x<$maxx) { 
      if ($x_label[$x]) { 
        ImageString($graph, $small_font,( -(strlen($x_label[$x]) * $small_font_width / 2.0) + xtr(0.5+$x)),  ($YSIZE - $y_bot_margin + 1.5*$small_font_height) ,$x_label[$x], $i_grid); 
      } 
      if ($y_data[$x] ) {  /* 画柱形 */ 
        ImageFilledRectangle($graph, xtr(0.25+$x), ytr($y_data[$x]), xtr(0.75+$x), ytr(0), $i_major); 
      } 
/* 以下是调试程序时的语句 */ 
/* 
$tempmid = "mid"; 
$tempajo =  $x; 
        ImageString($graph, $small_font, xtr($x), ytr(-1), $tempajo, $i_major); 
        ImageString($graph, $small_font, (-((strlen($tempmid))* $small_font_width /2.0) + xtr((.5+$x)) ), ytr(-1), $tempmid, $i_major);  
*/ 
/* 调试结束 */ 
      $x++; 
    }                                                                           

ImageGif($graph); 
ImageDestroy($graph); 
/*  
ImageDestroy($brush_out); 
ImageDestroy($brush_outm); 
*/ 
?> 
精彩图集

赞助商链接