2017-08-30 92 views
1

我從ajax調用中獲取一些數據,並希望顯示ajax調用成功的圖表。檢查我目前使用默認圖表的功能,並試圖在圖表上顯示一些靜態數據。 但當我點擊元素'asSvSs'開發人員工具檢查器顯示圖表數據,但沒有任何內容顯示在頁面上。Google圖表不顯示

我在做什麼錯?

$(document).on('click', '.asDvSs', function(e){ 

var uid = $('#sesval').data('uid'); 
var apikey = $('#sesval').data('key'); 
var gateway_id = $(this).data('gateway_id'); 
var device_id = $(this).data('device_id'); 
var device_type = $(this).data('device_type'); 

if(uid!= '' && apikey!= '') { 
    $.ajax({ 
     url: basePathUser + apiUrlAnalyticsDeviceSensorData + '/' + gateway_id + '/' + device_id + '/' + device_type, 
     type: 'GET', 
     headers: { 
      'uid': uid, 
      'Api-Key': apikey 
     }, 
     contentType: 'application/json; charset=utf-8;', 
     dataType: 'json', 
     async: false, 
     beforesend: function(xhr){ 
      setRequestHeader("uid", uid); 
      setRequestHeader("Api-Key", apikey); 
     }, 
     success: function(data, textStatus, xhr) { 
    google.charts.load('current', {'packages':['corechart', 'line']}); 
    google.charts.setOnLoadCallback(drawChart); 

    function drawChart() { 

     var data = google.visualization.arrayToDataTable([ 
        ['Year', 'Sales', 'Expenses'], 
        ['2004', 1000,  400], 
        ['2005', 1170,  460], 
        ['2006', 660,  1120], 
        ['2007', 1030,  540] 
      ]); 

     var options = { 
      title: 'Temperature Streaming', 
      width: 900, 
      height: 500, 

     hAxis: { 
       title: 'time' 
      }, 
      vAxis: { 
       title: 'device_value' 
      } 
    }; 

    var chart = new google.visualization.LineChart(document.getElementById('countries')); 

    chart.draw(data, options); 

} 
     } 
    }); 
} 
}); 

回答

0

建議裝載谷歌第一,
只需要每次載入網頁時發生一次,
不是每一次圖表

嘗試建立類似以下...

google.charts.load('current', { 
    callback: loadPage, 
    packages: ['corechart'] 
}); 

function loadPage() { 
    $(document).on('click', '.asDvSs', function(e){ 
    var uid = $('#sesval').data('uid'); 
    var apikey = $('#sesval').data('key'); 
    var gateway_id = $(this).data('gateway_id'); 
    var device_id = $(this).data('device_id'); 
    var device_type = $(this).data('device_type'); 

    if(uid!= '' && apikey!= '') { 
     $.ajax({ 
      url: basePathUser + apiUrlAnalyticsDeviceSensorData + '/' + gateway_id + '/' + device_id + '/' + device_type, 
      type: 'GET', 
      headers: { 
       'uid': uid, 
       'Api-Key': apikey 
      }, 
      contentType: 'application/json; charset=utf-8;', 
      dataType: 'json', 
      async: false, 
      beforesend: function(xhr){ 
       setRequestHeader("uid", uid); 
       setRequestHeader("Api-Key", apikey); 
      }, 
      success: function(data, textStatus, xhr) { 
      var data = google.visualization.arrayToDataTable([ 
       ['Year', 'Sales', 'Expenses'], 
       ['2004', 1000,  400], 
       ['2005', 1170,  460], 
       ['2006', 660,  1120], 
       ['2007', 1030,  540] 
      ]); 

      var options = { 
       title: 'Temperature Streaming', 
       width: 900, 
       height: 500, 
       hAxis: { 
       title: 'time' 
       }, 
       vAxis: { 
       title: 'device_value' 
       } 
      }; 

      var chart = new google.visualization.LineChart(document.getElementById('countries')); 
      chart.draw(data, options); 
      } 
     }); 
    } 
    }); 
} 
+0

嘿的WhiteHat 。非常感謝。它現在似乎正在工作。 – Mihir

+0

是的,我會馬上這樣做。乾杯! – Mihir