土曜日, 11月 28, 2009

PHPで、棒グラフ

PHPで、棒グラフなど書くことができます。画像に書き出してそれをHTMLのページに埋め込むことができるということです。データベースからデータを読み出して、グラフにして表示することが、オンラインで可能です。

以下のコードを、.php ファイルに保存して、ApacheなどPHPの動く環境でブラウザでアクセスすると、t.png の画像ファイルができて棒グラフになっているはずです。




$data=array();

array_push($data,1);
array_push($data,2);
array_push($data,3);

$width=300;
$height=10+10*count($data);

$image=imagecreatetruecolor($width,$height);

$white=imagecolorallocate($image,255,255,255);
imagefilledrectangle($image,0,0,$width,$height,$white);
$black=imagecolorallocate($image,0,0,0);
$i=0;
foreach($data as $item)
{
$y0=5+10*$i;
$x1=$item*10;
imagefilledrectangle($image,0,$y0,$x1,$y0+10,$black);
$i++;
}

$filename="t.png";
imagepng($image,$filename);
imagedestroy($image);

?>



PHP: 定数を扱う

プロジェクトごとの定数を扱うクラス Config\Constants の紹介です。 <?php namespace Config; class Constants {     public const DB_USER = "linguist...