I'm working on plotting a line graph using a JSON response from my MongoDB, but I keep encountering an error that indicates something might be wrong with my code structure.
Here's what I have attempted:
myurl = "/api/humidity"
$(function() {
$.ajax({
type: "GET",
url: myurl,
cache: false,
dataType : "json",
success: function (data1) {
$.each(data1.when, function(position, when) {
if (data1.when) {
chartData.labels.push(data1.when);
} else {
chartData.labels.push('');
}
chartData.datasets[0].data.push(data.message);
});
}
})
})
var chartData = {
labels: [],
datasets: [
{
label: "Humidity",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: []
},
]
};
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx).Line (chartData, {
});
I hope to optimize my code to iterate through all items in my database smoothly in the future.
This is how my JSON data is structured:
[{
"_id": "585b544f5c86b6c8537c34d6",
"topic": "Humidity",
"message": 23,
"when": "2016-12-22T04:19:27.000Z"
}, {
"_id": "585b54505c86b6c8537c34d7",
"topic": "Humidity",
"message": 23,
"when": "2016-12-22T04:19:28.000Z"
}, {
"_id": "585b54515c86b6c8537c34d8",
"topic": "Humidity",
"message": 23,
"when": "2016-12-22T04:19:29.000Z"
}, {
"_id": "585b54525c86b6c8537c34d9",
"topic": "Humidity",
"message": 23,
"when": "2016-12-22T04:19:30.000Z"
}, {
"_id": "585b54535c86b6c8537c34da",
"topic": "Humidity",
"message": 23,
"when": "2016-12-22T04:19:31.000Z"}]
Any assistance or suggestions would be greatly appreciated.