I have json data in DRF which run locally at url: http://127.0.0.1:8000/api/data/ like this :
[
{
"id": 2,
"timestamp": "2020-03-15T11:46:10+07:00",
"vibration": 3,
"moisture": 70,
"gps_latitude": "-7.7713794",
"gps_longitude": "110.3753111",
"gyro_x": 6.58,
"gyro_y": 85.0,
"gyro_z": -3.9,
"accelero_x": 6.58,
"accelero_y": 85.0,
"accelero_z": -3.9,
"displacement": 10,
"node_id": 1
},
{
"id": 6,
"timestamp": "2020-03-15T12:00:10+07:00",
"vibration": 3,
"moisture": 75,
"gps_latitude": "-7.7713794",
"gps_longitude": "110.3753111",
"gyro_x": 6.58,
"gyro_y": 85.0,
"gyro_z": -3.9,
"accelero_x": 6.58,
"accelero_y": 85.0,
"accelero_z": -3.9,
"displacement": 10,
"node_id": 1
},
{
"id": 7,
"timestamp": "2020-03-15T13:00:10+07:00",
"vibration": 3,
"moisture": 75,
"gps_latitude": "-7.7713794",
"gps_longitude": "110.3753111",
"gyro_x": 6.58,
"gyro_y": 85.0,
"gyro_z": -3.9,
"accelero_x": 6.58,
"accelero_y": 85.0,
"accelero_z": -3.9,
"displacement": 10,
"node_id": 1
},
{
"id": 8,
"timestamp": "2020-03-16T07:00:00+07:00",
"vibration": 3,
"moisture": 80,
"gps_latitude": "-7.7713794",
"gps_longitude": "110.3753111",
"gyro_x": 6.58,
"gyro_y": 85.0,
"gyro_z": -3.9,
"accelero_x": 6.58,
"accelero_y": 85.0,
"accelero_z": -3.9,
"displacement": 10,
"node_id": 1
}
]
I want to make a line chart based on all the data on field "vibration" and "moisture". And i've tried it with codes like this :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Try Chart</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
<script>
function dspChrt(hum, vibrate) {
hum = loadChart().hum;
vibrate = loadChart().vibrate;
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
datasets: [{
label: 'Humidity',
data: hum, // json value received used in method
backgroundColor: "rgba(153,255,51,0.4)"
}, {
label: 'Vibration',
data: vibrate,
backgroundColor: "rgba(255,153,0,0.4)"
}]
}
});
}
</script>
<script>
var myVar = setInterval(loadChart, 60000); // updates chart every one minute
function loadChart()
{
var data, hum, vibrate;
var requestURL = 'http://127.0.0.1:8000/api/data'; //URL of the JSON data
var request = new XMLHttpRequest({mozSystem: true}); // create http request
request.onreadystatechange = function() {
if(request.readyState == 4 && request.status == 200) {
data = JSON.parse(request.responseText);
for (var i=0; i<data.length;i++) {
hum = data[i].moisture;
vibrate = data[i].vibration;
}
console.log('hum', hum);
console.log('vibrate', vibrate);
console.log('data', data);
return data,hum,vibrate;
dspChrt(hum, vibrate);
}
}
request.open('GET', requestURL);
request.send(); // send the request
}
</script>
</head>
<body onload="loadChart()">
<div class="container">
<h2>Try Chart</h2>
<div>
<canvas id="myChart"></canvas>
</div>
</div>
</body>
</html>
But it doesn't work, the data doesn't appear on the chart and in the inspect element of the web page only return the last data of "vibration" and "moisture". I want all the data in "vibration" and "moisture" to be plotted on the chart, not only the last one. this is the inspect element of the webpage : inspect-element-chart I think this is happen because format of the json. Anybody know how to fix it? I am still a newbie in programming. Thank you before
