I'm able to plot following chart just out of the box (https://developers.google.com/chart/interactive/docs/gallery/piechart), but when I try to load it via jquery after clicking a button it doesn't work. I read a lot of questions/answers here in stackoverflow and elsewhere in the Internet, but somehow they didin't work to me and I was unable to solve the issue. Nothing is plotted on the screen after clicking the button.
Here the last code I tried:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
$(document).ready(function(){
$(".btn").click(function(){
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
});
});
</script>
</head>
<body>
<button class="btn">Plot chart!</button>
<div id="piechart" class="piechart">Chart would be plotted here</div>
</body>
</html>
I would appreciate any help!