All files plugin.js

33.5% Statements 131/391
30.13% Branches 72/239
43.9% Functions 18/41
33.5% Lines 131/391

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724          1x     1x     1x 1x   1x                               16x 16x     16x     16x 16x         16x 16x 16x 16x     16x   16x 15x 15x   1x 1x 1x         5x 5x 10x 10x     5x 10x                       26x   26x 22x 4x 4x             6x             6x       6x             6x                                                                                       6x 6x   6x 6x 6x   6x 6x   6x 6x                                   6x 6x 6x                         5x 5x             5x   5x 5x   5x       5x         5x     5x 10x 3x 3x 7x   3x 3x       5x           5x     5x                                                                                                                                                                                                                                                                       1x 1x 1x 1x 1x 1x 1x 1x   1x 1x     1x         6x                                                                                 10x       6x     6x 6x   6x 6x   6x 5x 5x     6x             6x 5x       5x   5x   5x 5x 5x   5x 5x 5x   5x 5x     5x 5x   5x 5x       5x   5x 5x 5x 5x   5x 5x 5x   5x         5x         6x 6x                                                                         6x                                                                                                                                                                                                           11x   11x                                                                                   6x     6x 6x   6x 6x 6x 6x 6x   6x   6x 6x                       1x    
'use strict';
 
import Chart from 'chart.js';
import Hammer from 'hammerjs';
 
var helpers = Chart.helpers;
 
// Take the zoom namespace of Chart
var zoomNS = Chart.Zoom = Chart.Zoom || {};
 
// Where we store functions to handle different scale types
var zoomFunctions = zoomNS.zoomFunctions = zoomNS.zoomFunctions || {};
var panFunctions = zoomNS.panFunctions = zoomNS.panFunctions || {};
 
Chart.Zoom.defaults = Chart.defaults.global.plugins.zoom = {
	pan: {
		enabled: false,
		mode: 'xy',
		speed: 20,
		threshold: 10
	},
	zoom: {
		enabled: false,
		mode: 'xy',
		sensitivity: 3,
		speed: 0.1
	}
};
 
function resolveOptions(chart, options) {
	var deprecatedOptions = {};
	Iif (typeof chart.options.pan !== 'undefined') {
		deprecatedOptions.pan = chart.options.pan;
	}
	Iif (typeof chart.options.zoom !== 'undefined') {
		deprecatedOptions.zoom = chart.options.zoom;
	}
	var props = chart.$zoom;
	options = props._options = helpers.merge({}, [options, deprecatedOptions]);
 
	// Install listeners. Do this dynamically based on options so that we can turn zoom on and off
	// We also want to make sure listeners aren't always on. E.g. if you're scrolling down a page
	// and the mouse goes over a chart you don't want it intercepted unless the plugin is enabled
	var node = props._node;
	var zoomEnabled = options.zoom && options.zoom.enabled;
	var dragEnabled = options.zoom.drag;
	Iif (zoomEnabled && !dragEnabled) {
		node.addEventListener('wheel', props._wheelHandler);
	} else {
		node.removeEventListener('wheel', props._wheelHandler);
	}
	if (zoomEnabled && dragEnabled) {
		node.addEventListener('mousedown', props._mouseDownHandler);
		node.ownerDocument.addEventListener('mouseup', props._mouseUpHandler);
	} else {
		node.removeEventListener('mousedown', props._mouseDownHandler);
		node.removeEventListener('mousemove', props._mouseMoveHandler);
		node.ownerDocument.removeEventListener('mouseup', props._mouseUpHandler);
	}
}
 
function storeOriginalOptions(chart) {
	var originalOptions = chart.$zoom._originalOptions;
	helpers.each(chart.scales, function(scale) {
		Eif (!originalOptions[scale.id]) {
			originalOptions[scale.id] = helpers.clone(scale.options);
		}
	});
	helpers.each(originalOptions, function(opt, key) {
		Iif (!chart.scales[key]) {
			delete originalOptions[key];
		}
	});
}
 
