Thrukal
Thrukal

Reputation: 270

Load Google Charts via Ajax

I want to draw several pie charts to my site using the Google Charts API. This works fine so long as everything is placed in the same document. But now I want to load the chart-code via Ajax for example in response to some user action. But that doesn't work (with simple jQuery-Calls). How can I do that?

     <script type="text/javascript">
         google.load("visualization", "1", {packages:["corechart"]});
         google.setOnLoadCallback(drawChart);

         function first(){
         var my_chart = new google.visualization.DataTable();
         //chart code

       var chart_pie= new google.visualization.PieChart(document.getElementById('meins'));
       chart_pie.draw(my_chart, options);
     }

     function drawChart() {
         first();
         second();
         //more
     }

   </script>

Upvotes: 2

Views: 7999

Answers (1)

user1419950
user1419950

Reputation: 326

for example,a handler for the click event to an element id of 'target'.

At first

google.load("visualization", "1", {packages:["corechart"]});

next

$('#target').on('click',function(){               

    $.ajax({
        url: url,
        data: "",
        success: function(resultData){
            google.setOnLoadCallback(drawChart(resultData));                                                   
        }
    });     
});

Upvotes: 6

Related Questions