javascript - Google chart redraw onclick -
i trying make chart data selectable through various dropdown menu's , date selector. can't seem find way pass new data in chart on click event. got working far onclick, draws entire new chart. doesn't seem way me.
so there other way this? html:
<div id="piechart" style="width: 450px; height: 500px;"></div> <div class="date-selector-container"> <div class="btn-group"> <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> jaar <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu"> <li><a class="2015-btn" href="#">2015</a></li> <li><a href="#">2014</a></li> <li><a href="#">2013</a></li> </ul> </div>
js:
google.load("visualization", "1", {packages:["corechart"], "callback": drawchart}); google.setonloadcallback(drawchart); function drawchart() { var data = google.visualization.arraytodatatable([ ['task', 'hours per day'], ['work', 1], ['eat', 22], ['commute', 32], ['watch tv', 42], ['sleep', 75] ]); var options = { chartarea: {width:'100%',height:'100%'}, forceiframe: 'false', is3d: 'true', pieslicetext: 'value', slicevisibilitythreshold: 1/20, // > 5% shown. titleposition: 'none' }; var chart = new google.visualization.piechart(document.getelementbyid('piechart')); chart.draw(data, options); } }); //on button click, load new data $(".2015-btn").click(function(){ google.load("visualization", "1", {packages:["corechart"], "callback": drawchart}); 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 = { chartarea: {width:'100%',height:'100%'}, forceiframe: 'false', is3d: 'true', pieslicetext: 'value', slicevisibilitythreshold: 1/20, // > 5% shown. titleposition: 'none' }; var chart = new google.visualization.piechart(document.getelementbyid('piechart')); chart.draw(data, options); } });
change js below.
create chart variable outside drawchart function , instead of creating new chart use everywhere 1 have.
working example here jsfiddle
google.load("visualization", "1", {packages:["corechart"], "callback": drawchart}); google.setonloadcallback(drawchart); var chart; function drawchart() { var data = google.visualization.arraytodatatable([ ['task', 'hours per day'], ['work', 1], ['eat', 22], ['commute', 32], ['watch tv', 42], ['sleep', 75] ]); var options = { chartarea: {width:'100%',height:'100%'}, forceiframe: 'false', is3d: 'true', pieslicetext: 'value', slicevisibilitythreshold: 1/20, // > 5% shown. titleposition: 'none' }; chart = new google.visualization.piechart(document.getelementbyid('piechart')); chart.draw(data, options); } $(document).ready(function(){ //on button click, load new data $(".2015-btn").click(function() { var data = google.visualization.arraytodatatable([ ['task', 'hours per day'], ['work', 11], ['eat', 2], ['commute', 2], ['watch tv', 2], ['sleep', 7] ]); var options = { chartarea: { width: '100%', height: '100%' }, forceiframe: 'false', is3d: 'true', pieslicetext: 'value', slicevisibilitythreshold: 1 / 20, // > 5% shown. titleposition: 'none' }; chart.draw(data, options); }); });
Comments
Post a Comment