CodeIgniterの学習 5 - PHPでグラフを生成して表示したい 1 (調査)
(08/11/25 追記) http://d.hatena.ne.jp/dix3/20081125/1227568495 に関連エントリ(PHPLOTのライブラリ化実験ソース)を追加しました。
前回のエントリで次はxajaxを使えるようにすると書きつつ、先にグラフの生成に浮気してみる。
タスクリストに達成率とかのグラフを描画して色を添える目的。(で、その後実務に適用する)
CodeIgniterで、JP Graph を使ってグラフを生成する方法は
http://codeigniter.com/wiki/JP_Graph/
の通りにそのまま辿ってやればできるっぽいが、如何せんライセンスの問題がある。
(商用はフリーではないのね)
んで前から気になっていた、PHPLOT - http://phplot.sourceforge.net/ を使ってみることにした。
(こっちはGPL)
レーダーチャートが作れないのが残念だが、扱いやすくて、プラグイン化も容易そう。
(08/09/26 追記) 他にもいくつかチャートを生成する方法はあるみたい。画像ファイルの生成にこだわらなければjavascriptで作る方式でもいいのだが。
実験
今回はとりあえずソースをダウンロードしてサンプルを試してみる。(CodeIgniterのプラグイン化は後でやる)
PHPLOT ソース http://sourceforge.net/projects/phplot/
ダウンロードすると、中に
がある。
これを、適当なパス(ドキュメントルート配下以外)に置いて使う。
今回は実験だけなので、全部ドキュメントルートに置いた。(mod_rewriteは一時停止)
READMEと、デモソース - http://phplot.sourceforge.net/demosource.php
を参考にサンプルを作って試してみる。
simpleplot.html - 呼び出し側
<html> <head> <title>Hello, PHPlot!</title> </head> <body> <p>area:<br/><img src="testplot.php?p=1"></p> <p>bars:<br/><img src="testplot.php?p=2"></p> <p>linepoints:<br/><img src="testplot.php?p=3"></p> <p>lines:<br/><img src="testplot.php?p=4"></p> <p>pie:<br/><img src="testplot.php?p=5"></p> <p>points:<br/><img src="testplot.php?p=6"></p> <p>squared:<br/><img src="testplot.php?p=7"></p> <p>stackedbars:<br/><img src="testplot.php?p=8"></p> <p>thinbarline:<br/><img src="testplot.php?p=9"></p> </body> </html>
(img src でphpを呼び出して動的にグラフを生成しているが、グラフをファイルとして保存することもできる。プラグインを作るときはファイル保存形式でいこう)
生成側 testplot.php - ここをCodeIgniterのプラグイン化する予定
<?php // dix3の実験(CodeIgniterのプラグイン化 前調査) // Modified at 2008-09-22 dix3 require_once 'phplot.php'; // 配列でデータを持たせる // todo プラグインの引数 $data = array( array( '2001', 60, 35, 20 , 10 ), array( '2002', 65, 30, 30 , 20 ), array( '2003', 70, 25, 40 , 30 ), array( '2004', 72, 20, 60 , 40 ), array( '2005', 75, 15, 70 , 50 ), array( '2006', 77, 10, 80 , 60 ), array( '2007', 80, 5, 90 , 70 ), ); // 画像のサイズと、第3引数でファイル名が指定できる。 // todo プラグインの引数候補 $p = new PHPlot( 400, 300 ); // フォントの指定 $p -> SetDefaultTTFont( '/usr/share/fonts/sazanami-fonts-gothic/sazanami-gothic.ttf' ); // グラフのタイトル $p -> SetTitle( 'グラフのテストなりよ' ); // Select the data array representation and store the data: $p -> SetDataType( 'text-data' ); $p -> SetDataValues( $data ); // グラフの種類 /** * area Filled areas between lines. Also known as 'cumulative line plot' or 'component line plot'. * bars Filled bars with optional 3-D look, multiple datasets are offset * linepoints Lines between points, a marker at each point, optional error bars * lines Straight lines between data points, optional error bars * pie Pie chart with or without 3-D affects * points Draws a marker at each data point, optional error bars * squared Stepped lines * stackedbars Filled bars with optional 3-D look, multiple data sets are accumulated and the sum is graphed * thinbarline Vertical lines from X axis up. Also known as impulse. */ // 実験なのでGETパラメータで渡してる // todo プラグインの引数候補 switch ( $_GET["p"] ) { case "1": $p -> SetPlotType( 'area' ); break; case "2": $p -> SetPlotType( 'bars' ); break; case "3": $p -> SetPlotType( 'linepoints' ); break; case "4": $p -> SetPlotType( 'lines' ); break; case "5": $p -> SetPlotType( 'pie' ); break; case "6": $p -> SetPlotType( 'points' ); break; case "7": $p -> SetPlotType( 'squared' ); break; case "8": $p -> SetPlotType( 'stackedbars' ); break; case "9": $p -> SetPlotType( 'thinbarline' ); break; default: $p -> SetPlotType( 'bars' ); break; } // todo プラグインの引数候補、ここから下全部プラグインの引数で指定できるようにする // Define the data range. PHPlot can do this automatically, but not as well. $p -> SetPlotAreaWorld( 0, 0, 7, 100 ); // Select an overall image background color and another color under the plot: // 枠の色 $p -> SetBackgroundColor( '#ade949' ); // グラフの背景 $p -> SetDrawPlotAreaBackground( true ); $p -> SetPlotBgColor( '#f5f3f1' ); // Draw lines on all 4 sides of the plot: // 内側の枠 $p -> SetPlotBorderType( 'full' ); // 凡例 $p -> SetLegend( array( 'くー', 'こー', 'りんりん' , 'はわわ' ) ); // 凡例の位置 $p -> SetLegendWorld( 0.1, 99 ); // Turn data labels on, and all ticks and tick labels off: // ラベルの有無と、刻みの有無と位置 $p -> SetXDataLabelPos( 'plotdown' ); $p -> SetXTickPos( 'none' ); $p -> SetXTickLabelPos( 'none' ); $p -> SetYTickPos( 'plotright' ); $p -> SetYTickLabelPos( 'plotright' ); // Generate and output the graph now: // グラフの描画 $p -> DrawGraph(); ?>
いろんな出力結果
うーん、なかなかいいねぇ。採用!後はどれだけ簡単に呼び出しできるようにするかだな。
さて、プラグイン化はいつやろう。
(08/11/25 追記) http://d.hatena.ne.jp/dix3/20081125/1227568495 に関連エントリ(PHPLOTのライブラリ化実験ソース)を追加しました。