/**
 * @param {string} mode can be 'x', 'y' or 'xy'
 * @param {string} dir can be 'x' or 'y'
 * @param {Chart} chart instance of the chart in question
 */
function directionEnabled(mode, dir, chart) {
	Iif (mode === undefined) {
		return true;
	} else if (typeof mode === 'string') {
		return mode.indexOf(dir) !== -1;
	} else Eif (typeof mode === 'function') {
		return mode({chart: chart}).indexOf(dir) !== -1;
	}
 
	return false;
}
 
function rangeMaxLimiter(zoomPanOptions, newMax) {
	Iif (zoomPanOptions.scaleAxes && zoomPanOptions.rangeMax &&
			!helpers.isNullOrUndef(zoomPanOptions.rangeMax[zoomPanOptions.scaleAxes])) {
		var rangeMax = zoomPanOptions.rangeMax[zoomPanOptions.scaleAxes];
		if (newMax > rangeMax) {
			newMax = rangeMax;
		}
	}
	return newMax;
}
 
function rangeMinLimiter(zoomPanOptions, newMin) {
	Iif (zoomPanOptions.scaleAxes && zoomPanOptions.rangeMin &&
			!helpers.isNullOrUndef(zoomPanOptions.rangeMin[zoomPanOptions.scaleAxes])) {
		var rangeMin = zoomPanOptions.rangeMin[zoomPanOptions.scaleAxes];
		if (newMin < rangeMin) {
			newMin = rangeMin;
		}
	}
	return newMin;
}
 
function zoomCategoryScale(scale, zoom, center, zoomOptions) {
	var labels = scale.chart.data.labels;
	var minIndex = scale.minIndex;
	var lastLabelIndex = labels.length - 1;
	var maxIndex = scale.maxIndex;
	var sensitivity = zoomOptions.sensitivity;
	var chartCenter = scale.isHorizontal() ? scale.left + (scale.width / 2) : scale.top + (scale.height / 2);
	var centerPointer = scale.isHorizontal() ? center.x : center.y;
 
	zoomNS.zoomCumulativeDelta = zoom > 1 ? zoomNS.zoomCumulativeDelta + 1 : zoomNS.zoomCumulativeDelta - 1;
 
	if (Math.abs(zoomNS.zoomCumulativeDelta) > sensitivity) {
		if (zoomNS.zoomCumulativeDelta < 0) {
			if (centerPointer >= chartCenter) {
				if (minIndex <= 0) {
					maxIndex = Math.min(lastLabelIndex, maxIndex + 1);
				} else {
					minIndex = Math.max(0, minIndex - 1);
				}
			} else if (centerPointer < chartCenter) {
				if (maxIndex >= lastLabelIndex) {
					minIndex = Math.max(0, minIndex - 1);
				} else {
					maxIndex = Math.min(lastLabelIndex, maxIndex + 1);
				}
			}
			zoomNS.zoomCumulativeDelta = 0;
		} else if (zoomNS.zoomCumulativeDelta > 0) {
			if (centerPointer >= chartCenter) {
				minIndex = minIndex < maxIndex ? minIndex = Math.min(maxIndex, minIndex + 1) : minIndex;
			} else if (centerPointer < chartCenter) {
				maxIndex = maxIndex > minIndex ? maxIndex = Math.max(minIndex, maxIndex - 1) : maxIndex;
			}
			zoomNS.zoomCumulativeDelta = 0;
		}
		scale.options.ticks.min = rangeMinLimiter(zoomOptions, labels[minIndex]);
		scale.options.ticks.max = rangeMaxLimiter(zoomOptions, labels[maxIndex]);
	}
}
 
