php выполняется на сервере, поэтому он будет запускаться только один раз при загрузке страницы.
, чтобы обновить диаграмму без перезагрузки страницы,
вам нужно будет отделить php от html / javascript.
сохранить php в отдельный файл.
вам необходимо включить остальные данные в php,
, включая заголовки столбцов.
см. Следующий фрагмент для php.
для примера, мы назовем файл -> getdata.php
<?php
$servername = "localhost";
$username = "u644759843_miki";
$password = "plantaze2020!";
$dbname = "u644759843_plantazeDB";
// Create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($servername, $username, $password, $dbname);
$conn->set_charset('utf8mb4');
$sql = "SELECT ProductPurity FROM `Precizno ProductPurity` ORDER BY TimesStamp DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
// create data array
$data = [];
$data[] = ["Label", "Value"];
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$data[] = ["ProductPurity", $row["ProductPurity"]];
}
mysqli_close($conn);
// write data array to page
echo json_encode($data);
?>
далее, сохраните html / javascript в свой собственный файл.
для примера, мы назовем файл -> chart.html
, чтобы получить данные из php,
мы будем использовать jquery ajax.
см. Следующий фрагмент ...
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load('current', {
packages: ['gauge']
}).then(function () {
var options = {
width: 400, height: 120,
redFrom: 90, redTo: 100,
yellowFrom:75, yellowTo: 90,
minorTicks: 5
};
var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
drawChart();
function drawChart() {
$.ajax({
url: 'getdata.php',
dataType: 'json'
}).done(function (jsonData) {
// use response from php for data table
var data = google.visualization.arrayToDataTable(jsonData);
chart.draw(data, options);
// draw again in 5 seconds
window.setTimeout(drawChart, 5000);
});
}
});
</script>
</head>
<body>
<div id="chart_div" style="width: 400px; height: 120px;"></div>
</body>
</html>
РЕДАКТИРОВАТЬ
необходимо убедиться, что столбец «Значение» ...
- это число -> 99.9594
не строка -> "99.9594"
вы можете преобразовать, используя -> (float)
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$data[] = ["ProductPurity", (float) $row["ProductPurity"]];
}
и / или используйте константу JSON_NUMERIC_CHECK
в операторе кодирования ...
echo json_encode($data, JSON_NUMERIC_CHECK);