Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbbb2ca

Browse files
committed
RangeSlider: Break apart update computations into data and pixel setting methods
1 parent cf62849 commit cbbb2ca

File tree

1 file changed

+67
-37
lines changed

1 file changed

+67
-37
lines changed

src/components/rangeslider/create_slider.js

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,49 @@ module.exports = function createSlider(gd) {
141141
window.addEventListener('mouseup', mouseUp);
142142

143143
function mouseMove(e) {
144-
var delta = +e.clientX - startX;
144+
var delta = +e.clientX - startX,
145+
pixelMin,
146+
pixelMax;
145147

146148
switch(target) {
147149
case slideBox:
148150
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));
150157
break;
151158

152159
case grabAreaMin:
153160
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));
155167
break;
156168

157169
case grabAreaMax:
158170
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));
160177
break;
161178

162179
default:
163180
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));
165187
break;
166188
}
167189
}
@@ -173,6 +195,17 @@ module.exports = function createSlider(gd) {
173195
}
174196
});
175197

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+
176209

177210
function setRange(min, max) {
178211
min = min || -Infinity;
@@ -187,52 +220,49 @@ module.exports = function createSlider(gd) {
187220
setPixelRange(pixelMin, pixelMax);
188221
}
189222

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+
}
190235

191-
function setPixelRange(min, max) {
192236

193-
min = Lib.constrain(min, 0, width);
194-
max = Lib.constrain(max, 0, width);
237+
function setPixelRange(pixelMin, pixelMax) {
195238

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;
200246
}
201247

202248
helpers.setAttributes(slider, {
203-
'data-min': min,
204-
'data-max': max
249+
'data-min': pixelMin,
250+
'data-max': pixelMax
205251
});
206252

207253
helpers.setAttributes(slideBox, {
208-
'x': min,
209-
'width': max - min
254+
'x': pixelMin,
255+
'width': pixelMax - pixelMin
210256
});
211257

212-
helpers.setAttributes(maskMin, { 'width': min });
258+
helpers.setAttributes(maskMin, { 'width': pixelMin });
213259
helpers.setAttributes(maskMax, {
214-
'x': max,
215-
'width': width - max
260+
'x': pixelMax,
261+
'width': width - pixelMax
216262
});
217263

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 + ')' });
236266
}
237267

238268

0 commit comments

Comments
 (0)