• ベストアンサー

GDライブラリで線グラフを作りたいのですが

PHP+MySQL+GDライブラリで折れ線グラフを実現したいと思っています。全くの初挑戦で、いろいろと調べてみたのですが良い資料が見つかりません。 参考になるサイトや書籍などがありましたらおしえてください。

  • PHP
  • 回答数1
  • ありがとう数3

質問者が選んだベストアンサー

  • ベストアンサー
  • kusukusu
  • ベストアンサー率38% (141/363)
回答No.1

UPローダでUPしたらなぜか消されたので本文を ホントここの基準ってよく分からん┐(  ̄ー ̄)┌ だいぶ前に作ったグラフを作成するクラスです。 実用性はない(できあがりのグラフが汚すぎる(^^;)ですが、参考にはなるかと思います。 折れ線だけでなく、ついでに棒グラフや円グラフもかけます。 <?php ////グラフクラス class make_graph { //$this->data 2次元配列 $data=array(array("項目名","数値"), array()…) //メンバ変数不変 var $data; var $title; var $graph_max; var $sort_column; var $between; //メンバ変数可変 var $fonts=array( "/usr/X11R6/lib/X11/fonts/TrueType/kochi-mincho.ttf", "/usr/X11R6/lib/X11/fonts/TrueType/kochi-gothic.ttf", "/usr/X11R6/lib/X11/fonts/TrueType/tlgothic.ttc", "/usr/X11R6/lib/X11/fonts/TrueType/win-kaisho.ttf", "/usr/X11R6/lib/X11/fonts/TrueType/ms-min.ttc" ); var $frame_width=700; var $frame_height=500; var $bgcolor="#dddddd"; var $font_size=10; var $font_color="#333355"; var $bar_col_type; var $bar_col_light; var $txt_space="15"; var $v_sp="100"; //コンストラクタ function make_graph($title,$graph_max,$data,$sort_column) { $this->title=$title; $this->graph_max=$graph_max; $this->data=$data; $this->sort_column=$sort_column; if($this->font_size<8||$this->font_size>14) { die("フォントサイズが小さすぎるか、大きすぎます。「8~14」で指定してください"); } $this->between=$this->font_size+4; $this->v_sp=$this->frame_height-$this->v_sp-20; if($this->sort_column=="down"||$this->sort_column=="up") { $this->sort_data(); } } ////オーバライド不可のメンバ関数 //ヘッダ関数 function make_header() { header("Content-type:image/png"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); } //カラーコード変換関数 //$hex=16の時 16進数から10進数へ、 $hex=10の時 10進数から16進数へ function change_color_code($color,$hex) { if($hex==16) { $op1=16; $op2=10; } else if($hex==10) { $op1=10; $op2=16; } else { die("error"); } $color=ereg_replace("#","",$color); $r=base_convert(substr($color,0,2),$op1,$op2); $g=base_convert(substr($color,2,2),$op1,$op2); $b=base_convert(substr($color,4,2),$op1,$op2); $color=($hex==16)?array($r,$g,$b):"#".$r.$g.$b; return $color; } //ランダム色作成関数 function make_color($keitou,$light) { for($i=0;$i<3;++$i) { $mt=mt_srand((double)microtime()*1000000); if($keitou=="r") { $s=($i==0)?100:0; $e=($i==0)?255:100; } else if($keitou=="g") { $s=($i==1)?100:0; $e=($i==1)?255:100; } else if($keitou=="b") { $e=($i==2)?255:100; } else { $s=0; $e=255; } if($light=="d") { $s=$s-50; if($s<0) { $s=0; } $e=$e-50; if($e<0) { $e=0; } } else if($light=="l") { $s=$s+50; if($s>255) { $s=255; } $e=$e+50; if($e>255) { $e=255; } } $b_col[$i]=mt_rand($s,$e); } return $b_col; } //データのソート function sort_data() { function sort_by_val($p1,$p2) { return($p1[1]-$p2[1]); } usort($this->data,"sort_by_val"); if($this->sort_column=="down") { rsort($this->data); } } ////オーバーライド可能なメンバ関数 //グラフ枠線関数 function frame_line($im,$id) { //$id==0 横線 imageline($im,0,20,$this->frame_width+$this->txt_space,20,imagecolorallocate($im,150,0,0)); if($id==0) { imageline($im,$this->txt_space+3,0,$this->txt_space+3,$this->frame_height,imagecolorallocate($im,150,0,0)); imageline($im,0,$this->v_sp+20,$this->frame_width+$this->txt_space,$this->v_sp+20,imagecolorallocate($im,150,0,0)); }else { imageline($im,$this->txt_space,20,$this->txt_space,$this->frame_height,imagecolorallocate($im,150,0,0)); } } //グラフ目盛り線関数 function memory_line($im,$id) { //$id==0 横線 $h_h=($id==0)?($this->v_sp)/10:($this->frame_width)/10; for($i=1;$i<=10;++$i) { ($id==0)?imageline($im,$this->txt_space+4,$i*$h_h+20,$this->frame_width+$this->txt_space,$i*$h_h+20,imagecolorallocate($im,255,190,190)):imageline($im,$this->txt_space+$i*$h_h,20,$this->txt_space+$i*$h_h,$this->frame_height,imagecolorallocate($im,255,190,190)); } } //開始関数(共通) function start() { $im=imagecreate($this->frame_width+$this->txt_space,$this->frame_height); $bg=$this->change_color_code($this->bgcolor,16); $bg_color=imagecolorallocate($im,$bg[0],$bg[1],$bg[2]); $fc=$this->change_color_code($this->font_color,16); $font_color=imagecolorallocate($im,$fc[0],$fc[1],$fc[2]); $colums_bg_color=imagecolorallocatealpha($im,$bg[0],$bg[1],$bg[2],100); return array($im,$font_color,$colums_bg_color); } //フォントサイズのよるサイズ決定関数 function create_x_y($id) { //$id==0 横線 if($id==0) { switch($this->font_size) { case "8": $w=12.2; break; case "9": $w=13.2; break; case "10": $w=14.2; break; case "11":$w=15.2; break; case "12": $w=16.2; break; case "13": $w=17.2; break; case "14": $w=18.2; break; } }else { switch($this->font_size) { case "8": $w=14.2; break; case "9": $w=15.2; break; case "10": $w=16.2; break; case "11": $w=17.2; break; case "12": $w=18.2; break; case "13": $w=19.2; break; case "14": $w=20.5; break; } } return $w; } ////メイン関数 //棒グラフ「縦」 function bar_graph_v() { $this->make_header(); $w=$this->create_x_y(0); $this->frame_width=$w*sizeof($this->data); $start=$this->start(); $im=$start[0]; $font_color=$start[1]; $colums_bg_color=$start[2]; $max=""; foreach($this->data as $d) { if($d[1]>=$max) { $max=$d[1]; } } //カラム壁色 imagefilledrectangle($im,3+$this->txt_space,$this->v_sp+20,$this->frame_width-3+$this->txt_space+20,$this->frame_height,$colums_bg_color); //グラフ枠線 $this->frame_line($im,0); //薄い横線 $this->memory_line($im,0); //文字 imagettftext($im,$this->font_size,0,$this->txt_space+10,15,$font_color,$this->fonts[3],mb_convert_encoding($this->title,"UTF-8","auto")); imagettftext($im,10,-90,1,25,$font_color,$this->fonts[0],"Max:".number_format($this->graph_max)." 1memory:".number_format($this->graph_max/10)); //メイン for($i=0;$i<sizeof($this->data);++$i) { $y1=($this->graph_max-$this->data[$i][1])/$this->graph_max*$this->v_sp+20; imagettftext($im,$this->font_size,-90,$i*$this->between+5+$this->txt_space,$this->v_sp+2+20,$font_color,$this->fonts[3],mb_convert_encoding($this->data[$i][0],"UTF-8","auto")); $b_col=$this->make_color($this->bar_col_type,$this->bar_col_light); $col=imagecolorallocate($im,$b_col[0],$b_col[1],$b_col[2]); imagefilledrectangle($im,$i*$this->between+6+$this->txt_space,$y1,($i+1)*$this->between-1+$this->txt_space,$this->v_sp+20,$col); } imagepng($im); imagedestroy($im); } //棒グラフ「横」 function bar_graph_h() { $this->make_header(); $w=$this->create_x_y(1); $this->frame_height=$w*sizeof($this->data); $start=$this->start(); $im=$start[0]; $font_color=$start[1]; $colums_bg_color=$start[2]; $max=""; foreach($this->data as $d) { if($d[1]>=$max) { $max=$d[1]; } } //カラム壁色 imagefilledrectangle($im,0,20,$this->txt_space,$this->frame_height,$colums_bg_color); //グラフ枠線 $this->frame_line($im,1); //薄い横線 $this->memory_line($im,1); //文字 $word=mb_convert_encoding($this->title,"UTF-8","auto")." Max:".number_format($this->graph_max)." 1memory:".number_format($this->graph_max/10); imagettftext($im,$this->font_size,0,10,15,$font_color,$this->fonts[3],$word); //メイン for($i=0;$i<sizeof($this->data);++$i) { $x2=$this->data[$i][1]/$this->graph_max*$this->frame_width; imagettftext($im,$this->font_size,0,5,$i*$this->between+20+$this->font_size+3,$font_color,$this->fonts[3],mb_convert_encoding($this->data[$i][0],"UTF-8","auto")); $b_col=$this->make_color($this->bar_col_type,$this->bar_col_light); $col=imagecolorallocate($im,$b_col[0],$b_col[1],$b_col[2]); imagefilledrectangle($im,$this->txt_space,$i*$this->between+3+20,$x2+$this->txt_space,($i+1)*$this->between-1+20,$col); } imagepng($im); imagedestroy($im); } //折れ線グラフ function line_graph() { $this->make_header(); $w=$this->create_x_y(0); $this->frame_width=$w*sizeof($this->data); $start=$this->start(); $im=$start[0]; $font_color=$start[1]; $colums_bg_color=$start[2]; $max=""; foreach($this->data as $d) { if($d[1]>=$max) { $max=$d[1]; } } //カラム壁色 imagefilledrectangle($im,3+$this->txt_space,$this->v_sp+20,$this->frame_width-3+$this->txt_space+20,$this->frame_height,$colums_bg_color); //グラフ枠線 $this->frame_line($im,0); //薄い横線 $this->memory_line($im,0); //文字 imagettftext($im,$this->font_size,0,$this->txt_space+10,15,$font_color,$this->fonts[3],mb_convert_encoding($this->title,"UTF-8","auto")); imagettftext($im,10,-90,1,25,$font_color,$this->fonts[0],"Max:".number_format($this->graph_max)." 1memory:".number_format($this->graph_max/10)); //メイン $x1=$this->txt_space+4; $y1=$this->v_sp+20; imageantialias($im,true); for($i=0;$i<sizeof($this->data);++$i) { $b_col=$this->make_color($this->bar_col_type,$this->bar_col_light); $col=imagecolorallocate($im,$b_col[0],$b_col[1],$b_col[2]); $x2=$i*$this->between+6+$this->txt_space; $y2=($this->graph_max-$this->data[$i][1])/$this->graph_max*$this->v_sp+20; imageline($im,$x1,$y1,$x2,$y2,$col); imagefilledarc($im,$x2,$y2,5,5,0,360,$col,IMG_ARC_PIE); imagefilledarc($im,$x2,$y2,2,2,0,360,imagecolorallocate($im,255,255,255),IMG_ARC_PIE); imagettftext($im,$this->font_size,-90,$i*$this->between+5+$this->txt_space,$this->v_sp+2+20,$font_color,$this->fonts[3],mb_convert_encoding($this->data[$i][0],"UTF-8","auto")); $x1=$x2; $y1=$y2; } imagepng($im); imagedestroy($im); } //円グラフ function circle_graph() { if(sizeof($this->data)>30) { die("項目数が30を超え、多すぎます。円グラフには不向きです"); } $this->make_header(); $w=$this->create_x_y(1); $this->frame_height=$w*sizeof($this->data); if($this->frame_height<$this->frame_width) { $this->frame_height=$this->frame_width; } if($this->frame_height-20<$this->frame_width) { $this->frame_height=$this->frame_width+20; } $dia=$this->frame_width-20-10; $cx=($this->frame_width-10)/2+$this->txt_space+5; $cy=20+($this->frame_height)/2; $im=imagecreate($this->frame_width+$this->txt_space,$this->frame_height+20); $bg=$this->change_color_code($this->bgcolor,16); $bg_color=imagecolorallocate($im,$bg[0],$bg[1],$bg[2]); $fc=$this->change_color_code($this->font_color,16); $font_color=imagecolorallocate($im,$fc[0],$fc[1],$fc[2]); $colums_bg_color=imagecolorallocatealpha($im,$bg[0],$bg[1],$bg[2],100); //円描画 imageellipse($im,$cx,$cy,$dia,$dia,$bg_color); //カラム壁色 imagefilledrectangle($im,0,20,$this->txt_space,$this->frame_height+20,$colums_bg_color); //文字 imagettftext($im,$this->font_size,0,$this->txt_space+10,15,$font_color,$this->fonts[3],mb_convert_encoding($this->title,"UTF-8","auto")); //メイン $total=0; foreach($this->data as $d) { $total+=$d[1]; } imageantialias($im,true); $s=-90; for($i=0;$i<sizeof($this->data);++$i) { $e=$s+$this->data[$i][1]/$total*360; if($e<0) { $e=$e+360; } $b_col=$this->make_color($this->bar_col_type,$this->bar_col_light); $col=imagecolorallocate($im,$b_col[0],$b_col[1],$b_col[2]); imagefilledarc($im,$cx,$cy,$dia,$dia,$s,$e,$col,IMG_ARC_PIE); $word=mb_convert_encoding($this->data[$i][0],"UTF-8","auto")."(".number_format($this->data[$i][1]/$total*100,1)."%)"; imagettftext($im,$this->font_size,0,5,$i*$this->between+20+$this->font_size+3,$col,$this->fonts[3],$word); $s=$e; } //グラフ枠線 imageline($im,0,20,$this->frame_width+$this->txt_space,20,imagecolorallocate($im,150,0,0)); imageline($im,$this->txt_space+3,0,$this->txt_space+3,$this->frame_height,imagecolorallocate($im,150,0,0)); imageline($im,$cx,20,$cx,$this->frame_height+20,imagecolorallocate($im,150,0,0)); imageline($im,$this->txt_space+4,$cy,$this->txt_space+$this->frame_width,$cy,imagecolorallocate($im,150,0,0)); imagepng($im); imagedestroy($im); } }

note4129
質問者

お礼

ありがとうございます。大変参考になりました。 結局jpgraphを利用してみることにしました。こちらも日本語マニュアルがないようで苦労しそうですが。

関連するQ&A

  • GDライブラリについて

    いつもお世話になります。PHPで初めて円グラフ作成を行おうと思っているのですが、GDライブラリが旨く組み込まれていません。OSはwindows,PHP Version 4.3.1です。下記項目は行っています。何か足りない点などありましたらお聞かせ下さい。 1. phpiniのphp_gd2.dllのコメントをはずしました。 2. phpiniにextension_dir = c:/php/extensions/   を挿入。 3. GDライブラリをダウンロードしプロジェクトフォルダに展開。・・・・・ 実はこの部分が自信なく、インストールなど何もしていないので果たしてよいのかどうか・・・ 4. phpinfo()で内容を確認していますがgdに関するメッセージは表示されない。 よろしくお願いいたします。 因みにプログラムを実行すると Call to undefined function: imagecreate() in ... のようなメッセージが出力されるので、旨く組み込まれていないのでは無いかと判断しています。

    • ベストアンサー
    • PHP
  • GDライブラリについて

    GDライブラリについて PHPのプログラムを使ううえでGDライブラリというのをインストールする必要があるとの事で調べて見たのですが英語表記で書かれているため、まったくインストール方法に関して理解できず困っているところです。 何方かに、ご指導して頂けると助かるのですがお願いできないでしょうか? よろしくお願いします。

    • ベストアンサー
    • PHP
  • PHP 4.3で使える グラフで表示させるライブラリ

    結果をグラフで表示させたいのですが、PHP 4.3で使えるライブラリはあるのでしょうか? 棒グラフや円マーク、折れ線グラフなどです。 簡単でできるだけフリーのものが良いです。 個人で使うので、商用利用以外が無料というものでも構いません。 無ければ、有償の方も知りたいです。

    • 締切済み
    • PHP
  • GDライブラリが使えない・・・

    当方fedora8にてApache2とphp4.4.9にてwebサーバーを構築しております。 Apacheはyumにてphpは4をインストールしたい加減からソースからコンパイルしてインストールしました。 configure時に下記のようにしてインストールしております。 './configure' '--prefix=/usr/local/php-4.4.9' '--enable-mbstring' '--with-apxs2=/usr/sbin/apxs' '--with-pgsql=/usr/local/pgsql' '--with-dg' '--with-zlib' '--with-jpeg-dir' phpinfo()を見ても上記のように設定されております。 しかし、phpinfoにgd関係の表示がされないのですがなぜでしょうか? phpのエラーログにはこうありました。 Warning: Unknown(): Unable to load dynamic library './php_gd2.dll' - ./php_gd2.dll: cannot open shared object file: No such file or directory in Unknown on line 0 php.iniのgd2のコメントアウトは消してあります。 必要なライブラリ関係はyumにてすべてインストール完了しております。 どなたか教えてください。 ではよろしくお願いいたします。

  • pchartというグラフ生成ライブラリについて

    はじめまして、鏑木といいます。 phpでグラフを生成したいと思い、以下のサイトを見つけました。 http://pchart.sourceforge.net/ pchartというフリーソースのグラフ生成ライブラリです。 サンプルソースがいくつかついていますので、これをDLしてサンプルを動かしてみたのですが、動きません。 冒頭でインクルードしているクラスファイルのほとんどの行がエラーを返します。(ファンクションが呼び出せないとか色々な内容で、ブラウザフリーズしました。) 借りているサーバはヘテムルで、php5を使っています。 サーバにはGDライブラリは入っています。 何か他に必要な設定などあるのでしょうか? わかるかたいらっしゃいましたら教えてくださると助かります。 よろしくお願いいたします。

    • ベストアンサー
    • PHP
  • GDについて

    プログラミング初心者です。 PHPのGDライブラリを使いたくてPHPをインストールしました。 ネットで調べて色々試したのですが、GDライブラリが有効になっているかはどのように確認したらいいのでしょうか? phpinfo()でConfigure Commandのところに--with-gd=sharedみたいのは出てるんですがこれは違いますよね?? 他にGDの文字を確認できないのですが・・・ よろしくお願いします。 PHPのバージョンは5.2.5です。windouwsxpです。

    • 締切済み
    • PHP
  • PHPのGDライブラリについて

    掲題の件について質問です。 いま、PHPのGDライブラリを用いて大量の既存のイメージファイル(JPEG)から各三種類ほどのサムネイルを作成しているのですが一点問題があります。 既存のイメージを縮小してサムネイルを作成しているのですが、この元となる画像は 解像度72dpiの画像となるのですが、GDを用いて作成されたサムネイルは96dpiの解像度として作成されるのです。 故合ってdpiは72でなければなりません。 このPHPのGDライブラリで画像のDPIの指定は出来ないでしょうか? 識者の方、よろしければご教授ください。 お願い致します。

    • ベストアンサー
    • PHP
  • OS10.3 ServerでGDライブラリ

    OS10.3.5 Server + PHPで運用しております。 画像の自動生成を行いたくGDライブラリと、jpeg-6bやlibpngを組み込みたいと思っております。PHPのバージョンは4.2.10(OSの標準搭載)、場合によってはバージョンアップしても構わないと思っております。 サイトを色々さがしても、Server10.3以降のPHPのコンパイル方法がどこも見当たりません。 どなたかServer10.3以降でGDを実装済みの方、ご教授願います。

    • ベストアンサー
    • PHP
  • 折れ線グラフの描画について

    GDを使って折れ線グラフを描画しようと考えております。 JpGraphなどを使うケースが多いようですが、GDのみで作成をしなければ いけないのですが、良いサンプルなどはありますでしょうか。 環境は以下です。 PHP4.2.2 GD1.6.2

    • ベストアンサー
    • PHP
  • 共有サーバでのGDライブラリ有効化

    共有サーバを使用しているため、php.iniを編集することができません。 この状態で、GDライブラリを有効にすることはできないのでしょうか。 ありましたら、その方法を教えて頂きたいです。

    • 締切済み
    • PHP

専門家に質問してみよう