php生成柱状图
设计思路
1.还是要首先确定纵轴的刻度值,确定纵轴的刻度最大值 2.然后根据得到的数据个数确定图像的宽度,这时就可以创建图像了 3.计算每个色柱的高度,用高度可以计算出色柱的填充范围 4.用直线画出坐标轴,标注刻度值 5.用矩形填充色柱,并在色柱上方标注数据值 6.用Html方式画出需要的横轴坐标名称
实现过程
<?
$kuan
=30;
$jiange
=20;
$zuo
=20;
$you
=20;
$shang
=20;
$xia
=10;
$zuidashujuzhi
=1;
if
(
$_GET
[
"a"
]==
""
)
die
(
"error id:0"
);
$shuju
=split(
","
,
$_GET
[
"a"
]);
for
(
$i
=0;
$i
<
count
(
$shuju
);
$i
++){
if
(!
is_numeric
(
$shuju
[
$i
]))
die
(
"error id:1"
);
if
(
$shuju
[
$i
]>
$zuidashujuzhi
)
$zuidashujuzhi
=
$shuju
[
$i
];
}
$img_kuan
=
$zuo
+
$you
+
$jiange
+
count
(
$shuju
)*(
$kuan
+
$jiange
);
$img_gao
=170;
$zhugaodu
=
array
();
$image
= imagecreate(
$img_kuan
,
$img_gao
);
$white
= imagecolorallocate(
$image
, 0xEE, 0xEE, 0xEE);
$shuju_yanse
=
array
(
imagecolorallocate(
$image
, 0x97, 0xbd, 0x00),
imagecolorallocate(
$image
, 0x00, 0x99, 0x00),
imagecolorallocate(
$image
, 0xcc, 0x33, 0x00),
imagecolorallocate(
$image
, 0xff, 0xcc, 0x00),
imagecolorallocate(
$image
, 0x33, 0x66, 0xcc),
imagecolorallocate(
$image
, 0x33, 0xcc, 0x33),
imagecolorallocate(
$image
, 0xff, 0x99, 0x33),
imagecolorallocate(
$image
, 0xcc, 0xcc, 0x99),
imagecolorallocate(
$image
, 0x99, 0xcc, 0x66),
imagecolorallocate(
$image
, 0x66, 0xff, 0x99)
);
$zuobiao_yanse
= imagecolorallocate(
$image
, 0x00, 0x00, 0x00);
imageline (
$image
,
$zuo
,
$img_gao
-
$xia
,
$img_kuan
-
$you
/2,
$img_gao
-
$xia
,
$zuobiao_yanse
);
imageline (
$image
,
$zuo
,
$shang
/2,
$zuo
,
$img_gao
-
$xia
,
$zuobiao_yanse
);
imageline (
$image
,
$zuo
,
$shang
,
$zuo
+6,
$shang
,
$zuobiao_yanse
);
imagestring (
$image
, 3,
$zuo
/4,
$shang
,
round
(
$zuidashujuzhi
),
$zuobiao_yanse
);
imageline (
$image
,
$zuo
,
$shang
+(
$img_gao
-
$shang
-
$xia
)*1/4,
$zuo
+6,
round
(
$shang
+(
$img_gao
-
$shang
-
$xia
)*1/4),
$zuobiao_yanse
);
imagestring (
$image
, 3,
$zuo
/4,
$shang
+(
$img_gao
-
$shang
-
$xia
)*1/4,
round
(
$zuidashujuzhi
*3/4),
$zuobiao_yanse
);
imageline (
$image
,
$zuo
,
$shang
+(
$img_gao
-
$shang
-
$xia
)*2/4,
$zuo
+6,
$shang
+(
$img_gao
-
$shang
-
$xia
)*2/4,
$zuobiao_yanse
);
imagestring (
$image
, 3,
$zuo
/4,
$shang
+(
$img_gao
-
$shang
-
$xia
)*2/4,
round
(
$zuidashujuzhi
*2/4),
$zuobiao_yanse
);
imageline (
$image
,
$zuo
,
$shang
+(
$img_gao
-
$shang
-
$xia
)*3/4,
$zuo
+6,
$shang
+(
$img_gao
-
$shang
-
$xia
)*3/4,
$zuobiao_yanse
);
imagestring (
$image
, 3,
$zuo
/4,
$shang
+(
$img_gao
-
$shang
-
$xia
)*3/4,
round
(
$zuidashujuzhi
*1/4),
$zuobiao_yanse
);
for
(
$i
=0;
$i
<
count
(
$shuju
);
$i
++){
array_push
(
$zhugaodu
,
round
((
$img_gao
-
$shang
-
$xia
)*
$shuju
[
$i
]/
$zuidashujuzhi
));
}
$shuju_yanse_int
=0;
for
(
$i
=0;
$i
<
count
(
$shuju
);
$i
++){
imagefilledrectangle(
$image
,
$zuo
+
$jiange
+
$i
*(
$kuan
+
$jiange
),
$shang
+(
$img_gao
-
$shang
-
$xia
)-
$zhugaodu
[
$i
],
$zuo
+
$jiange
+
$i
*(
$kuan
+
$jiange
)+
$kuan
,(
$img_gao
-
$xia
)-1 ,
$shuju_yanse
[
$shuju_yanse_int
]);
if
(
$shuju_yanse_int
==9){
$shuju_yanse_int
=0;
}
else
{
$shuju_yanse_int
++;
}
}
for
(
$i
=0;
$i
<
count
(
$shuju
);
$i
++){
imagestring (
$image
, 1,
$zuo
+
$jiange
+
$i
*(
$kuan
+
$jiange
)+2,
$shang
+(
$img_gao
-
$shang
-
$xia
)-
$zhugaodu
[
$i
]-10,
$shuju
[
$i
],
$zuobiao_yanse
);
}
header('Content-type: image/png');
imagepng(
$image
);
imagedestroy(
$image
);
?>
使用方法
在需要显示图像的位置插入如下代码
<img src=
"zhu_img.php?a=5.4,2,30.2,4,0,6,7.7,3.8,2,3,4"
/>
其中a的值由你自己计算得出
a的文本格式是由“,”连接的若干个数据的字符串,get方式传入。
同样使用一个html解决方案,解决横轴刻度名称的问题:
根据数据个数的不同,动态生成一个表格放置横轴坐标刻度名称就行了,像这样
<table width=
"550"
border=
"0"
cellspacing=
"0"
cellpadding=
"0"
>
<tr align=
"center"
>
<?
for
(
$i
=0;
$i
<12;
$i
++) {
echo
"<td width="
50
">"
.
$i
.
"月</td>"
;
}
?>
</tr>
</table>