Pear::Image_Graph その3
Image_Graph CSV 読込グラフ PHP ファイル
p_gra-62-csv.php
63Pear Villageに戻る
<?php
include 'Image/Graph.php';
// ファイル取得
$filepath = "file/p_gra_samp.csv";
$file = new SplFileObject($filepath);
$file -> setFlags
(SplFileObject::READ_CSV);
/*
ファイル内のデータループ
$key $line $str の変数名は適宜決定
*/
foreach ($file as $key => $line) {
foreach($line as $str){
$records[$key][] = $str;
}
}
// データ 多重連想配列
/*
$data = array(
array(10,13,20,15,12,18),//予測-1
array( 5, 4, 8,10, 9, 5),//予測-2
array(22, 0, 0, 0, 0,22),//消線
array(12,11,19,14,15,14),//実績-1
array( 5, 3, 9,7.5,8, 4) //実績-2
);
*/
for ($i = 2; $i < 7; $i++) {
//必要デ-タ数 2~6 = 5個
for ($j = 2; $j < 8; $j++) {
//必要デ-タ数 2~7 = 6個
$data[$i-2][$j-2] = $records[$j][$i];
}
}
/*
X軸ラベル
$x = array
('1','2','3','4','5','6');
*/
for ($j = 2; $j < 8; $j++) {
//必要横デ-タ数 2~7 = 6個
$x[$j-2] = $records[$j][1];
}
// 凡例
$l = array
('予測-1','予測-2',' ',
'実績-1','実績-2');
// 線色
$c = array
('red','black','white','red','black');
$aib = array
('area','area','impulse',
'impulse','bar');
/*
Image_Graphオブジェクト作成
第二引数でグラフのサイズと画像書式 png
を指定可能
*/
$Graph = Image_Graph::factory
('graph', array(400, 300));
$Font =
$Graph->addNew
('font', 'fonts/ipaexm');
// フォントを適用
$Graph->setFont($Font);
$Graph->add(
Image_Graph::vertical(
Image_Graph::factory(
'title',array(
'サンプルグラフ表示',12)),
Image_Graph::vertical(
$Legend = Image_Graph::factory('legend'),
$Plotarea =
Image_Graph::factory
('plotarea'),
8 // 凡例の割合
),6 // タイトルの割合
)
);
$Legend->setPlotarea($Plotarea);
$fillColor =
Image_Graph::factory
('Image_Graph_Fill_Array');
/*
背景色設定(色@透明度)
$Plotarea->setFillColor('blue@0.1');
*/
$Plotarea->setFillColor('blue@0');
/*
設定を省略すると黒くなる。
値を小さくすること
軸のオブジェクトは、プロットエリアから
取得できます。
今回 X 軸の設定はなし
$axis_x =& $Plotarea->getAxis('x');
//横軸
$axis_x->forceMinimum(0);
$axis_x->forceMaximum(30);
*/
$axis_y =& $Plotarea->getAxis('y');
//縦軸
$axis_y->forceMinimum(2);
$axis_y->forceMaximum(22);
//表示するデータの作成
for ($i=0;$i < count($l);$i++) {
$Dataset[$i] =
Image_Graph::factory('dataset');
for (
$n = 0; $n < count($data[$i]);
$n++)
{
$Dataset[$i] ->
addPoint($x[$n],$data[$i][$n]);
}
$Dataset[$i]->setName($l[$i]);
$Plot = $Plotarea-> addNew
($aib[$i],array($Dataset[$i]));
$Plot->setLineColor($c[$i]);
if ($aib[$i]=='bar'){
$fillColor->addColor('black');
$Plot->setFillStyle($fillColor);
$Plot->setSpacing(59);
}
}
/*
グリッドの設定
null はダミ、なんでもいいから入れておく
$GridX->setLineColor('gray@0.3');
*/
$GridY = $Plotarea-> addNew
('line_grid', null, IMAGE_GRAPH_AXIS_Y);
$GridY->setLineColor('gray@0.3');
/*
画面に出力
$Graph->done();
グラフの出力
*/
$filename = 'img/p_gra-62-csv.png';
$Graph->done(
array(
'filename' => $filename,
)
);
echo
"<img src =
'img/p_gra-62-csv.png'>";
?>