google.charts.load('current', {
callback: function() {
var data = new google.visualization.DataTable({
"cols": [
{"label": "X", "type": "number"},
{"label": "Y", "type": "number"}
],
"rows": [
{"c": [{"v": 0}, {"v": 0}]},
{"c": [{"v": 1}, {"v": 1}]},
{"c": [{"v": 2}, {"v": 2}]},
{"c": [{"v": 3}, {"v": 4}]},
{"c": [{"v": 4}, {"v": 8}]},
{"c": [{"v": 5}, {"v": 16}]},
{"c": [{"v": 6}, {"v": 32}]},
{"c": [{"v": 7}, {"v": 64}]},
{"c": [{"v": 8}, {"v": 128}]},
{"c": [{"v": 9}, {"v": 256}]}
]
});
var options = {
explorer: {
actions: ['dragToZoom', 'rightClickToReset'],
axis: 'horizontal',
keepInBounds: true
},
hAxis: {
title: 'X'
},
pointSize: 3,
vAxis: {
title: 'Y'
}
};
var chartDiv = document.getElementById('chart_div');
var chart = new google.visualization.LineChart(chartDiv);
document.getElementById('reset-chart').addEventListener('click', function() {
setRange();
}, false);
document.getElementById('update-chart').addEventListener('click', function() {
setRange(getCoords());
}, false);
function getCoords() {
var chartLayout = chart.getChartLayoutInterface();
var chartBounds = chartLayout.getChartAreaBoundingBox();
return {
x: {
min: chartLayout.getHAxisValue(chartBounds.left),
max: chartLayout.getHAxisValue(chartBounds.width + chartBounds.left)
},
y: {
min: chartLayout.getVAxisValue(chartBounds.top),
max: chartLayout.getVAxisValue(chartBounds.height + chartBounds.top)
}
};
}
function setRange(coords) {
options.hAxis.viewWindow = {};
options.vAxis.viewWindow = {};
if (coords) {
options.hAxis.viewWindow.min = coords.x.min;
options.hAxis.viewWindow.max = coords.x.max;
options.vAxis.viewWindow.min = coords.y.min;
options.vAxis.viewWindow.max = coords.y.max;
}
chart.draw(data, options);
}
chart.draw(data, options);
},
packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<input id="update-chart" type="button" value="Redraw Chart" />
<input id="reset-chart" type="button" value="Reset Chart" />
<div id="chart_div"></div>