chartjs-plugin-zoom icon indicating copy to clipboard operation
chartjs-plugin-zoom copied to clipboard

Programmatically Set Zoom

Open theouteredge opened this issue 8 years ago • 7 comments

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.

theouteredge avatar Dec 03 '18 18:12 theouteredge

put the references of the charts into an array and set the zoom with a forEach loop?

Karlito88 avatar Dec 13 '18 15:12 Karlito88

@Karlito88 Do you have an example on how to actually programmatically zoom?

dmrickey avatar Dec 18 '18 19:12 dmrickey

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).

ub3rman123 avatar Jan 11 '19 15:01 ub3rman123

Thanks for sharing how to do this. I'm going to close this issue since I think that explained it pretty well

benmccann avatar Feb 04 '19 15:02 benmccann

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

`

maxwell-oroark avatar Nov 07 '20 21:11 maxwell-oroark

Also why not consider just adding to the code base and have a new method in the official API? 🙂

maxwell-oroark avatar Nov 07 '20 21:11 maxwell-oroark

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.

charleskiefer01 avatar Nov 07 '20 21:11 charleskiefer01