1
1
/*!
2
2
* Chart.Zoom.js
3
3
* http://chartjs.org/
4
- * Version: 0.1.2
4
+ * Version: 0.1.3
5
5
*
6
6
* Copyright 2016 Evert Timberg
7
7
* Released under the MIT license
@@ -20,14 +20,14 @@ Chart = typeof(Chart) === 'function' ? Chart : window.Chart;
20
20
var helpers = Chart . helpers ;
21
21
22
22
// Take the zoom namespace of Chart
23
- Chart . Zoom = Chart . Zoom || { } ;
23
+ var zoomNS = Chart . Zoom = Chart . Zoom || { } ;
24
24
25
25
// Where we store functions to handle different scale types
26
- var zoomFunctions = Chart . Zoom . zoomFunctions = Chart . Zoom . zoomFunctions || { } ;
27
- var panFunctions = Chart . Zoom . panFunctions = Chart . Zoom . panFunctions || { } ;
26
+ var zoomFunctions = zoomNS . zoomFunctions = zoomNS . zoomFunctions || { } ;
27
+ var panFunctions = zoomNS . panFunctions = zoomNS . panFunctions || { } ;
28
28
29
29
// Default options if none are provided
30
- var defaultOptions = Chart . Zoom . defaults = {
30
+ var defaultOptions = zoomNS . defaults = {
31
31
pan : {
32
32
enabled : true ,
33
33
mode : 'xy' ,
@@ -93,7 +93,7 @@ function doZoom(chartInstance, zoom, center) {
93
93
center = {
94
94
x : ( ca . left + ca . right ) / 2 ,
95
95
y : ( ca . top + ca . bottom ) / 2 ,
96
- }
96
+ } ;
97
97
}
98
98
99
99
var zoomOptions = chartInstance . options . zoom ;
@@ -133,16 +133,16 @@ function panTimeScale(scale, delta) {
133
133
}
134
134
135
135
function panNumericalScale ( scale , delta ) {
136
- var options = scale . options ;
136
+ var tickOpts = scale . options . ticks ;
137
137
var start = scale . start ,
138
138
end = scale . end ;
139
139
140
- if ( options . ticks . reverse ) {
141
- options . ticks . max = scale . getValueForPixel ( scale . getPixelForValue ( start ) - delta ) ;
142
- options . ticks . min = scale . getValueForPixel ( scale . getPixelForValue ( end ) - delta ) ;
140
+ if ( tickOpts . reverse ) {
141
+ tickOpts . max = scale . getValueForPixel ( scale . getPixelForValue ( start ) - delta ) ;
142
+ tickOpts . min = scale . getValueForPixel ( scale . getPixelForValue ( end ) - delta ) ;
143
143
} else {
144
- options . ticks . min = scale . getValueForPixel ( scale . getPixelForValue ( start ) - delta ) ;
145
- options . ticks . max = scale . getValueForPixel ( scale . getPixelForValue ( end ) - delta ) ;
144
+ tickOpts . min = scale . getValueForPixel ( scale . getPixelForValue ( start ) - delta ) ;
145
+ tickOpts . max = scale . getValueForPixel ( scale . getPixelForValue ( end ) - delta ) ;
146
146
}
147
147
}
148
148
@@ -176,32 +176,32 @@ function positionInChartArea(chartInstance, position) {
176
176
}
177
177
178
178
// Store these for later
179
- Chart . Zoom . zoomFunctions [ ' category' ] = zoomIndexScale ;
180
- Chart . Zoom . zoomFunctions [ ' time' ] = zoomTimeScale ;
181
- Chart . Zoom . zoomFunctions [ ' linear' ] = zoomNumericalScale ;
182
- Chart . Zoom . zoomFunctions [ ' logarithmic' ] = zoomNumericalScale ;
183
- Chart . Zoom . panFunctions [ ' category' ] = panIndexScale ;
184
- Chart . Zoom . panFunctions [ ' time' ] = panTimeScale ;
185
- Chart . Zoom . panFunctions [ ' linear' ] = panNumericalScale ;
186
- Chart . Zoom . panFunctions [ ' logarithmic' ] = panNumericalScale ;
179
+ zoomNS . zoomFunctions . category = zoomIndexScale ;
180
+ zoomNS . zoomFunctions . time = zoomTimeScale ;
181
+ zoomNS . zoomFunctions . linear = zoomNumericalScale ;
182
+ zoomNS . zoomFunctions . logarithmic = zoomNumericalScale ;
183
+ zoomNS . panFunctions . category = panIndexScale ;
184
+ zoomNS . panFunctions . time = panTimeScale ;
185
+ zoomNS . panFunctions . linear = panNumericalScale ;
186
+ zoomNS . panFunctions . logarithmic = panNumericalScale ;
187
187
188
188
// Chartjs Zoom Plugin
189
- var ZoomPlugin = Chart . PluginBase . extend ( {
189
+ var zoomPlugin = {
190
190
beforeInit : function ( chartInstance ) {
191
191
var node = chartInstance . chart . ctx . canvas ;
192
192
var options = chartInstance . options ;
193
- var panThreshold = helpers . getValueOrDefault ( options . pan ? options . pan . threshold : undefined , Chart . Zoom . defaults . pan . threshold ) ;
193
+ var panThreshold = helpers . getValueOrDefault ( options . pan ? options . pan . threshold : undefined , zoomNS . defaults . pan . threshold ) ;
194
194
195
195
var wheelHandler = function ( e ) {
196
- if ( e . wheelDelta > 0 ) {
196
+ if ( e . deltaY < 0 ) {
197
197
doZoom ( chartInstance , 1.1 ) ;
198
198
} else {
199
199
doZoom ( chartInstance , 0.909 ) ;
200
200
}
201
201
} ;
202
202
chartInstance . _wheelHandler = wheelHandler ;
203
203
204
- node . addEventListener ( 'mousewheel ' , wheelHandler ) ;
204
+ node . addEventListener ( 'wheel ' , wheelHandler ) ;
205
205
206
206
if ( Hammer ) {
207
207
var mc = new Hammer . Manager ( node ) ;
@@ -255,6 +255,19 @@ var ZoomPlugin = Chart.PluginBase.extend({
255
255
}
256
256
} ,
257
257
258
+ beforeDatasetsDraw : function ( chartInstance ) {
259
+ var ctx = chartInstance . chart . ctx ;
260
+ var chartArea = chartInstance . chartArea ;
261
+ ctx . save ( ) ;
262
+ ctx . beginPath ( ) ;
263
+ ctx . rect ( chartArea . left , chartArea . top , chartArea . right - chartArea . left , chartArea . bottom - chartArea . top ) ;
264
+ ctx . clip ( ) ;
265
+ } ,
266
+
267
+ afterDatasetsDraw : function ( chartInstance ) {
268
+ chartInstance . chart . ctx . restore ( ) ;
269
+ } ,
270
+
258
271
destroy : function ( chartInstance ) {
259
272
var node = chartInstance . chart . ctx . canvas ;
260
273
@@ -268,8 +281,8 @@ var ZoomPlugin = Chart.PluginBase.extend({
268
281
mc . remove ( 'panend' ) ;
269
282
}
270
283
}
271
- } ) ;
284
+ } ;
272
285
273
- Chart . pluginService . register ( new ZoomPlugin ( ) ) ;
286
+ Chart . pluginService . register ( zoomPlugin ) ;
274
287
275
288
} , { "chart.js" :1 , "hammerjs" :1 } ] } , { } , [ 2 ] ) ;
0 commit comments