I would like to draw a graph using RPi
, MySQLi
, PHP
and Chart.js. To do that I followed this tutorial:
For now, my graph is empty. X axis is 'undefined' and Y axis has got a wrong range. In my case X axis should show time format values(HH:MM:SS). Does anybody know how to properly set received time data from MySQL for Chart.js?
I also checked this solution but it did not work for me:
Thank you!
index.php
<?php
$servername = "localhost";
$username = "airqualitysensor";
$password = "password";
$database = "temperatura";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error){
die("Connection failed: ". $conn->connect_error);
exit();
}
$sql = "SELECT * FROM temperaturaodczyt";
$result = $conn->query($sql);
$data = array();
foreach($result as $row){
$data[] = $row;
}
$result->close();
$conn->close();
print json_encode($data);
?>
app.js
$(document).ready(function(){
$.ajax({
url:"http://localhost/index.php",
method: "GET",
success: function(data) {
console.log(data);
var tczas = [];
var temperatura = [];
for (var i in data){
tczas.push(data[i].tczas);
temperatura.push(data[i].temperatura);
}
var chartdata = {
labels: tczas,
datasets : [
{
label : "Zmierzona temperatura",
fill: false,
lineTension: 0.1,
backgroundColor: "rgb(200, 200, 200, 0.75)",
borderColor: "rgb(200, 200, 200, 0.75)",
hoverBackgroundColor: "rgb(200, 200, 200, 1)",
hoverBorderColor: "rgb(200, 200, 200, 0.75)",
data: temperatura
}
]
};
var ctx = $("#mycanvas");
var lineGraph = new Chart(ctx, {
type: "line",
data: chartdata
});
},
error: function(data) {
}
});
});