11/*!
22 * chartjs-plugin-zoom
33 * http://chartjs.org/
4- * Version: 0.4.5
4+ * Version: 0.5.0
55 *
66 * Copyright 2016 Evert Timberg
77 * Released under the MIT license
@@ -35,12 +35,12 @@ var defaultOptions = zoomNS.defaults = {
3535 enabled : true ,
3636 mode : 'xy' ,
3737 speed : 20 ,
38- threshold : 10 ,
38+ threshold : 10
3939 } ,
4040 zoom : {
4141 enabled : true ,
4242 mode : 'xy' ,
43- sensitivity : 3 ,
43+ sensitivity : 3
4444 }
4545} ;
4646
@@ -54,6 +54,26 @@ function directionEnabled(mode, dir) {
5454 return false ;
5555}
5656
57+ function rangeMaxLimiter ( zoomPanOptions , newMax ) {
58+ if ( zoomPanOptions . scaleAxes && zoomPanOptions . rangeMax && zoomPanOptions . rangeMax [ zoomPanOptions . scaleAxes ] ) {
59+ var rangeMax = zoomPanOptions . rangeMax [ zoomPanOptions . scaleAxes ] ;
60+ if ( newMax > rangeMax ) {
61+ newMax = rangeMax ;
62+ }
63+ }
64+ return newMax ;
65+ }
66+
67+ function rangeMinLimiter ( zoomPanOptions , newMin ) {
68+ if ( zoomPanOptions . scaleAxes && zoomPanOptions . rangeMin && zoomPanOptions . rangeMin [ zoomPanOptions . scaleAxes ] ) {
69+ var rangeMin = zoomPanOptions . rangeMin [ zoomPanOptions . scaleAxes ] ;
70+ if ( newMin < rangeMin ) {
71+ newMin = rangeMin ;
72+ }
73+ }
74+ return newMin ;
75+ }
76+
5777function zoomIndexScale ( scale , zoom , center , zoomOptions ) {
5878 var labels = scale . chart . data . labels ;
5979 var minIndex = scale . minIndex ;
@@ -90,12 +110,12 @@ function zoomIndexScale(scale, zoom, center, zoomOptions) {
90110 }
91111 zoomNS . zoomCumulativeDelta = 0 ;
92112 }
93- scale . options . ticks . min = labels [ minIndex ] ;
94- scale . options . ticks . max = labels [ maxIndex ] ;
113+ scale . options . ticks . min = rangeMinLimiter ( zoomOptions , labels [ minIndex ] ) ;
114+ scale . options . ticks . max = rangeMaxLimiter ( zoomOptions , labels [ maxIndex ] ) ;
95115 }
96116}
97117
98- function zoomTimeScale ( scale , zoom , center ) {
118+ function zoomTimeScale ( scale , zoom , center , zoomOptions ) {
99119 var options = scale . options ;
100120
101121 var range ;
@@ -114,11 +134,11 @@ function zoomTimeScale(scale, zoom, center) {
114134 var minDelta = newDiff * min_percent ;
115135 var maxDelta = newDiff * max_percent ;
116136
117- options . time . min = scale . getValueForPixel ( scale . getPixelForValue ( scale . firstTick ) + minDelta ) ;
118- options . time . max = scale . getValueForPixel ( scale . getPixelForValue ( scale . lastTick ) - maxDelta ) ;
137+ options . time . min = rangeMinLimiter ( zoomOptions , scale . getValueForPixel ( scale . getPixelForValue ( scale . firstTick ) + minDelta ) ) ;
138+ options . time . max = rangeMaxLimiter ( zoomOptions , scale . getValueForPixel ( scale . getPixelForValue ( scale . lastTick ) - maxDelta ) ) ;
119139}
120140
121- function zoomNumericalScale ( scale , zoom , center ) {
141+ function zoomNumericalScale ( scale , zoom , center , zoomOptions ) {
122142 var range = scale . max - scale . min ;
123143 var newDiff = range * ( zoom - 1 ) ;
124144
@@ -129,8 +149,8 @@ function zoomNumericalScale(scale, zoom, center) {
129149 var minDelta = newDiff * min_percent ;
130150 var maxDelta = newDiff * max_percent ;
131151
132- scale . options . ticks . min = scale . min + minDelta ;
133- scale . options . ticks . max = scale . max - maxDelta ;
152+ scale . options . ticks . min = rangeMinLimiter ( zoomOptions , scale . min + minDelta ) ;
153+ scale . options . ticks . max = rangeMaxLimiter ( zoomOptions , scale . max - maxDelta ) ;
134154}
135155
136156function zoomScale ( scale , zoom , center , zoomOptions ) {
@@ -158,9 +178,11 @@ function doZoom(chartInstance, zoom, center) {
158178
159179 helpers . each ( chartInstance . scales , function ( scale , id ) {
160180 if ( scale . isHorizontal ( ) && directionEnabled ( zoomMode , 'x' ) ) {
181+ zoomOptions . scaleAxes = "x" ;
161182 zoomScale ( scale , zoom , center , zoomOptions ) ;
162183 } else if ( ! scale . isHorizontal ( ) && directionEnabled ( zoomMode , 'y' ) ) {
163184 // Do Y zoom
185+ zoomOptions . scaleAxes = "y" ;
164186 zoomScale ( scale , zoom , center , zoomOptions ) ;
165187 }
166188 } ) ;
@@ -185,17 +207,17 @@ function panIndexScale(scale, delta, panOptions) {
185207
186208 maxIndex = Math . min ( lastLabelIndex , minIndex + offsetAmt - 1 ) ;
187209
188- scale . options . ticks . min = labels [ minIndex ] ;
189- scale . options . ticks . max = labels [ maxIndex ] ;
210+ scale . options . ticks . min = rangeMinLimiter ( panOptions , labels [ minIndex ] ) ;
211+ scale . options . ticks . max = rangeMaxLimiter ( panOptions , labels [ maxIndex ] ) ;
190212}
191213
192- function panTimeScale ( scale , delta ) {
214+ function panTimeScale ( scale , delta , panOptions ) {
193215 var options = scale . options ;
194- options . time . min = scale . getValueForPixel ( scale . getPixelForValue ( scale . firstTick ) - delta ) ;
195- options . time . max = scale . getValueForPixel ( scale . getPixelForValue ( scale . lastTick ) - delta ) ;
216+ options . time . min = rangeMinLimiter ( panOptions , scale . getValueForPixel ( scale . getPixelForValue ( scale . firstTick ) - delta ) ) ;
217+ options . time . max = rangeMaxLimiter ( panOptions , scale . getValueForPixel ( scale . getPixelForValue ( scale . lastTick ) - delta ) ) ;
196218}
197219
198- function panNumericalScale ( scale , delta ) {
220+ function panNumericalScale ( scale , delta , panOptions ) {
199221 var tickOpts = scale . options . ticks ;
200222 var start = scale . start ,
201223 end = scale . end ;
@@ -207,6 +229,8 @@ function panNumericalScale(scale, delta) {
207229 tickOpts . min = scale . getValueForPixel ( scale . getPixelForValue ( start ) - delta ) ;
208230 tickOpts . max = scale . getValueForPixel ( scale . getPixelForValue ( end ) - delta ) ;
209231 }
232+ tickOpts . min = rangeMinLimiter ( panOptions , tickOpts . min ) ;
233+ tickOpts . max = rangeMaxLimiter ( panOptions , tickOpts . max ) ;
210234}
211235
212236function panScale ( scale , delta , panOptions ) {
@@ -224,8 +248,10 @@ function doPan(chartInstance, deltaX, deltaY) {
224248
225249 helpers . each ( chartInstance . scales , function ( scale , id ) {
226250 if ( scale . isHorizontal ( ) && directionEnabled ( panMode , 'x' ) && deltaX !== 0 ) {
251+ panOptions . scaleAxes = "x" ;
227252 panScale ( scale , deltaX , panOptions ) ;
228253 } else if ( ! scale . isHorizontal ( ) && directionEnabled ( panMode , 'y' ) && deltaY !== 0 ) {
254+ panOptions . scaleAxes = "y" ;
229255 panScale ( scale , deltaY , panOptions ) ;
230256 }
231257 } ) ;
0 commit comments