Programmatically Set Zoom
Hi,
Is it possible to programmatically set the zoom for a chart?
I have a couple of charts on my page with the same x scale and when you zoom on one chart I want to be able to zoom on all charts. I also pick out sections of interest and would like to be able to zoom on these when someone clicks on an item.
Thanks.
put the references of the charts into an array and set the zoom with a forEach loop?
@Karlito88 Do you have an example on how to actually programmatically zoom?
I set this up for my own:
helpers.each(chartInstance.scales, function(scale, id) {
if (id == "x-axis-0") {
var tickOptions = scale.options.ticks;
if (tickOptions) {
var labels = scale.chart.data.labels;
scale.options.ticks.min = rangeMinLimiter(scale.options, labels[min]);
scale.options.ticks.max = rangeMaxLimiter(scale.options, labels[max]);
}
scale.options = helpers.configMerge(scale.options, scale.originalOptions);
}
});
helpers.each(chartInstance.data.datasets, function(dataset, id) {
dataset._meta = null;
});
chartInstance.update();
};
Insert it below chartInstance.resetZoom. Mine only handles X-axis zooms since I have it locked to that direction. Call like graph.setZoom(minIndex, maxIndex).
Thanks for sharing how to do this. I'm going to close this issue since I think that explained it pretty well
Broader snippet of code in case anyone gets confused about the slightly incomplete one above:
`
afterInit: function(chartInstance) {
chartInstance.resetZoom = function() {
storeOriginalOptions(chartInstance);
var originalOptions = chartInstance.$zoom._originalOptions;
helpers.each(chartInstance.scales, function(scale) {
var timeOptions = scale.options.time;
var tickOptions = scale.options.ticks;
if (originalOptions[scale.id]) {
if (timeOptions) {
timeOptions.min = originalOptions[scale.id].time.min;
timeOptions.max = originalOptions[scale.id].time.max;
}
if (tickOptions) {
tickOptions.min = originalOptions[scale.id].ticks.min;
tickOptions.max = originalOptions[scale.id].ticks.max;
}
} else {
if (timeOptions) {
delete timeOptions.min;
delete timeOptions.max;
}
if (tickOptions) {
delete tickOptions.min;
delete tickOptions.max;
}
}
});
chartInstance.update();
};
// NEW CODE START FOR PROGRAMATIC ZOOM
chartInstance.setZoom = function(min, max) {
helpers.each(chartInstance.scales, function(scale, id) {
if (id == "x-axis-0") {
var tickOptions = scale.options.ticks;
if (tickOptions) {
var labels = scale.chart.data.labels;
scale.options.ticks.min = rangeMinLimiter(scale.options, labels[min]);
scale.options.ticks.max = rangeMaxLimiter(scale.options, labels[max]);
}
scale.options = helpers.configMerge(scale.options, scale.originalOptions);
}
});
helpers.each(chartInstance.data.datasets, function(dataset, id) {
dataset._meta = null;
});
chartInstance.update();
}
// NEW CODE END FOR PROGRAMATIC ZOOM
`
Also why not consider just adding to the code base and have a new method in the official API? 🙂
I'm just too lazy to clean it up for public consumption! People would be horrified to see the hackery that I've made of the rest of it.