2020年3月27日金曜日

JpGraphを久しぶりに弄って 一部問題解決。

室内と庭の温度、湿度のpostgreSQL DBが動くようになったので、今まで使っていた、HPをちょっと弄って化粧をしてみました。

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()の該当する日時にデータが挿入されるように変更
  1. require_once ('jpgraph/jpgraph.php');
  2. require_once ('jpgraph/jpgraph_bar.php');
  3. require_once ('jpgraph/jpgraph_stock.php');
  4. require_once ('jpgraph/jpgraph_line.php');
  5. $ydata = array();
  6. $y1data = array();
  7. $ymax = array();
  8. $ymin = array();
  9. $yhumid = array();
  10. $ymaxhumid = array();
  11. $yminhumid = array();
  12. $ypressure = array();
  13. $ymaxpressure = array();
  14. $yminpressure = array();
  15. $sdata = array();
  16. $conn = "hostaddr=192.168.?.?? dbname=XXXXX user=Who password=Are_you?"; //fake user/password
  17. $link = pg_connect($conn);
  18. if (!$link) {
  19. die ('Connection failed '.pg_last_error());
  20. }
  21. $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'" );
  22. if (!$result){
  23. die('Query failed'.pg_last_error());
  24. }
  25. $tempE=pg_fetch_result($result, pg_num_rows($result)-1, 0); // chech when day ends
  26. for ($i = 0, $j = 0, $k = 0; $i < $tempE; $i++){
  27. $temp =pg_fetch_result($result, $k, 0); // day
  28. if ($i == $temp-1) {
  29. $x_axis[$i] = $temp;
  30. $ydata[$i] = pg_fetch_result($result, $k, 'avg_temperature');
  31. $yhumid[$i] = pg_fetch_result($result, $k, 'avg_humidity');
  32. $ymaxhumid[$i] = pg_fetch_result($result, $k, 'max_humidity');
  33. $yminhumid[$i] = pg_fetch_result($result, $k, 'min_humidity');
  34. $ypressure[$i] = pg_fetch_result($result, $k, 'avg_pressure');
  35. $ymaxpressure[$i] = pg_fetch_result($result, $k, 'max_pressure');
  36. $yminpressure[$i] = pg_fetch_result($result, $k, 'min_pressure');
  37. $sdata[$j++] = pg_fetch_result($result, $k, 'min_temperature');
  38. $sdata[$j++] = pg_fetch_result($result, $k, 'min_temperature');
  39. $sdata[$j++] = pg_fetch_result($result, $k, 'max_temperature');
  40. $sdata[$j++] = pg_fetch_result($result, $k, 'max_temperature');
  41. $k++;
  42. } else {
  43. $x_axis[$i] = $i+1;
  44. $ydata[$i] = '';
  45. $yhumid[$i] = '';
  46. $ymaxhumid[$i] = '';
  47. $yminhumid[$i] = '';
  48. $ypressure[$i] = '';
  49. $ymaxpressure[$i] = '';
  50. $yminpressure[$i] = '';
  51. $sdata[$j++] = '';
  52. $sdata[$j++] = '';
  53. $sdata[$j++] = '';
  54. $sdata[$j++] = '';
  55. }
  56. }
  57. $result = '';
  58. $result = pg_query("SELECT day, avg_temperature from dailydatacurrentmonth_prevyear where location='MyRoom'" );
  59. if (!$result){
  60. die('Query failed'.pg_last_error());
  61. }
  62. for ($i = 0; $i < pg_num_rows($result); $i++){
  63. $rows = pg_fetch_array($result, NULL, PGSQL_ASSOC);
  64. $y1data[$i] = $rows['avg_temperature'];
  65. }
  66. $close_flag = pg_close($link);
  67. $graph = new Graph(750, 450);
  68. $graph->clearTheme();
  69. $graph->SetFrame(true);
  70. $graph->SetScale("textlin",10, 40);
  71. $graph->SetY2Scale("lin",10, 90);
  72. $graph->img->SetMargin(50,30,20,90);
  73. $graph->SetShadow();
  74. $graph->title->SetFont(FF_ARIAL,FS_BOLD,14);
  75. $graph->title->Set("Daily Max/Min/Avg Temperature & Avg Humidity of the Month");
  76. //$graph->yaxis->title->SetFont(FF_ARIAL.FS_ITALIC, 10);
  77. $graph->yaxis->title->Set("Temperature");
  78. $graph->ygrid->Show(true,false);
  79. $graph->y2axis->title->Set("Humidity");
  80. $graph->xaxis->title->Set("Month-Day");
  81. //$graph->xaxis->title->SetFont(FF_ARIAL.FSBOLD, 10);
  82. $graph->xgrid->Show(true,true);
  83. // Specify the tick lables
  84. $graph->xaxis->SetTickLabels($x_axis);
  85. $graph->xaxis->SetTextLabelInterval(1);
  86. // Create the linear plot
  87. $lineplot = new LinePlot($ydata);
  88. $lineplot->SetColor("blue");
  89. $lineplot->mark->SetType(MARK_UTRIANGLE);
  90. $lineplot->value->show();
  91. $lineplot->value->SetFont( FF_ARIAL, FS_ITALIC, 7 );
  92. $lineplot->value->SetColor('darkred');
  93. $lineplot->value->SetFormat('%0.1f');
  94. $p1 = new StockPlot($sdata);
  95. $p1->SetWidth(9);
  96. $line2plot = new LinePlot($y1data);
  97. $line2plot->SetColor("red");
  98. $line2plot->mark->SetType(MARK_UTRIANGLE);
  99. //$line2plot->value->show();
  100. $line2plot->value->SetColor('darkred');
  101. $line2plot->value->SetFont( FF_ARIAL, FS_BOLD, 10 );
  102. $line2plot->value->SetFormat('%0.1f');
  103. $barplot = new BarPlot($yhumid);
  104. $barplot->SetColor("black");
  105. $barplot->value->Show();
  106. $barplot->value->SetFont( FF_ARIAL, FS_BOLD, 10 );
  107. $barplot->value->SetFormat('%0.1f');
  108. // Add the plot to the graph
  109. $graph->Add($lineplot);
  110. $graph->Add($line2plot);
  111. $graph->AddY2($barplot);
  112. $graph->Add($p1);
  113. $graph->Stroke();
  114. ?>
  115.  

0 件のコメント:

RM Noise

最近Galaxyの携帯のCMで風のノイズ除去をしているシーンが気になっていて、「AIで処理すれば、ノイズ除去性能は上がるのでは?」なんて思いながら、 AIのopen source?の Hagging face をブラブラしていましたら、何故かFacebookで RM noize ...