魏长东

weichangdong

php生成折线图

之前想用两组数据,一组是日期,另一组是这个日期对应的数值用excel生成一个折线图,折腾半天没搞定,最后用PHP实现了。

效果图,还是很漂亮滴。

php-line.jpg

代码如下(拿别人的代码改的,注释了好多,我觉得不必要的,或者没有完善的)

<?php
/**
*  php折线图示例代码
*/
  $data = array(200,19450,14650,37900,26600,19200,100,15650,33350,28500,6400);
  $month= array('2014-10-09','2014-10-10','2014-10-11','2014-10-27','2014-10-28','2014-10-29','2014-10-30','2014-10-31','2014-11-03','2014-11-04','2014-11-05');
  //$rdata = array(0.1,0.2,6,0.4,0.5,0.6,0.7,2,0.9,1,1.1,1.2);

  if (count($data) != count($month)) {
	exit('data not equl');
  }
  $max=max($data);   //获取销售额的最大值
  //$rmax=max($rdata);  //获取利润的最大值
  
  $im = imagecreate(1200,300); //创建画布 
  $green = imagecolorallocate($im,21,99,101);//设置颜色值
  $black = imagecolorallocate($im,0,0,0);   
  $red = imagecolorallocate($im,255,0,0);
  $blue = imagecolorallocate($im,0,0,255);
//imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
  imageline($im,20,230,1100,230, $blue ); //设置X轴横坐标
  imageline($im,20,20,20,230, $blue );  //设置Y轴纵坐标
  imagestring($im,3,1120,222,"X",$black); //输出字符X
  imagestring($im,3,16,1,"Y",$black); //输出字符Y
   
  $num = count($data);
  
  /*
  $l=190;
  $k1=20;
  $k2=500; 
     for($j=0;$j<$num;$j++){
     imageline($im,$k1,$l,$k2,$l,$black); //设置X轴网格线横坐标
     $l=$l-40;  
  }

  $f=60; 
  $z1=30;
  $z2=230;
     for($j=0;$j<$num;$j++){
     imageline($im,$f,$z1,$f,$z2,$black);  //设置Y轴网格线纵坐标
     $f=$f+40; 
   } 
  */
  
  //输出Y轴坐标值
  $a=max($data);
  //$b=max($rdata);
  $c=$a;
  $l=185;
     for($j=0;$j<5;$j++){
      imagestring($im,2,2,$l,floor($c/5*($j+1)),$red);
      $l=$l-40;  
   } 
   
     $x = 50;
     $y = 230;
 	 
for ($i=1;$i<$num;$i++){
	  $y_lt = 230-(($data[$i-1]/$max)*200); //设置商品销售额所在的纵坐标值
	  $y_ht = 230-(($data[$i]/$max)* 200); //设置商品销售额所在的纵坐标值
	  imageline($im,$x*($i*2-1)+20,$y_lt,$x*(($i+1)*2-1)+20,$y_ht,$red); //绘制销售额折线图
	  imagefilledellipse($im,$x*(($i+1)*2-1)+20,$y_ht,10,10,$red);
	 // imagesetpixel($im,$x*(($i+1)*2-1)+20,$y_ht-25,$black);
  }  
  
  for ($i=1;$i<$num+1;$i++){ 
      imagestring( $im,2,$x*($i-1)*2+30,$y+11,$month[$i-1],$black);   //输出月份的值
      imagestring( $im,2,$x*($i-1)*2+60,$y+25,$data[$i-1],$red); //输出商品销售额的值
     // imagestring( $im,2,$x*($i-1)*2+30,$y+40,$rdata[$i-1],$blue);    //输出销售利润的值
}    
  $fontpath=$_SERVER["SystemRoot"]."\\fonts\\simhei.ttf"; //设置字库路径  
  //$str=iconv("gb2312","UTF-8","中国人民123");
  /*
  $str="hello";
  $str=str_repeat($str,1); 
  imagettftext($im,25,-45,80,20,$blue,$fontpath,$str); 
  imagettftext($im,15,30,180,190,$red,$fontpath,$str);
*/

/*
   $x1 = 20;
   $y1 = 230;
   for ($r=1;$r<12;$r++){
   $y_lt1 = 230-(($rdata[$r-1]/$max)* 200); //设置商品销售利润所在的纵坐标值
   $y_ht1 = 230-(($rdata[$r]/$max)*200); //设置商品销售利润所在的纵坐标值
   imageline($im,$x1*($r*2-1)+20,$y_lt1,$x*(($r+1)*2-1)+20,$y_ht1,$blue); //绘制销售利润折线图
} 
 */
   /*
   绘制图片生成时间
  list($usec, $sec) = explode(" ", microtime());
  $ss=(string)((float)$usec + (float)$sec);
  $ss2=((string)$sec).(substr((string)$usec,1,strlen((string)$usec)-1));
  //echo $ss2;echo "<br>";
  imagestring($im,8,200,10,$ss,$black);  
  imagestring($im,8,200,40,$ss2,$black);
  */
  
  $str=iconv("gb2312","UTF-8","9");
  $str='swordman';
  imagettftext($im,50,0,180,190,$red,$fontpath,$str); 
      
  header("Content-type: image/png"); 
  echo(imagepng($im)); 
  imagedestroy($im); //释放图像资源
?>