10 lines
7.4 KiB
JavaScript
Executable File
10 lines
7.4 KiB
JavaScript
Executable File
/*!
|
|
* chartjs-plugin-zoom
|
|
* https://chartjs.org/
|
|
* Version: 0.4.5
|
|
*
|
|
* Copyright 2016 Evert Timberg
|
|
* Released under the MIT license
|
|
* https://github.com/chartjs/chartjs-plugin-zoom/blob/master/LICENSE.md
|
|
*/
|
|
!function e(t,o,n){function a(r,l){if(!o[r]){if(!t[r]){var m="function"==typeof require&&require;if(!l&&m)return m(r,!0);if(i)return i(r,!0);var u=new Error("Cannot find module '"+r+"'");throw u.code="MODULE_NOT_FOUND",u}var s=o[r]={exports:{}};t[r][0].call(s.exports,function(e){var o=t[r][1][e];return a(o?o:e)},s,s.exports,e,t,o,n)}return o[r].exports}for(var i="function"==typeof require&&require,r=0;r<n.length;r++)a(n[r]);return a}({1:[function(e,t,o){},{}],2:[function(e,t,o){function n(e,t){return void 0===e||"string"==typeof e&&e.indexOf(t)!==-1}function a(e,t,o,n){var a=e.chart.data.labels,i=e.minIndex,r=a.length-1,l=e.maxIndex,m=n.sensitivity,u=e.isHorizontal()?e.left+e.width/2:e.top+e.height/2,s=e.isHorizontal()?o.x:o.y;z.zoomCumulativeDelta=t>1?z.zoomCumulativeDelta+1:z.zoomCumulativeDelta-1,Math.abs(z.zoomCumulativeDelta)>m&&(z.zoomCumulativeDelta<0?(s<=u?i<=0?l=Math.min(r,l+1):i=Math.max(0,i-1):s>u&&(l>=r?i=Math.max(0,i-1):l=Math.min(r,l+1)),z.zoomCumulativeDelta=0):z.zoomCumulativeDelta>0&&(s<=u?i=i<l?i=Math.min(l,i+1):i:s>u&&(l=l>i?l=Math.max(i,l-1):l),z.zoomCumulativeDelta=0),e.options.ticks.min=a[i],e.options.ticks.max=a[l])}function i(e,t,o){var n,a,i=e.options;e.isHorizontal()?(n=e.right-e.left,a=(o.x-e.left)/n):(n=e.bottom-e.top,a=(o.y-e.top)/n);var r=1-a,l=n*(t-1),m=l*a,u=l*r;i.time.min=e.getValueForPixel(e.getPixelForValue(e.firstTick)+m),i.time.max=e.getValueForPixel(e.getPixelForValue(e.lastTick)-u)}function r(e,t,o){var n=e.max-e.min,a=n*(t-1),i=e.isHorizontal()?o.x:o.y,r=(e.getValueForPixel(i)-e.min)/n,l=1-r,m=a*r,u=a*l;e.options.ticks.min=e.min+m,e.options.ticks.max=e.max-u}function l(e,t,o,n){var a=x[e.options.type];a&&a(e,t,o,n)}function m(e,t,o){var a=e.chartArea;o||(o={x:(a.left+a.right)/2,y:(a.top+a.bottom)/2});var i=e.options.zoom;if(i&&g.getValueOrDefault(i.enabled,F.zoom.enabled)){var r=g.getValueOrDefault(e.options.zoom.mode,F.zoom.mode);i.sensitivity=g.getValueOrDefault(e.options.zoom.sensitivity,F.zoom.sensitivity),g.each(e.scales,function(e,a){e.isHorizontal()&&n(r,"x")?l(e,t,o,i):!e.isHorizontal()&&n(r,"y")&&l(e,t,o,i)}),e.update(0)}}function u(e,t,o){var n,a=e.chart.data.labels,i=a.length-1,r=Math.max(e.ticks.length-(e.options.gridLines.offsetGridLines?0:1),1),l=o.speed,m=e.minIndex,u=Math.round(e.width/(r*l));z.panCumulativeDelta+=t,m=z.panCumulativeDelta>u?Math.max(0,m-1):z.panCumulativeDelta<-u?Math.min(i-r+1,m+1):m,z.panCumulativeDelta=m!==e.minIndex?0:z.panCumulativeDelta,n=Math.min(i,m+r-1),e.options.ticks.min=a[m],e.options.ticks.max=a[n]}function s(e,t){var o=e.options;o.time.min=e.getValueForPixel(e.getPixelForValue(e.firstTick)-t),o.time.max=e.getValueForPixel(e.getPixelForValue(e.lastTick)-t)}function c(e,t){var o=e.options.ticks,n=e.start,a=e.end;o.reverse?(o.max=e.getValueForPixel(e.getPixelForValue(n)-t),o.min=e.getValueForPixel(e.getPixelForValue(a)-t)):(o.min=e.getValueForPixel(e.getPixelForValue(n)-t),o.max=e.getValueForPixel(e.getPixelForValue(a)-t))}function d(e,t,o){var n=D[e.options.type];n&&n(e,t,o)}function p(e,t,o){var a=e.options.pan;if(a&&g.getValueOrDefault(a.enabled,F.pan.enabled)){var i=g.getValueOrDefault(e.options.pan.mode,F.pan.mode);a.speed=g.getValueOrDefault(e.options.pan.speed,F.pan.speed),g.each(e.scales,function(e,r){e.isHorizontal()&&n(i,"x")&&0!==t?d(e,t,a):!e.isHorizontal()&&n(i,"y")&&0!==o&&d(e,o,a)}),e.update(0)}}function f(e){var t=e.scales;for(var o in t){var n=t[o];if(!n.isHorizontal())return n}}var v=e("hammerjs");v="function"==typeof v?v:window.Hammer;var h=e("chart.js");h="function"==typeof h?h:window.Chart;var g=h.helpers,z=h.Zoom=h.Zoom||{},x=z.zoomFunctions=z.zoomFunctions||{},D=z.panFunctions=z.panFunctions||{},F=z.defaults={pan:{enabled:!0,mode:"xy",speed:20,threshold:10},zoom:{enabled:!0,mode:"xy",sensitivity:3}};z.zoomFunctions.category=a,z.zoomFunctions.time=i,z.zoomFunctions.linear=r,z.zoomFunctions.logarithmic=r,z.panFunctions.category=u,z.panFunctions.time=s,z.panFunctions.linear=c,z.panFunctions.logarithmic=c,z.panCumulativeDelta=0,z.zoomCumulativeDelta=0;var _={afterInit:function(e){g.each(e.scales,function(e){e.originalOptions=JSON.parse(JSON.stringify(e.options))}),e.resetZoom=function(){g.each(e.scales,function(e,t){var o=e.options.time,n=e.options.ticks;o&&(delete o.min,delete o.max),n&&(delete n.min,delete n.max),e.options=g.configMerge(e.options,e.originalOptions)}),g.each(e.data.datasets,function(e,t){e._meta=null}),e.update()}},beforeInit:function(e){e.zoom={};var t=e.zoom.node=e.chart.ctx.canvas,o=e.options,n=g.getValueOrDefault(o.pan?o.pan.threshold:void 0,z.defaults.pan.threshold);if(o.zoom&&o.zoom.drag?(o.zoom.mode="x",e.zoom._mouseDownHandler=function(t){e.zoom._dragZoomStart=t},t.addEventListener("mousedown",e.zoom._mouseDownHandler),e.zoom._mouseMoveHandler=function(t){e.zoom._dragZoomStart&&(e.zoom._dragZoomEnd=t,e.update(0)),e.update(0)},t.addEventListener("mousemove",e.zoom._mouseMoveHandler),e.zoom._mouseUpHandler=function(t){if(e.zoom._dragZoomStart){var o=e.chartArea,n=f(e),a=e.zoom._dragZoomStart,i=a.target.getBoundingClientRect().left,r=Math.min(a.clientX,t.clientX)-i,l=Math.max(a.clientX,t.clientX)-i,u=l-r,s=o.right-o.left,c=1+(s-u)/s;u>0&&m(e,c,{x:u/2+r,y:(n.bottom-n.top)/2}),e.zoom._dragZoomStart=null,e.zoom._dragZoomEnd=null}},t.addEventListener("mouseup",e.zoom._mouseUpHandler)):(e.zoom._wheelHandler=function(t){var o=t.target.getBoundingClientRect(),n=t.clientX-o.left,a=t.clientY-o.top,i={x:n,y:a};t.deltaY<0?m(e,1.1,i):m(e,.909,i),t.preventDefault()},t.addEventListener("wheel",e.zoom._wheelHandler)),v){var a=new v.Manager(t);a.add(new v.Pinch),a.add(new v.Pan({threshold:n}));var i,r=function(t){var o=1/i*t.scale;m(e,o,t.center),i=t.scale};a.on("pinchstart",function(e){i=1}),a.on("pinch",r),a.on("pinchend",function(e){r(e),i=null,z.zoomCumulativeDelta=0});var l=null,u=null,s=!1,c=function(t){if(null!==l&&null!==u){s=!0;var o=t.deltaX-l,n=t.deltaY-u;l=t.deltaX,u=t.deltaY,p(e,o,n)}};a.on("panstart",function(e){l=0,u=0,c(e)}),a.on("panmove",c),a.on("panend",function(e){l=null,u=null,z.panCumulativeDelta=0,setTimeout(function(){s=!1},500)}),e.zoom._ghostClickHandler=function(e){s&&(e.stopImmediatePropagation(),e.preventDefault())},t.addEventListener("click",e.zoom._ghostClickHandler),e._mc=a}},beforeDatasetsDraw:function(e){var t=e.chart.ctx,o=e.chartArea;if(t.save(),t.beginPath(),e.zoom._dragZoomEnd){var n=f(e),a=e.zoom._dragZoomStart,i=e.zoom._dragZoomEnd,r=a.target.getBoundingClientRect().left,l=Math.min(a.clientX,i.clientX)-r,m=Math.max(a.clientX,i.clientX)-r,u=m-l;t.fillStyle="rgba(225,225,225,0.3)",t.lineWidth=5,t.fillRect(l,n.top,u,n.bottom-n.top)}t.rect(o.left,o.top,o.right-o.left,o.bottom-o.top),t.clip()},afterDatasetsDraw:function(e){e.chart.ctx.restore()},destroy:function(e){if(e.zoom){var t=e.options,o=e.zoom.node;t.zoom&&t.zoom.drag?(o.removeEventListener("mousedown",e.zoom._mouseDownHandler),o.removeEventListener("mousemove",e.zoom._mouseMoveHandler),o.removeEventListener("mouseup",e.zoom._mouseUpHandler)):o.removeEventListener("wheel",e.zoom._wheelHandler),v&&o.removeEventListener("click",e.zoom._ghostClickHandler),delete e.zoom;var n=e._mc;n&&(n.remove("pinchstart"),n.remove("pinch"),n.remove("pinchend"),n.remove("panstart"),n.remove("pan"),n.remove("panend"))}}};t.exports=_,h.pluginService.register(_)},{"chart.js":1,hammerjs:1}]},{},[2]); |