ホームページ  >  記事  >  バックエンド開発  >  PHP で円グラフを生成する 3 次元円グラフ_PHP チュートリアル

PHP で円グラフを生成する 3 次元円グラフ_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:43:58894ブラウズ

円グラフ

コードをコピーします コードは次のとおりです:

//+---------------------- --+
//| pie3dfun.PHP//パブリック関数|
//+----------------------+
define("ANGLE_STEP" , 3); //楕円弧を描画する際の角度ステップを定義します
define("FONT_USED", "C:WINDOWSFontssimhei.ttf") //使用するフォントファイルの場所
functiondraw_getdarkcolor($img,$clr); // $clr に対応する暗い色を検索します
{
$rgb = imagecolorsforindex($img,$clr)
return array($rgb["red"]/2,$rgb["green"]/2,$rgb[ "blue"]/ 2);
}
functiondraw_getexy($a, $b, $d) //角度 $d に対応する楕円上の点座標を見つけます
{
$d = deg2rad($d); return array(round($ a*Cos($d)),round($b*Sin($d))); sd,$ed,$ clr) //楕円弧関数
{
$n = ceil(($ed-$sd)/ANGLE_STEP)
$d = $sd) =draw_getexy; ($a,$b ,$d);
for($i=0; $i<$n; $i++)
{
$d = ($d+ANGLE_STEP)>$ed?$ed:($ d+ANGLE_STEP);
list($x, $y) =draw_getexy($a, $b, $d);
imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+ $oy, $clr) ;
$x0 = $x;
}
}
関数draw_sector($a, $b, $sd, $clr) // セクターの描画
{
$n = ceil(($ed-$sd)/ANGLE_STEP)
list($x0,$y0) =draw_getexy($a, $b; , $d);
imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
for($i<$n; $i++);
$d = ($ d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
list($x, $y) =draw_getexy($a, $b, $d); $img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
$x0 = $x
}
imageline($img, $; x0+$ox, $y0+ $oy, $ox, $oy, $clr);
list($x, $y) =draw_getexy($a/2, $b/2, ($d+$sd)/2) ;
imagefill($x+$ox, $y+$oy, $clr);
}
関数draw_sector3d($a, $b, $v, $sd, $ed , $clr) // 3D セクター
{
draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);
if($sd<180)
{
list ($R, $G , $B) =draw_getdarkcolor($img, $clr);
$clr=imagecolorallocate($img, $R, $G, $B); $ed = 180;
リスト($ sx, $sy) =draw_getexy($a,$b,$sd);
$sy += $oy; draw_getexy($a, $b, $ed);
$ex += $ox;
imageline($img, $sx, $sy, $sx, $sy+$v, $clr; );
imageline($img, $ex, $ey, $ex, $ey+$v, $clr);
draw_arc($img, $ox, $oy+$v, $a, $b, $sd, $ ed, $clr);
list($sx, $sy) =draw_getexy($a, $b, ($sd+$ed)/2); = $ox;
imagefill ($img, $sx, $sy, $clr);
}
}
functiondraw_getindexcolor($img, $clr) //RGB をインデックスカラーにします
{
$R = ($clr> >16) & 0xff ;
$G = ($clr>8)& 0xff;
return imagecolorallocate($img, $R, $G, $B);
}
// Main 関数を描画し、画像を出力します
// $datLst はデータ配列、$datLst はラベル配列、$datLst はカラー配列です
// 上記 3 つの配列の次元は等しい必要があります
function draw_img($datLst,$labLst,$clrLst ,$a=200,$b=90,$v=20,$font=10)
{
$ox = 5+$a
$oy = 5+$b; ;
$fw = imagefontwidth($font) ;
$n = count($datLst);//データ項目数
$w = 10+$a*2; h = 10+$b*2+$ v+($fh+2)*$n;
$img = imagecreate($w, $h);
//RGB をインデックスカラーに変換します
for($i=0; $i $clrLst[$i] =draw_getindexcolor($img,$clrLst[$i]);
$clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff); imagecolorallocate($img, 0x00, 0x00, 0x00 );
//背景色を塗りつぶします
imagefill($img, 0, $clrbk)
//合計
$tot = 0; ; $i$tot += $datLst[$i];
$ed = 10+$b*2+$v; ($i=0; $i<$ n; $i++)
{
$sd = $ed;
$ed += $datLst[$i]/$tot*360;
//丸いケーキを描く
draw_sector3d; ($img, $ox, $oy, $ a, $b, $v, $sd, $ed, $clrLst[$i]); //$sd,$ed,$clrLst[$i]); //ラベルを描画
imagefilledrectangle($img, 5 , $ly, 5+$fw, $ly+$fh, $clrLst[$i]);
imagerectangle($img, 5, $ly, 5+$fw, $ ly+$fh, $clrt);
// imagestring($img, $font, 5+2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(" .(round(10000*($datLst[$ i]/$tot))/100)."%)", $clrt);
$str = iconv("GB2312", "UTF-8", $labLst[ $i]);
ImageTTFText($img, $font, 0, 5+2*$fw, $ly+13, $clrt, FONT_USED, $str.":".$datLst[$i]."(" .(round(10000*($datLst[$ i]/$tot))/100)."%)");
$ly += $fh+2;
//出力グラフィックス
header("Content -type: image/png");
/ /生成された画像を出力
imagepng($img);
}
$datLst = array(30, 20, 20, 20, 10, 20, 10, 20); //データ
$labLst = array("浙江省" , "広東省" , "上海市" , "北京市" , "福建省" , "江蘇省" , "湖北省" , "安徽省") //タグ
$clrLst = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999);
//Draw_img($datLst,$labLst,$clrLst);


/upload/20090928204252867.jpg

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/320667.html技術記事円グラフのコピー コードは次のとおりです。 ?php //+--------------------------+ //| function| // +----------------------+define("ANGLE_STEP", 3); //楕円弧を描くときの角度を定義します。 ..
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。