元々、自分の庭の温湿度をDBに蓄えたかったのは、それを利用して庭の花の開花時期などを予想したりする事をしたかったからですが、安定的に(バッテリ切れ等なく)温湿度を計測できる環境の制作に時間が係り、手を付けられていませんでしたが、Xiao S3のdeep sleep 時14μAは、流石に効果があり、1か月以上安定に動作しています。
で、本題の有効積算温度グラフ表示を実装してみることにしました。
まづ、有効積算温度(Effective Accumulated Temperature)とは;
農業技術辞典 によれば「温度の日平均値をそのまま合計したもの。基準温度以下の温度は生育に寄与しないという考え方に基づき、生育に必要な最低温度(基準温度、発育ゼロ点ともいう)以上の日の日平均温度を合計した値を有効積算温度と呼ぶ。....」(コピペ出来なかったので転記)とあります。
従って、DBから日平均温度を計算し、基準温度を超えた分だけを足してゆくようなSQLを書いてやればよいことになり、これをグラフ表示するようにすればよいわけです。
SQLを含むphpのソースは;
<?php
// include('funcs.php');
//$threshold = 3;
// $location='GardenEast';
// $startDate = 20250401;
// $endDate = 20251030;
$averagedataPoints = array();
$temp;
$dbh = db_connect();
$handle = $dbh->prepare('select yyyymmdd, location, case when avg_temp - :threshold > 0 then (avg_temp - :threshold) else 0 end as datapoint from dailydataview where location = :location and yyyymmdd between :start and :end');
$handle->execute(['threshold' => $threshold, 'location' => $location,'start'=> $startDate,'end'=>$endDate]);
$result = $handle->fetchAll(PDO::FETCH_OBJ);
$temp1 = 0;
$count = 0;
foreach ($result as $row) {
$temp1 = $temp1 + $row->datapoint;
$a = substr($row->yyyymmdd,4,2)."月".substr($row->yyyymmdd,6)."日";
$label = $a . "(".$count."日)";
array_push($averagedataPoints, array("y"=> number_format($temp1, 2, '.', '' ), "label"=>$label));
$count = $count+1;
}
$handle=null;
$result=null;
// print json_encode($averagedataPoints,JSON_NUMERIC_CHECK).'
';
?>
これを表示するcanvasjs/php/htmlのコードは以下のようにしましたが、formへの指定入力内容が消えてブランクのままグラフの上に残るので見てくれが悪く、「何とかならないのか?」苦慮中です。 指定内容はグラフの方に表示されるので、間違いはありませんが、、、。
また、積算温度の予測機能は持っていませんので、過去の積算温度推移を併記させるようにと考えましたが、DBの過去のデータに欠落があって、開始時期とかが指定通りにならないことがあるので、現時点では、単年の表示のみ。
作業中、1000以上の数字がグラフ表示されなくて、1~2日悩みましたが、phpのnumber_formatが引数を3つ取り、2番目が小数点以下、3番目が千の単位の区切りを指定できるようになっているため、省略すると千の単位にコンマが入り、これがCanvasJsで問題を起こすのでした。要注意!!
<thtml>
<head>
<script>
<?php
include "/usr/local/www/data/DBAccess/funcs.php";
include "/usr/local/www/data/utilities.php";
$threshold = $_POST['threshold'];
$location = $_POST['location'];
$startDate=$_POST['startDate'];
$endDate = $_POST['endDate'];
include "/usr/local/www/data/DBAccess/AccumulatedTemperature2.php";
?>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
theme: "light2",
title:{
text: ,
},
subtitles: [
{
text: ,
fontColor: "blue",
fontSize:20
},
{
text: ,
fontColor: "blue",
fontSize:15
}
],
axisY: {
title: "積算温度(°C)",
lineColor: "#C24642",
tickColor: "#C24642",
labelFontColor: "#C24642",
titleFontColor: "#C24642",
interlacedColor: "#F0F8FF" ,
includeZero: true,
suffix: "°C",
},
data: [
{
name: "積算温度",
type: "line",
indexLabelFontSize: 16,
color: "rgba(255,0,0,0.3)", // red
lineThickness: 3,
showInLegend: true,
yValueFormatString: "#0.## °C",
dataPoints: <?php echo json_encode($averagedataPoints, JSON_NUMERIC_CHECK); ?>
},
]
});
chart.render();
}
</script>
</head>
<body>
<form action="" method="post">
閾値(温度°C):<input name="threshold" type="text" value="10" />
開始日時(YYYYMMDD):<input name="startDate" type="text" />
終了日時(YYYYMMDD):<input name="endDate" type="text" />
場所:<select name="location">
<option value="GardenEast"></option>
<option value="GardenCenter"></option>
<option value="GardenNorth"></option>
<option value="MyRoom"></option>
</select>
<input type="submit" value="submit" />
</form>
<div id="chartContainer" style="height: 600px; width: 90%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>










.png)