PHP的GD2函数创建折线图源码示例
代码来自 codego.net/tags/4/1/
<?php
$img_height=300; //画布的高度
$img_width=0; //画布宽度
$img_space=27; //刻度之间的间隔
$left=30; //左侧留出的宽度
$right=30; //右侧留出的宽度
$up=30; //上面留出的高度
$down=30; //下面留出的高度
$max=1; //最大数据值
$p_x = array(); //数据的x轴坐标
$p_y = array(); //数据的y轴坐标
for($i=1;$i<=32;$i++){ //获取月份的值
$month[]=$i;
}
//月份的变量
if ($lmbs=="") die("error id:0");
$data=split(",",$lmbs); //按照","分隔字符串 $data为获取的数据变量
for($i=0;$i if(!is_numeric($data[$i])) die("error id:1");
if($data[$i]>$max) $max=$data[$i]; }
$img_width=$left+$right+count($data)*$img_space; //画布的真正宽度
$image = imagecreate($img_width,$img_height); //创建画布
$white = imagecolorallocate($image, 0xEE, 0xEE, 0xEE); //匹配颜色
$left_color = imagecolorallocate($image, 0x00, 0x00, 0x00);
$down_color = imagecolorallocate($image, 0x00, 0x00, 0xFF);
imageline ( $image, $left, $img_height-$down, $img_width-$right/2, $img_height-$down, $left_color); //x轴坐标
imageline ( $image, $left, $up/2, $left, $img_height-$down, $left_color); //y轴坐标
//描述出数据在坐标上的点
for($i=0;$i array_push ($p_x, $left+$i*$img_space);
array_push ($p_y, $up+round(($img_height-$up-$down)*(1-$data[$i]/$max))); }
//输出y轴最高点的值
imageline ( $image, $left, $up, $left+6, $up, $left_color);
imagestring ( $image, 3, $left/4, $up,$max, $left_color);
//输出y轴3/4高度的值
imageline ( $image, $left, $up+($img_height-$up-$down)*1/4, $left+6, $up+($img_height-$up-$down)*1/4, $left_color);
imagestring ( $image, 3, $left/4, $up+($img_height-$up-$down)*1/4,$max*3/4, $left_color);
//输出y轴2/4高度的值
imageline ( $image, $left, $up+($img_height-$up-$down)*2/4, $left+6, $up+($img_height-$up-$down)*2/4, $left_color);
imagestring ( $image, 3, $left/4, $up+($img_height-$up-$down)*2/4,$max*2/4, $left_color);
//输出y轴1/4高度的值
imageline ( $image, $left, $up+($img_height-$up-$down)*3/4, $left+6, $up+($img_height-$up-$down)*3/4, $left_color);
imagestring ( $image, 3, $left/4, $up+($img_height-$up-$down)*3/4,$max*1/4, $left_color);
//输出x轴的刻度
for($i=0;$i imageline ( $image, $left+$i*$img_space, $img_height-$down, $left+$i*$img_space, $img_height-$down-6, $left_color);
imagestring ( $image, 2, $left+$i*$img_space-$img_space/4, $up+($img_height-$up-$down)+2,$month[$i], $left_color); }
$data_color_int=0;
//为每个点的坐标连线
for($i=0;$i if($i+1<>count($data)){
imageline ( $image, $p_x[$i], $p_y[$i], $p_x[$i+1], $p_y[$i+1], $down_color);
imagefilledrectangle($image, $p_x[$i]-1, $p_y[$i]-1, $p_x[$i]+1, $p_y[$i]+1, $down_color); }
}
imagefilledrectangle($image, $p_x[count($data)-1]-1, $p_y[count($data)-1]-1, $p_x[count($data)-1]+1, $p_y[count($data)-1]+1, $down_color);
//输出每个坐标点的数据值
for($i=0;$i imagestring ( $image, 3, $p_x[$i]+4, $p_y[$i]-12,$data[$i], $left_color); }
header('Content-type: image/png');
imagepng($image);
imagedestroy($image); //释放图像资源
?>
代码来自 codego.net/tags/4/1/
<?php
$img_height=300; //画布的高度
$img_width=0; //画布宽度
$img_space=27; //刻度之间的间隔
$left=30; //左侧留出的宽度
$right=30; //右侧留出的宽度
$up=30; //上面留出的高度
$down=30; //下面留出的高度
$max=1; //最大数据值
$p_x = array(); //数据的x轴坐标
$p_y = array(); //数据的y轴坐标
for($i=1;$i<=32;$i++){ //获取月份的值
$month[]=$i;
}
//月份的变量
if ($lmbs=="") die("error id:0");
$data=split(",",$lmbs); //按照","分隔字符串 $data为获取的数据变量
for($i=0;$i if(!is_numeric($data[$i])) die("error id:1");
if($data[$i]>$max) $max=$data[$i]; }
$img_width=$left+$right+count($data)*$img_space; //画布的真正宽度
$image = imagecreate($img_width,$img_height); //创建画布
$white = imagecolorallocate($image, 0xEE, 0xEE, 0xEE); //匹配颜色
$left_color = imagecolorallocate($image, 0x00, 0x00, 0x00);
$down_color = imagecolorallocate($image, 0x00, 0x00, 0xFF);
imageline ( $image, $left, $img_height-$down, $img_width-$right/2, $img_height-$down, $left_color); //x轴坐标
imageline ( $image, $left, $up/2, $left, $img_height-$down, $left_color); //y轴坐标
//描述出数据在坐标上的点
for($i=0;$i array_push ($p_x, $left+$i*$img_space);
array_push ($p_y, $up+round(($img_height-$up-$down)*(1-$data[$i]/$max))); }
//输出y轴最高点的值
imageline ( $image, $left, $up, $left+6, $up, $left_color);
imagestring ( $image, 3, $left/4, $up,$max, $left_color);
//输出y轴3/4高度的值
imageline ( $image, $left, $up+($img_height-$up-$down)*1/4, $left+6, $up+($img_height-$up-$down)*1/4, $left_color);
imagestring ( $image, 3, $left/4, $up+($img_height-$up-$down)*1/4,$max*3/4, $left_color);
//输出y轴2/4高度的值
imageline ( $image, $left, $up+($img_height-$up-$down)*2/4, $left+6, $up+($img_height-$up-$down)*2/4, $left_color);
imagestring ( $image, 3, $left/4, $up+($img_height-$up-$down)*2/4,$max*2/4, $left_color);
//输出y轴1/4高度的值
imageline ( $image, $left, $up+($img_height-$up-$down)*3/4, $left+6, $up+($img_height-$up-$down)*3/4, $left_color);
imagestring ( $image, 3, $left/4, $up+($img_height-$up-$down)*3/4,$max*1/4, $left_color);
//输出x轴的刻度
for($i=0;$i imageline ( $image, $left+$i*$img_space, $img_height-$down, $left+$i*$img_space, $img_height-$down-6, $left_color);
imagestring ( $image, 2, $left+$i*$img_space-$img_space/4, $up+($img_height-$up-$down)+2,$month[$i], $left_color); }
$data_color_int=0;
//为每个点的坐标连线
for($i=0;$i if($i+1<>count($data)){
imageline ( $image, $p_x[$i], $p_y[$i], $p_x[$i+1], $p_y[$i+1], $down_color);
imagefilledrectangle($image, $p_x[$i]-1, $p_y[$i]-1, $p_x[$i]+1, $p_y[$i]+1, $down_color); }
}
imagefilledrectangle($image, $p_x[count($data)-1]-1, $p_y[count($data)-1]-1, $p_x[count($data)-1]+1, $p_y[count($data)-1]+1, $down_color);
//输出每个坐标点的数据值
for($i=0;$i imagestring ( $image, 3, $p_x[$i]+4, $p_y[$i]-12,$data[$i], $left_color); }
header('Content-type: image/png');
imagepng($image);
imagedestroy($image); //释放图像资源
?>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29974986/viewspace-1321050/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29974986/viewspace-1321050/