Can I disable/enable Scrolling in Google Chart with a Button?


Can I disable/enable Scrolling in Google Chart with a Button?



I have a Google line chart on my site, which show the school grades of a student over the course of his school years. I have mouse wheel zooming enabled.


explorer: {
axis: 'horizontal',
maxZoomIn: 0.25,
maxZoomOut: 4
},



Now I would like to give the user the possibility to disable scrolling with a button. Is it possible to remove the 'explorer' part or just disable scrolling through a variable?




1 Answer
1



sure, let's start by moving the explorer option to a separate variable...


var options = {
pointSize: 4
};

var explorer = {
axis: 'horizontal',
maxZoomIn: 0.25,
maxZoomOut: 4
};



then we can enable explorer like so...


options.explorer = explorer;



to disable...


options.explorer = null;



keep in mind, anytime an option is changed, the chart needs to be re-drawn...



see following working snippet...




google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['x', 'y'],
[0, 0],
[1, 1],
[2, 2],
[3, 3],
[4, 4]
]);

var options = {
pointSize: 4
};

var explorer = {
axis: 'horizontal',
maxZoomIn: 0.25,
maxZoomOut: 4
};

$('#explorer-option').on('click', function () {
if ($('#explorer-label').html() === 'Enable') {
options.explorer = explorer;
drawChart();

$('#explorer-label').html('Disable');
$('#explorer-icon').removeClass('ui-icon-circle-check');
$('#explorer-icon').addClass('ui-icon-circle-close');
} else {
options.explorer = null;
drawChart();

$('#explorer-label').html('Enable');
$('#explorer-icon').removeClass('ui-icon-circle-close');
$('#explorer-icon').addClass('ui-icon-circle-check');
}
});

var container = document.getElementById('chart_div');

drawChart();
function drawChart() {
var chart = new google.visualization.LineChart(container);
chart.draw(data, options);
}
});


https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
https://www.gstatic.com/charts/loader.js
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"/>
<button class="ui-button ui-widget ui-corner-all" id="explorer-option">
<span id="explorer-icon" class="ui-icon ui-icon-circle-check"></span>&nbsp;<span id="explorer-label">Enable</span>
</button>





Thank you that helped alot. the only problem is, that it only works once :/
– David Stalder
Jun 29 at 8:08





thanks, i forgot there is a bug with using explorer, the chart must be rebuilt to re-enable explorer, see comments here -- changed answer above, working now multiple times...
– WhiteHat
Jun 29 at 11:17







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift