室内と庭の温度、湿度のpostgreSQL DBが動くようになったので、今まで使っていた、HPをちょっと弄って化粧をしてみました。
JpGraphを使ってグラフを描かせていましたが、何故かグラフの各点の値表示が出来な
かったり、TrueTypeのフォントが使えていなかったりしたのを、一日がかりで修正。
JpGraphを使ってグラフを描かせていましたが、何故かグラフの各点の値表示が出来な
かったり、TrueTypeのフォントが使えていなかったりしたのを、一日がかりで修正。
- 値の表示が出来なかった原因 ; $graph->clearTheme()を設定していないと、表示をしてくれなかった。下のphpのソースコード参照
- TrueTypeフォントのインストール ; FreeBSDで動いているので、packageからwebfont(MSのTTF同等?)をインストール。 /usr/local/lib/php/jpgraph/jpg-config.inc.phpのTTF, MBTTFを実際にインストールされているディレクトリに指定する。
//
// UNIX:
// CACHE_DIR /tmp/jpgraph_cache/
// TTF_DIR /usr/share/fonts/truetype/
// MBTTF_DIR /usr/share/fonts/truetype/
//
// WINDOWS:
// CACHE_DIR $SERVER_TEMP/jpgraph_cache/
// TTF_DIR $SERVER_SYSTEMROOT/fonts/
// MBTTF_DIR $SERVER_SYSTEMROOT/fonts/
//
//------------------------------------------------------------------------
// define('CACHE_DIR','/tmp/jpgraph_cache/');
define('TTF_DIR','/usr/local/share/fonts/webfonts/');
define('MBTTF_DIR','/usr/local/share/fonts/webfonts/');
ついでにDBの欠落データを正しくグラフに反映させる為に、pg_fetch_rows()から、pg_fetch_result()に変更して、array()の該当する日時にデータが挿入されるように変更
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
require_once ('jpgraph/jpgraph_stock.php');
require_once ('jpgraph/jpgraph_line.php');
$ydata = array();
$y1data = array();
$ymax = array();
$ymin = array();
$yhumid = array();
$ymaxhumid = array();
$yminhumid = array();
$ypressure = array();
$ymaxpressure = array();
$yminpressure = array();
$sdata = array();
$conn = "hostaddr=192.168.?.?? dbname=XXXXX user=Who password=Are_you?"; //fake user/password
$link = pg_connect($conn);
if (!$link) {
die ('Connection failed '.pg_last_error());
}
$result = pg_query("SELECT day, avg_temperature, max_temperature, min_temperature,avg_humidity, max_humidity, min_humidity, avg_pressure,max_pressure,min_pressure from dailydatacurrentmonth where location='MyRoom'" );
if (!$result){
die('Query failed'.pg_last_error());
}
$tempE=pg_fetch_result($result, pg_num_rows($result)-1, 0); // chech when day ends
for ($i = 0, $j = 0, $k = 0; $i < $tempE; $i++){
$temp =pg_fetch_result($result, $k, 0); // day
if ($i == $temp-1) {
$x_axis[$i] = $temp;
$ydata[$i] = pg_fetch_result($result, $k, 'avg_temperature');
$yhumid[$i] = pg_fetch_result($result, $k, 'avg_humidity');
$ymaxhumid[$i] = pg_fetch_result($result, $k, 'max_humidity');
$yminhumid[$i] = pg_fetch_result($result, $k, 'min_humidity');
$ypressure[$i] = pg_fetch_result($result, $k, 'avg_pressure');
$ymaxpressure[$i] = pg_fetch_result($result, $k, 'max_pressure');
$yminpressure[$i] = pg_fetch_result($result, $k, 'min_pressure');
$sdata[$j++] = pg_fetch_result($result, $k, 'min_temperature');
$sdata[$j++] = pg_fetch_result($result, $k, 'min_temperature');
$sdata[$j++] = pg_fetch_result($result, $k, 'max_temperature');
$sdata[$j++] = pg_fetch_result($result, $k, 'max_temperature');
$k++;
} else {
$x_axis[$i] = $i+1;
$ydata[$i] = '';
$yhumid[$i] = '';
$ymaxhumid[$i] = '';
$yminhumid[$i] = '';
$ypressure[$i] = '';
$ymaxpressure[$i] = '';
$yminpressure[$i] = '';
$sdata[$j++] = '';
$sdata[$j++] = '';
$sdata[$j++] = '';
$sdata[$j++] = '';
}
}
$result = '';
$result = pg_query("SELECT day, avg_temperature from dailydatacurrentmonth_prevyear where location='MyRoom'" );
if (!$result){
die('Query failed'.pg_last_error());
}
for ($i = 0; $i < pg_num_rows($result); $i++){
$rows = pg_fetch_array($result, NULL, PGSQL_ASSOC);
$y1data[$i] = $rows['avg_temperature'];
}
$close_flag = pg_close($link);
$graph = new Graph(750, 450);
$graph->clearTheme();
$graph->SetFrame(true);
$graph->SetScale("textlin",10, 40);
$graph->SetY2Scale("lin",10, 90);
$graph->img->SetMargin(50,30,20,90);
$graph->SetShadow();
$graph->title->SetFont(FF_ARIAL,FS_BOLD,14);
$graph->title->Set("Daily Max/Min/Avg Temperature & Avg Humidity of the Month");
//$graph->yaxis->title->SetFont(FF_ARIAL.FS_ITALIC, 10);
$graph->yaxis->title->Set("Temperature");
$graph->ygrid->Show(true,false);
$graph->y2axis->title->Set("Humidity");
$graph->xaxis->title->Set("Month-Day");
//$graph->xaxis->title->SetFont(FF_ARIAL.FSBOLD, 10);
$graph->xgrid->Show(true,true);
// Specify the tick lables
$graph->xaxis->SetTickLabels($x_axis);
$graph->xaxis->SetTextLabelInterval(1);
// Create the linear plot
$lineplot = new LinePlot($ydata);
$lineplot->SetColor("blue");
$lineplot->mark->SetType(MARK_UTRIANGLE);
$lineplot->value->show();
$lineplot->value->SetFont( FF_ARIAL, FS_ITALIC, 7 );
$lineplot->value->SetColor('darkred');
$lineplot->value->SetFormat('%0.1f');
$p1 = new StockPlot($sdata);
$p1->SetWidth(9);
$line2plot = new LinePlot($y1data);
$line2plot->SetColor("red");
$line2plot->mark->SetType(MARK_UTRIANGLE);
//$line2plot->value->show();
$line2plot->value->SetColor('darkred');
$line2plot->value->SetFont( FF_ARIAL, FS_BOLD, 10 );
$line2plot->value->SetFormat('%0.1f');
$barplot = new BarPlot($yhumid);
$barplot->SetColor("black");
$barplot->value->Show();
$barplot->value->SetFont( FF_ARIAL, FS_BOLD, 10 );
$barplot->value->SetFormat('%0.1f');
// Add the plot to the graph
$graph->Add($lineplot);
$graph->Add($line2plot);
$graph->AddY2($barplot);
$graph->Add($p1);
$graph->Stroke();
?>
0 件のコメント:
コメントを投稿