我的linechar没有画任何线。 这是我的代码:
dataC = new google.visualization.DataTable();
dataC.addColumn('datetime', 'Datum');
dataC.addColumn('number', 'Temp1');
dataC.addColumn('number', 'Temp2');
dataC.addColumn('number', 'Temp3');
dataC.addColumn('number', 'Temp4');
var optionsC = {
title: 'Temperaturen',
curveType: 'function',
legend: { position: 'bottom' },
series: { 4: { type: 'line' }, 3: { type: 'line' }, 2: { type: 'line' }, 1: { type: 'line' }}
};
chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
$.getJSON("https://api.thingspeak.com/channels/" + channel_id + "/feeds.json?api_key=" + api_key + "&results=5", function(json1) {
json1.feeds.forEach(function(feed,i){
//console.log("\n The details of " + i + "th Object are : \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2+"\nField3:" + feed.field3);
my_json = feed;
//console.log(my_json); //Object {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"}
var data = [];
for(var i in my_json)
data.push(my_json [i]);
var data2 = [new Date(data[0]),parseInt(data[2]),parseInt(data[3]),parseInt(data[4]),parseInt(data[5])];
console.log(data2);
dataC.addRow([new Date(data[0]),parseInt(data[2]),parseInt(data[3]),parseInt(data[4]),parseInt(data[5])]);
});
});
//console.log(dataC.length);
chart.draw(dataC, optionsC);
我想在图表中绘制4个温度。 这是怎么了? 我的数组看起来很好... 请帮助
答案 0 :(得分:0)
getJSON
是异步的,这意味着图表是在数据加载之前绘制的
按如下所示将chart.draw
移到getJSON
内...
$.getJSON("https://api.thingspeak.com/channels/" + channel_id + "/feeds.json?api_key=" + api_key + "&results=5", function(json1) {
json1.feeds.forEach(function(feed,i){
//console.log("\n The details of " + i + "th Object are : \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2+"\nField3:" + feed.field3);
my_json = feed;
//console.log(my_json); //Object {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"}
var data = [];
for(var i in my_json)
data.push(my_json [i]);
var data2 = [new Date(data[0]),parseInt(data[2]),parseInt(data[3]),parseInt(data[4]),parseInt(data[5])];
console.log(data2);
dataC.addRow([new Date(data[0]),parseInt(data[2]),parseInt(data[3]),parseInt(data[4]),parseInt(data[5])]);
});
//console.log(dataC.length);
chart.draw(dataC, optionsC);
});