load google charts through an ajax call

前端 未结 1 827
慢半拍i
慢半拍i 2021-02-06 05:19

I\'m trying to call the google chart after clicking on a link. This is what my function looks like:

function getGraphData(id) {

    var ajax_url = \'

        
相关标签:
1条回答
  • 2021-02-06 06:02

    You can load the Google Charts with Ajax call using below code.

    $.ajax({
      url: 'https://www.google.com/jsapi?callback',
      cache: true,
      dataType: 'script',
      success: function(){
        google.load('visualization', '1', {packages:['corechart'], 'callback' : function()
          {
    
                $.ajax({
                     type: "POST",
                     dataType: "json",
                     data: {id: YOURIDHERE},
                     url: '<?=URL?>' + 'ajaxlibrary/get-charts',
                     success: function(jsondata) {
                        var data = google.visualization.arrayToDataTable(jsondata);
    
                        var options = {title: 'My Daily Activities'};
    
                        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                        chart.draw(data, options);
                     }
                });    
            ]);
    
          }
        });
        return true;
      }
    });
    

    you can load any other chart types instead of just the corechart using Google API.

    0 讨论(0)
提交回复
热议问题