71 lines
1.3 KiB
JavaScript
71 lines
1.3 KiB
JavaScript
function fecthData(dataset) {
|
|
let url = 'http://iot.drbx.me/api?dataset='+dataset
|
|
return new Promise((resolve, reject) => {
|
|
fetch(url).then((response) => {
|
|
var contentType = response.headers.get("content-type");
|
|
if(contentType && contentType.indexOf("application/json") !== -1) {
|
|
return response.json().then(function(json) {
|
|
resolve(json)
|
|
});
|
|
} else {
|
|
reject()
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
function generateDatasetFromJson(data,color, label, path)
|
|
{
|
|
return {
|
|
datasets: [ {
|
|
label: label,
|
|
data:data.map((data) => {
|
|
content
|
|
return {
|
|
y:getByPath(data,path,0),
|
|
t:new Date(data.content.generatedDate)
|
|
}
|
|
}),
|
|
backgroundColor: color
|
|
}]}
|
|
}
|
|
|
|
function drawGraph(data)
|
|
{
|
|
var ctx = document.getElementById('myChart').getContext('2d');
|
|
console.log(data)
|
|
var chart = new Chart(ctx, {
|
|
type: 'line',
|
|
data: data,
|
|
options: {
|
|
scales: {
|
|
xAxes: [{
|
|
type: 'time',
|
|
time: {
|
|
unit: 'minute',
|
|
|
|
}
|
|
}]
|
|
},
|
|
}
|
|
})
|
|
}
|
|
|
|
function getByPath(obj, path, def) {
|
|
|
|
path = path
|
|
.replace(/\[/g, '.')
|
|
.replace(/]/g, '')
|
|
.split('.');
|
|
|
|
path.forEach(function (level) {
|
|
obj = obj[level];
|
|
});
|
|
|
|
if (obj === undefined) {
|
|
return def;
|
|
}
|
|
|
|
return obj;
|
|
}
|