function zoomNumericalScale(scale, zoom, center, zoomOptions) {
	var range = scale.max - scale.min;
	var newDiff = range * (zoom - 1);
 
	var centerPoint = scale.isHorizontal() ? center.x : center.y;
	var minPercent = (scale.getValueForPixel(centerPoint) - scale.min) / range;
	var maxPercent = 1 - minPercent;
 
	var minDelta = newDiff * minPercent;
	var maxDelta = newDiff * maxPercent;
 
	scale.options.ticks.min = rangeMinLimiter(zoomOptions, scale.min + minDelta);
	scale.options.ticks.max = rangeMaxLimiter(zoomOptions, scale.max - maxDelta);
}
 
function zoomTimeScale(scale, zoom, center, zoomOptions) {
	zoomNumericalScale(scale, zoom, center, zoomOptions);
 
	var options = scale.options;
	if (options.time) {
		if (options.time.min) {
			options.time.min = options.ticks.min;
		}
		if (options.time.max) {
			options.time.max = options.ticks.max;
		}
	}
}
 
function zoomScale(scale, zoom, center, zoomOptions) {
	var fn = zoomFunctions[scale.type];
	Eif (fn) {
		fn(scale, zoom, center, zoomOptions);
	}
}
 
/**
 * @param chart The chart instance
 * @param {number} percentZoomX The zoom percentage in the x direction
 * @param {number} percentZoomY The zoom percentage in the y direction
 * @param {{x: number, y: number}} focalPoint The x and y coordinates of zoom focal point. The point which doesn't change while zooming. E.g. the location of the mouse cursor when "drag: false"
 * @param {string} whichAxes `xy`, 'x', or 'y'
 * @param {number} animationDuration Duration of the animation of the redraw in milliseconds
 */
function doZoom(chart, percentZoomX, percentZoomY, focalPoint, whichAxes, animationDuration) {
	var ca = chart.chartArea;
	Iif (!focalPoint) {
		focalPoint = {
			x: (ca.left + ca.right) / 2,
			y: (ca.top + ca.bottom) / 2,
		};
	}
 
	var zoomOptions = chart.$zoom._options.zoom;
 
	Eif (zoomOptions.enabled) {
		storeOriginalOptions(chart);
		// Do the zoom here
		var zoomMode = typeof zoomOptions.mode === 'function' ? zoomOptions.mode({chart: chart}) : zoomOptions.mode;
 
		// Which axe should be modified when figers were used.
		var _whichAxes;
		Iif (zoomMode === 'xy' && whichAxes !== undefined) {
			// based on fingers positions
			_whichAxes = whichAxes;
		} else {
			// no effect
			_whichAxes = 'xy';
		}
 
		helpers.each(chart.scales, function(scale) {
			if (scale.isHorizontal() && directionEnabled(zoomMode, 'x', chart) && directionEnabled(_whichAxes, 'x', chart)) {
				zoomOptions.scaleAxes = 'x';
				zoomScale(scale, percentZoomX, focalPoint, zoomOptions);
			} else if (!scale.isHorizontal() && directionEnabled(zoomMode, 'y', chart) && directionEnabled(_whichAxes, 'y', chart)) {
				// Do Y zoom
				zoomOptions.scaleAxes = 'y';
				zoomScale(scale, percentZoomY, focalPoint, zoomOptions);
			}
		});
 
		Iif (animationDuration) {
			chart.update({
				duration: animationDuration,
				easing: 'easeOutQuad',
			});
		} else {
			chart.update(0);
		}
 
		Iif (typeof zoomOptions.onZoom === 'function') {
			zoomOptions.onZoom({chart: chart});
		}
	}
}
 
function panCategoryScale(scale, delta, panOptions) {
	var labels = scale.chart.data.labels;
	var lastLabelIndex = labels.length - 1;
	var offsetAmt = Math.max(scale.ticks.length, 1);
	var panSpeed = panOptions.speed;
	var minIndex = scale.minIndex;
	var step = Math.round(scale.width / (offsetAmt * panSpeed));
	var maxIndex;
 
	zoomNS.panCumulativeDelta += delta;
 
	minIndex = zoomNS.panCumulativeDelta > step ? Math.max(0, minIndex - 1) : zoomNS.panCumulativeDelta < -step ? Math.min(lastLabelIndex - offsetAmt + 1, minIndex + 1) : minIndex;
	zoomNS.panCumulativeDelta = minIndex !== scale.minIndex ? 0 : zoomNS.panCumulativeDelta;
 
	maxIndex = Math.min(lastLabelIndex, minIndex + offsetAmt - 1);
 
	scale.options.ticks.min = rangeMinLimiter(panOptions, labels[minIndex]);
	scale.options.ticks.max = rangeMaxLimiter(panOptions, labels[maxIndex]);
}
 
