weixin_33749131 2017-08-30 12:58 采纳率: 0%
浏览 11

Google图表未显示

I am getting some data from an ajax call and would like to display a chart on success of the ajax call. to check the functionality i am currently using a default chart and trying to display some static data on the chart. but when i click on the element 'asSvSs' The developer tools inspector shows the chart data but nothing is displayed on the page.

What am I doing wrong ?

$(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);

}
        }
    });
}
});
  • 写回答

1条回答 默认 最新

  • weixin_33713503 2017-08-30 13:04
    关注

    recommend loading google first,
    which only needs to happen once per page load,
    not once per chart

    try setup similar to following...

    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);
              }
          });
        }
      });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