As you're only changing the value of isStacked you need only add an event handler to a button to change a variable then redraw the barchart. Here's the code to do what you want::
<script type="text/javascript">
google.charts.load('current', {
packages: ['corechart'],
callback: initChart
});
function initChart() {
var button;
var chart;
var data;
var showChart = 'Show as Absolute';
var data = google.visualization.arrayToDataTable([
['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
'Western', 'Literature', { role: 'annotation' } ],
['2010', 10, 24, 20, 32, 18, 5, ''],
['2020', 16, 22, 23, 30, 16, 9, ''],
['2030', 28, 19, 29, 30, 12, 13, '']
]);
chart = new google.visualization.ChartWrapper({
containerId: 'chart_div',
dataTable: data
});
button = document.getElementById('btnSwitch');
button.addEventListener('click', switchChart, false);
// draw initial chart
switchChart();
function switchChart() {
button.value = showChart;
var barType;
showChart = (showChart === 'Show as Absolute') ? 'Show as Percent' : 'Show as Absolute';
if (showChart.indexOf("Absolute") >-1){
barType = true;
}
else {
barType = "percent";
}
drawChart(barType);
}
function drawChart(barType) {
var options = {
width: 600,
height: 400,
legend: { position: 'top', maxLines: 3 },
isStacked: barType,
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
}
</script>
<input type="button" id="btnSwitch" />
<div id="chart_div"></div>
You can use almost any element to do things like this, a dropdown list for example.