function panNumericalScale(scale, delta, panOptions) {
	var tickOpts = scale.options.ticks;
	var prevStart = scale.min;
	var prevEnd = scale.max;
	var newMin = scale.getValueForPixel(scale.getPixelForValue(prevStart) - delta);
	var newMax = scale.getValueForPixel(scale.getPixelForValue(prevEnd) - delta);
	// The time scale returns date objects so convert to numbers. Can remove at Chart.js v3
	newMin = newMin.valueOf ? newMin.valueOf() : newMin;
	newMax = newMax.valueOf ? newMax.valueOf() : newMax;
	var rangeMin = newMin;
	var rangeMax = newMax;
	var diff;
 
	if (panOptions.scaleAxes && panOptions.rangeMin &&
			!helpers.isNullOrUndef(panOptions.rangeMin[panOptions.scaleAxes])) {
		rangeMin = panOptions.rangeMin[panOptions.scaleAxes];
	}
	if (panOptions.scaleAxes && panOptions.rangeMax &&
			!helpers.isNullOrUndef(panOptions.rangeMax[panOptions.scaleAxes])) {
		rangeMax = panOptions.rangeMax[panOptions.scaleAxes];
	}
 
	if (newMin >= rangeMin && newMax <= rangeMax) {
		tickOpts.min = newMin;
		tickOpts.max = newMax;
	} else if (newMin < rangeMin) {
		diff = prevStart - rangeMin;
		tickOpts.min = rangeMin;
		tickOpts.max = prevEnd - diff;
	} else if (newMax > rangeMax) {
		diff = rangeMax - prevEnd;
		tickOpts.max = rangeMax;
		tickOpts.min = prevStart + diff;
	}
}
 
function panTimeScale(scale, delta, panOptions) {
	panNumericalScale(scale, delta, panOptions);
 
	var options = scale.options;
	if (options.time) {
		if (options.time.min) {
			options.time.min = options.ticks.min;
		}
		if (options.time.max) {
			options.time.max = options.ticks.max;
		}
	}
}
 
function panScale(scale, delta, panOptions) {
	var fn = panFunctions[scale.type];
	if (fn) {
		fn(scale, delta, panOptions);
	}
}
 
function doPan(chartInstance, deltaX, deltaY) {
	storeOriginalOptions(chartInstance);
	var panOptions = chartInstance.$zoom._options.pan;
	if (panOptions.enabled) {
		var panMode = typeof panOptions.mode === 'function' ? panOptions.mode({chart: chartInstance}) : panOptions.mode;
 
		helpers.each(chartInstance.scales, function(scale) {
			if (scale.isHorizontal() && directionEnabled(panMode, 'x', chartInstance) && deltaX !== 0) {
				panOptions.scaleAxes = 'x';
				panScale(scale, deltaX, panOptions);
			} else if (!scale.isHorizontal() && directionEnabled(panMode, 'y', chartInstance) && deltaY !== 0) {
				panOptions.scaleAxes = 'y';
				panScale(scale, deltaY, panOptions);
			}
		});
 
		chartInstance.update(0);
 
		if (typeof panOptions.onPan === 'function') {
			panOptions.onPan({chart: chartInstance});
		}
	}
}
 
function getXAxis(chartInstance) {
	var scales = chartInstance.scales;
	var scaleIds = Object.keys(scales);
	for (var i = 0; i < scaleIds.length; i++) {
		var scale = scales[scaleIds[i]];
 
		if (scale.isHorizontal()) {
			return scale;
		}
	}
}
 
