Pear::Image_Graph その5
Image_Graph ロ-ソク足 CSV 読込 PHP ファイル
p_gra-cand-2.php
63Pear Villageに戻る
<?php
$filepath = "file/p_gra5_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;
}
}
$last_l = count($records) - 1;
// echo $last_l;
require_once 'Image/Graph.php';
// create the graph
$Graph = Image_Graph::factory
('graph', array(300, 400));
$Font =
$Graph->addNew
('font', 'fonts/ipaexm');
/*
日本語を含むフォントを指定
.ttf は省略可
*/
$Font->setSize(8);
$Graph->setFont($Font);
// create the plotareas
$Graph -> add(
Image_Graph::vertical(
Image_Graph::factory('title', array(
'℃ 気温変化-ロ-ソク足表示 ',
12)),
Image_Graph::vertical(
$Plotarea =
Image_Graph::factory('plotarea'),
$Legend =
Image_Graph::factory('legend'),
85),
5)
);
$Legend -> setPlotarea($Plotarea);
$Plotarea -> setFillColor('white');
// create the dataset
$Dataset[0] =
Image_Graph::factory('dataset');
$Dataset[0] ->
setName('Forecast-Max');
$Dataset[1] =
Image_Graph::factory('dataset');
$Dataset[1] ->
setName('Forecast-Min');
$Dataset[2] =
Image_Graph::factory('dataset');
for ($k1 = 1; $k1 < $last_l; $k1++) {
$Dataset[0] -> addPoint(
$records[$k1][0],$records[$k1][5]
);
$Dataset[1] -> addPoint(
$records[$k1][0],$records[$k1][6]
);
$Dataset[2] -> addPoint(
$records[$k1][0],
array(
'min' => $records[$k1][3],
'open' =>$records[$k1][1],
'close'=>$records[$k1][4],
'max' => $records[$k1][2])
);
}
$Plot[0] = $Plotarea -> addNew
('area', array($Dataset[0]));
$Plot[0] -> setLineColor('red');
$Plot[1] = $Plotarea->addNew
('area', array($Dataset[1]));
$Plot[1]->setLineColor('blue');
$Grid = $Plotarea -> addNew(
'line_grid', null, IMAGE_GRAPH_AXIS_X);
$Grid -> setLineColor('lightgray@0.1');
$Grid = $Plotarea -> addNew(
'line_grid', null, IMAGE_GRAPH_AXIS_Y);
$Grid -> setLineColor('lightgray@0.1');
$Plot[2] = $Plotarea -> addNew(
'Image_Graph_Plot_CandleStick',
array($Dataset[2]));
$Fill = Image_Graph::factory(
'Image_Graph_Fill_Array');
$Fill -> addColor('green@0.4', 'red');
$Fill -> addColor('red@0.4', 'green');
$Plot[2] -> setFillStyle($Fill);
$Plot[2] -> setTitle(
'CandleStick Chart Daily 2017/6/1~4');
$AxisX = $Plotarea -> getAxis(IMAGE_GRAPH_AXIS_X);
//$AxisX->setFontAngle('vertical');
$AxisX->setLabelInterval(1);
$AxisY = $Plotarea -> getAxis(IMAGE_GRAPH_AXIS_Y);
$AxisY -> setLabelInterval(2);
$AxisY -> forceMinimum(18);
$AxisY -> forceMaximum(30);
$Legend->setFontSize(10);
//output the Graph
//$Graph->done();
// グラフの出力
$filename = 'img/plot_cand-2.png';
$Graph -> done(
array(
'filename' => $filename)
);
echo "<img src = $filename>";
?>