I have a somewhat similiar case. I want to create a dynamic Chart from an Mysql-DB
Unfortunatelly, I get the Msg “TypeError: chart is undefined”
I am “only” a Graphic Designer and no programmer :()
=============================================================================================================
SCRIPT
=============================================================================================================
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
<script type="text/javascript">
var updateInterval = 100;
var chart;
var dps;
$(document).ready(function () {
$.getJSON("testdata.php", function (result) {
dps = result;
var chart = new CanvasJS.Chart("chartContainer", {
title:{
text: "Test",
},
axisY:{
minimum: 0,
maximum: 1000
},
data: [
{
type: "stackedBar",
dataPoints: dps
}
]
});
chart.render();
});
var updateChart = function () {
$.getJSON("testdata.php", function (result) {
dps.splice(0, dps.length);
$.each(result, function (index, value) {
dps.push(value);
});
});
chart.render();
};
setInterval(function(){updateChart()}, updateInterval);
});
</script>
</head>
<body>
BODY
<div id="chartContainer" style="width: 800px; height: 380px;"></div>
</body>
</html>
=============================================================================================================
DATA
=============================================================================================================
<?php
header('Content-Type: application/json');
$con = mysqli_connect("localhost","wp1","QPZBtAe7","wp1");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to DataBase: " . mysqli_connect_error();
}else
{
$data_points = array();
$result = mysqli_query($con, "SELECT id,Laserschneider_Druckluft_Volumenstrom_Nm3h FROM blb ORDER BY id DESC LIMIT 500");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['id'] , "y" => $row['Laserschneider_Druckluft_Volumenstrom_Nm3h']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
}
mysqli_close($con);