0

我使用谷歌API顯示的圖表,我得到一個圖表,但它沒有顯示在圖表中該行使用谷歌API來顯示實際的圖表,無法在Javascript

這是我與嘗試代碼,我使用的圖表陣列中的數據,

var jsonlength = data.feed.entry.length; 
var timestamp = new Array(jsonlength); 
var temperature = new Array(jsonlength); 
var tempid = new Array(jsonlength); 

for (var i = 0; i < jsonlength; i++) { 
    timestamp[i] = ((data.feed.entry[i].gsx$timestamp.$t)); 
    temperature[i] = ((data.feed.entry[i].gsx$temperaturevalue.$t)); 
} 

google.charts.load('current', {packages: ['corechart', 'line']}); 
google.charts.setOnLoadCallback(drawBasic); 

function drawBasic() { 
     var data = new google.visualization.DataTable(); 
     data1.addColumn('number', 'X'); 
     data1.addColumn('number', 'X'); 


    for (var i = 0; i < jsonlength; i++) { 
     console.log(i); 
     data1.addRows(i,data.feed.entry[i].gsx$temperaturevalue.$t); 
     //Here I can display all those values, But still not getting the chart, Though I have given proper values, Help me here 
     console.log(data.feed.entry[i].gsx$temperaturevalue.$t); 
    }   
     var options = { 
     hAxis: { 
      title: 'Date' 
     }, 
     vAxis: { 
      title: 'Temperature' 
     } 
     }; 
     var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
     chart.draw(data1, options); 

這是我得到不換行輸出,請幫助我,我是新來這個

Output without a line in the graph

回答

0

最有可能是由於'string'值與number

試試這個...

temperature[i] = (parseFloat(data.feed.entry[i].gsx$temperaturevalue.$t)); 

這裏...

for (var i = 0; i < jsonlength; i++) { 
    timestamp[i] = ((data.feed.entry[i].gsx$timestamp.$t)); 
    temperature[i] = (parseFloat(data.feed.entry[i].gsx$temperaturevalue.$t)); 
} 

編輯

在這裏,您可以創建data

var data = new google.visualization.DataTable(); 

但是然後你將列添加到data1 ???

data1.addColumn('number', 'X'); 
    data1.addColumn('number', 'X'); 

嘗試同步起來變量名和檢查以下addRow ...

var data1 = new google.visualization.DataTable(); 
    data1.addColumn('number', 'X'); 
    data1.addColumn('number', 'X'); 


for (var i = 0; i < jsonlength; i++) { 

    // use addRow -- which takes an array 
    data1.addRow([i,data.feed.entry[i].gsx$temperaturevalue.$t]); 

}   

    var options = { 
    hAxis: { 
     title: 'Date' 
    }, 
    vAxis: { 
     title: 'Temperature' 
    } 
    }; 

    var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
    chart.draw(data1, options); 
+0

沒有,它不工作 –

+0

先生,我已經修改我的代碼,請看看它 –

+0

看到__EDIT__以上... – WhiteHat