function getYAxis(chartInstance) {
	var scales = chartInstance.scales;
	var scaleIds = Object.keys(scales);
	for (var i = 0; i < scaleIds.length; i++) {
		var scale = scales[scaleIds[i]];
 
		if (!scale.isHorizontal()) {
			return scale;
		}
	}
}
 
// Store these for later
zoomNS.zoomFunctions.category = zoomCategoryScale;
zoomNS.zoomFunctions.time = zoomTimeScale;
zoomNS.zoomFunctions.linear = zoomNumericalScale;
zoomNS.zoomFunctions.logarithmic = zoomNumericalScale;
zoomNS.panFunctions.category = panCategoryScale;
zoomNS.panFunctions.time = panTimeScale;
zoomNS.panFunctions.linear = panNumericalScale;
zoomNS.panFunctions.logarithmic = panNumericalScale;
// Globals for category pan and zoom
zoomNS.panCumulativeDelta = 0;
zoomNS.zoomCumulativeDelta = 0;
 
// Chartjs Zoom Plugin
var zoomPlugin = {
	id: 'zoom',
 
	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();
		};
 
	},
 
	beforeUpdate: function(chart, options) {
		resolveOptions(chart, options);
	},
 
	beforeInit: function(chartInstance, pluginOptions) {
		chartInstance.$zoom = {
			_originalOptions: {}
		};
		var node = chartInstance.$zoom._node = chartInstance.ctx.canvas;
		resolveOptions(chartInstance, pluginOptions);
 
		var options = chartInstance.$zoom._options;
		var panThreshold = options.pan && options.pan.threshold;
 
		chartInstance.$zoom._mouseDownHandler = function(event) {
			node.addEventListener('mousemove', chartInstance.$zoom._mouseMoveHandler);
			chartInstance.$zoom._dragZoomStart = event;
		};
 
		chartInstance.$zoom._mouseMoveHandler = function(event) {
			if (chartInstance.$zoom._dragZoomStart) {
				chartInstance.$zoom._dragZoomEnd = event;
				chartInstance.update(0);
			}
		};
 
		chartInstance.$zoom._mouseUpHandler = function(event) {
			Iif (!chartInstance.$zoom._dragZoomStart) {
				return;
			}
 
			node.removeEventListener('mousemove', chartInstance.$zoom._mouseMoveHandler);
 
			var beginPoint = chartInstance.$zoom._dragZoomStart;
 
			var offsetX = beginPoint.target.getBoundingClientRect().left;
			var startX = Math.min(beginPoint.clientX, event.clientX) - offsetX;
			var endX = Math.max(beginPoint.clientX, event.clientX) - offsetX;
 
			var offsetY = beginPoint.target.getBoundingClientRect().top;
			var startY = Math.min(beginPoint.clientY, event.clientY) - offsetY;
			var endY = Math.max(beginPoint.clientY, event.clientY) - offsetY;
 
			var dragDistanceX = endX - startX;
			var dragDistanceY = endY - startY;
 
			// Remove drag start and end before chart update to stop drawing selected area
			chartInstance.$zoom._dragZoomStart = null;
			chartInstance.$zoom._dragZoomEnd = null;
 
			var zoomThreshold = options.zoom && options.zoom.threshold || 0;
			Iif (dragDistanceX <= zoomThreshold && dragDistanceY <= zoomThreshold) {
				return;
			}
 
			var chartArea = chartInstance.chartArea;
 
			var zoomOptions = chartInstance.$zoom._options.zoom;
			var chartDistanceX = chartArea.right - chartArea.left;
			var xEnabled = directionEnabled(zoomOptions.mode, 'x', chartInstance);
			var zoomX = xEnabled && dragDistanceX ? 1 + ((chartDistanceX - dragDistanceX) / chartDistanceX) : 1;
 
			var chartDistanceY = chartArea.bottom - chartArea.top;
			var yEnabled = directionEnabled(zoomOptions.mode, 'y', chartInstance);
			var zoomY = yEnabled && dragDistanceY ? 1 + ((chartDistanceY - dragDistanceY) / chartDistanceY) : 1;
 
			doZoom(chartInstance, zoomX, zoomY, {
				x: (startX - chartArea.left) / (1 - dragDistanceX / chartDistanceX) + chartArea.left,
				y: (startY - chartArea.top) / (1 - dragDistanceY / chartDistanceY) + chartArea.top
			}, undefined, zoomOptions.drag.animationDuration);
 
			Iif (typeof zoomOptions.onZoomComplete === 'function') {
				zoomOptions.onZoomComplete({chart: chartInstance});
			}
		};
 
		var _scrollTimeout = null;
		chartInstance.$zoom._wheelHandler = function(event) {
			// Prevent the event from triggering the default behavior (eg. Content scrolling).
			if (event.cancelable) {
				event.preventDefault();
			}
 
			// Firefox always fires the wheel event twice:
			// First without the delta and right after that once with the delta properties.
			if (typeof event.deltaY === 'undefined') {
				return;
			}
 
			var rect = event.target.getBoundingClientRect();
			var offsetX = event.clientX - rect.left;
			var offsetY = event.clientY - rect.top;
 
			var center = {
				x: offsetX,
				y: offsetY
			};
 
			var zoomOptions = chartInstance.$zoom._options.zoom;
			var speedPercent = zoomOptions.speed;
 
			if (event.deltaY >= 0) {
				speedPercent = -speedPercent;
			}
			doZoom(chartInstance, 1 + speedPercent, 1 + speedPercent, center);
 
			clearTimeout(_scrollTimeout);
			_scrollTimeout = setTimeout(function() {
				if (typeof zoomOptions.onZoomComplete === 'function') {
					zoomOptions.onZoomComplete({chart: chartInstance});
				}
			}, 250);
		};
 
		Iif (Hammer) {
			var mc = new Hammer.Manager(node);
			mc.add(new Hammer.Pinch());
			mc.add(new Hammer.Pan({
				threshold: panThreshold
			}));
 
			// Hammer reports the total scaling. We need the incremental amount
			var currentPinchScaling;
			var handlePinch = function(e) {
				var diff = 1 / (currentPinchScaling) * e.scale;
				var rect = e.target.getBoundingClientRect();
				var offsetX = e.center.x - rect.left;
				var offsetY = e.center.y - rect.top;
				var center = {
					x: offsetX,
					y: offsetY
				};
 
				// fingers position difference
				var x = Math.abs(e.pointers[0].clientX - e.pointers[1].clientX);
				var y = Math.abs(e.pointers[0].clientY - e.pointers[1].clientY);
 
				// diagonal fingers will change both (xy) axes
				var p = x / y;
				var xy;
				if (p > 0.3 && p < 1.7) {
					xy = 'xy';
				} else if (x > y) {
					xy = 'x'; // x axis
				} else {
					xy = 'y'; // y axis
				}
 
				doZoom(chartInstance, diff, diff, center, xy);
 
				var zoomOptions = chartInstance.$zoom._options.zoom;
				if (typeof zoomOptions.onZoomComplete === 'function') {
					zoomOptions.onZoomComplete({chart: chartInstance});
				}
 
				// Keep track of overall scale
				currentPinchScaling = e.scale;
			};
 
			mc.on('pinchstart', function() {
				currentPinchScaling = 1; // reset tracker
			});
			mc.on('pinch', handlePinch);
			mc.on('pinchend', function(e) {
				handlePinch(e);
				currentPinchScaling = null; // reset
				zoomNS.zoomCumulativeDelta = 0;
			});
 
			var currentDeltaX = null;
			var currentDeltaY = null;
			var panning = false;
			var handlePan = function(e) {
				if (currentDeltaX !== null && currentDeltaY !== null) {
					panning = true;
					var deltaX = e.deltaX - currentDeltaX;
					var deltaY = e.deltaY - currentDeltaY;
					currentDeltaX = e.deltaX;
					currentDeltaY = e.deltaY;
					doPan(chartInstance, deltaX, deltaY);
				}
			};
 
			mc.on('panstart', function(e) {
				currentDeltaX = 0;
				currentDeltaY = 0;
				handlePan(e);
			});
			mc.on('panmove', handlePan);
			mc.on('panend', function() {
				currentDeltaX = null;
				currentDeltaY = null;
				zoomNS.panCumulativeDelta = 0;
				setTimeout(function() {
					panning = false;
				}, 500);
 
				var panOptions = chartInstance.$zoom._options.pan;
				if (typeof panOptions.onPanComplete === 'function') {
					panOptions.onPanComplete({chart: chartInstance});
				}
			});
 
			chartInstance.$zoom._ghostClickHandler = function(e) {
				if (panning && e.cancelable) {
					e.stopImmediatePropagation();
					e.preventDefault();
				}
			};
			node.addEventListener('click', chartInstance.$zoom._ghostClickHandler);
 
			chartInstance._mc = mc;
		}
	},
 
	beforeDatasetsDraw: function(chartInstance) {
		var ctx = chartInstance.ctx;
 
		Iif (chartInstance.$zoom._dragZoomEnd) {
			var xAxis = getXAxis(chartInstance);
			var yAxis = getYAxis(chartInstance);
			var beginPoint = chartInstance.$zoom._dragZoomStart;
			var endPoint = chartInstance.$zoom._dragZoomEnd;
 
			var startX = xAxis.left;
			var endX = xAxis.right;
			var startY = yAxis.top;
			var endY = yAxis.bottom;
 
			if (directionEnabled(chartInstance.$zoom._options.zoom.mode, 'x', chartInstance)) {
				var offsetX = beginPoint.target.getBoundingClientRect().left;
				startX = Math.min(beginPoint.clientX, endPoint.clientX) - offsetX;
				endX = Math.max(beginPoint.clientX, endPoint.clientX) - offsetX;
			}
 
			if (directionEnabled(chartInstance.$zoom._options.zoom.mode, 'y', chartInstance)) {
				var offsetY = beginPoint.target.getBoundingClientRect().top;
				startY = Math.min(beginPoint.clientY, endPoint.clientY) - offsetY;
				endY = Math.max(beginPoint.clientY, endPoint.clientY) - offsetY;
			}
 
			var rectWidth = endX - startX;
			var rectHeight = endY - startY;
			var dragOptions = chartInstance.$zoom._options.zoom.drag;
 
			ctx.save();
			ctx.beginPath();
			ctx.fillStyle = dragOptions.backgroundColor || 'rgba(225,225,225,0.3)';
			ctx.fillRect(startX, startY, rectWidth, rectHeight);
 
			if (dragOptions.borderWidth > 0) {
				ctx.lineWidth = dragOptions.borderWidth;
				ctx.strokeStyle = dragOptions.borderColor || 'rgba(225,225,225)';
				ctx.strokeRect(startX, startY, rectWidth, rectHeight);
			}
			ctx.restore();
		}
	},
 
	destroy: function(chartInstance) {
		Iif (!chartInstance.$zoom) {
			return;
		}
		var props = chartInstance.$zoom;
		var node = props._node;
 
		node.removeEventListener('mousedown', props._mouseDownHandler);
		node.removeEventListener('mousemove', props._mouseMoveHandler);
		node.ownerDocument.removeEventListener('mouseup', props._mouseUpHandler);
		node.removeEventListener('wheel', props._wheelHandler);
		node.removeEventListener('click', props._ghostClickHandler);
 
		delete chartInstance.$zoom;
 
		var mc = chartInstance._mc;
		Iif (mc) {
			mc.remove('pinchstart');
			mc.remove('pinch');
			mc.remove('pinchend');
			mc.remove('panstart');
			mc.remove('pan');
			mc.remove('panend');
			mc.destroy();
		}
	}
};
 
Chart.plugins.register(zoomPlugin);
export default zoomPlugin;