@@ -141,27 +141,49 @@ module.exports = function createSlider(gd) {
141
141
window . addEventListener ( 'mouseup' , mouseUp ) ;
142
142
143
143
function mouseMove ( e ) {
144
- var delta = + e . clientX - startX ;
144
+ var delta = + e . clientX - startX ,
145
+ pixelMin ,
146
+ pixelMax ;
145
147
146
148
switch ( target ) {
147
149
case slideBox :
148
150
slider . style . cursor = 'ew-resize' ;
149
- setPixelRange ( + maxVal + delta , + minVal + delta ) ;
151
+
152
+ pixelMin = + minVal + delta ;
153
+ pixelMax = + maxVal + delta ;
154
+
155
+ setPixelRange ( pixelMin , pixelMax ) ;
156
+ setDataRange ( pixelToData ( pixelMin ) , pixelToData ( pixelMax ) ) ;
150
157
break ;
151
158
152
159
case grabAreaMin :
153
160
slider . style . cursor = 'col-resize' ;
154
- setPixelRange ( + minVal + delta , + maxVal ) ;
161
+
162
+ pixelMin = + minVal + delta ;
163
+ pixelMax = + maxVal ;
164
+
165
+ setPixelRange ( pixelMin , pixelMax ) ;
166
+ setDataRange ( pixelToData ( pixelMin ) , pixelToData ( pixelMax ) ) ;
155
167
break ;
156
168
157
169
case grabAreaMax :
158
170
slider . style . cursor = 'col-resize' ;
159
- setPixelRange ( + minVal , + maxVal + delta ) ;
171
+
172
+ pixelMin = + minVal ;
173
+ pixelMax = + maxVal + delta ;
174
+
175
+ setPixelRange ( pixelMin , pixelMax ) ;
176
+ setDataRange ( pixelToData ( pixelMin ) , pixelToData ( pixelMax ) ) ;
160
177
break ;
161
178
162
179
default :
163
180
slider . style . cursor = 'ew-resize' ;
164
- setPixelRange ( offsetX , offsetX + delta ) ;
181
+
182
+ pixelMin = offsetX ;
183
+ pixelMax = offsetX + delta ;
184
+
185
+ setPixelRange ( pixelMin , pixelMax ) ;
186
+ setDataRange ( pixelToData ( pixelMin ) , pixelToData ( pixelMax ) ) ;
165
187
break ;
166
188
}
167
189
}
@@ -173,6 +195,17 @@ module.exports = function createSlider(gd) {
173
195
}
174
196
} ) ;
175
197
198
+ function pixelToData ( pixel ) {
199
+ var rangeMin = options . range [ 0 ] ,
200
+ rangeMax = options . range [ 1 ] ,
201
+ range = rangeMax - rangeMin ,
202
+ dataValue = pixel / width * range + rangeMin ;
203
+
204
+ dataValue = Lib . constrain ( dataValue , rangeMin , rangeMax ) ;
205
+
206
+ return dataValue ;
207
+ }
208
+
176
209
177
210
function setRange ( min , max ) {
178
211
min = min || - Infinity ;
@@ -187,52 +220,49 @@ module.exports = function createSlider(gd) {
187
220
setPixelRange ( pixelMin , pixelMax ) ;
188
221
}
189
222
223
+ function setDataRange ( dataMin , dataMax ) {
224
+
225
+ if ( window . requestAnimationFrame ) {
226
+ window . requestAnimationFrame ( function ( ) {
227
+ Plotly . relayout ( gd , 'xaxis.range' , [ dataMin , dataMax ] ) ;
228
+ } ) ;
229
+ } else {
230
+ setTimeout ( function ( ) {
231
+ Plotly . relayout ( gd , 'xaxis.range' , [ dataMin , dataMax ] ) ;
232
+ } , 16 ) ;
233
+ }
234
+ }
190
235
191
- function setPixelRange ( min , max ) {
192
236
193
- min = Lib . constrain ( min , 0 , width ) ;
194
- max = Lib . constrain ( max , 0 , width ) ;
237
+ function setPixelRange ( pixelMin , pixelMax ) {
195
238
196
- if ( max < min ) {
197
- var temp = max ;
198
- max = min ;
199
- min = temp ;
239
+ pixelMin = Lib . constrain ( pixelMin , 0 , width ) ;
240
+ pixelMax = Lib . constrain ( pixelMax , 0 , width ) ;
241
+
242
+ if ( pixelMax < pixelMin ) {
243
+ var temp = pixelMax ;
244
+ pixelMax = pixelMin ;
245
+ pixelMin = temp ;
200
246
}
201
247
202
248
helpers . setAttributes ( slider , {
203
- 'data-min' : min ,
204
- 'data-max' : max
249
+ 'data-min' : pixelMin ,
250
+ 'data-max' : pixelMax
205
251
} ) ;
206
252
207
253
helpers . setAttributes ( slideBox , {
208
- 'x' : min ,
209
- 'width' : max - min
254
+ 'x' : pixelMin ,
255
+ 'width' : pixelMax - pixelMin
210
256
} ) ;
211
257
212
- helpers . setAttributes ( maskMin , { 'width' : min } ) ;
258
+ helpers . setAttributes ( maskMin , { 'width' : pixelMin } ) ;
213
259
helpers . setAttributes ( maskMax , {
214
- 'x' : max ,
215
- 'width' : width - max
260
+ 'x' : pixelMax ,
261
+ 'width' : width - pixelMax
216
262
} ) ;
217
263
218
- helpers . setAttributes ( grabberMin , { 'transform' : 'translate(' + ( min - handleWidth - 1 ) + ')' } ) ;
219
- helpers . setAttributes ( grabberMax , { 'transform' : 'translate(' + max + ')' } ) ;
220
-
221
- var rangeMin = options . range [ 0 ] ,
222
- rangeMax = options . range [ 1 ] ,
223
- range = rangeMax - rangeMin ,
224
- dataMin = min / width * range + rangeMin ,
225
- dataMax = max / width * range + rangeMin ;
226
-
227
- if ( window . requestAnimationFrame ) {
228
- window . requestAnimationFrame ( function ( ) {
229
- Plotly . relayout ( gd , 'xaxis.range' , [ dataMin , dataMax ] ) ;
230
- } ) ;
231
- } else {
232
- setTimeout ( function ( ) {
233
- Plotly . relayout ( gd , 'xaxis.range' , [ dataMin , dataMax ] ) ;
234
- } , 16 ) ;
235
- }
264
+ helpers . setAttributes ( grabberMin , { 'transform' : 'translate(' + ( pixelMin - handleWidth - 1 ) + ')' } ) ;
265
+ helpers . setAttributes ( grabberMax , { 'transform' : 'translate(' + pixelMax + ')' } ) ;
236
266
}
237
267
238
268
0 commit comments