Skip to content

Commit 451ee24

Browse files
committed
fix date axis ranges and date interactions in gl2d
1 parent 3dd294f commit 451ee24

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

src/plots/gl2d/camera.js

+31-28
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ function createCamera(scene) {
3838
}
3939

4040
result.mouseListener = mouseChange(element, function(buttons, x, y) {
41-
var xrange = scene.xaxis.range,
42-
yrange = scene.yaxis.range,
41+
var dataBox = scene.calcDataBox(),
4342
viewBox = plot.viewBox;
4443

4544
var lastX = result.lastPos[0],
@@ -51,14 +50,15 @@ function createCamera(scene) {
5150
// mouseChange gives y about top; convert to about bottom
5251
y = (viewBox[3] - viewBox[1]) - y;
5352

54-
function updateRange(range, start, end) {
53+
function updateRange(i0, start, end) {
5554
var range0 = Math.min(start, end),
5655
range1 = Math.max(start, end);
5756

5857
if(range0 !== range1) {
59-
range[0] = range0;
60-
range[1] = range1;
61-
result.dataBox = range;
58+
dataBox[i0] = range0;
59+
dataBox[i0 + 2] = range1;
60+
result.dataBox = dataBox;
61+
scene.setRanges(dataBox);
6262
}
6363
else {
6464
scene.selectBox.selectBox = [0, 0, 1, 1];
@@ -70,11 +70,11 @@ function createCamera(scene) {
7070
case 'zoom':
7171
if(buttons) {
7272
var dataX = x /
73-
(viewBox[2] - viewBox[0]) * (xrange[1] - xrange[0]) +
74-
xrange[0];
73+
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
74+
dataBox[0];
7575
var dataY = y /
76-
(viewBox[3] - viewBox[1]) * (yrange[1] - yrange[0]) +
77-
yrange[0];
76+
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
77+
dataBox[1];
7878

7979
if(!result.boxEnabled) {
8080
result.boxStart[0] = dataX;
@@ -87,8 +87,8 @@ function createCamera(scene) {
8787
result.boxEnabled = true;
8888
}
8989
else if(result.boxEnabled) {
90-
updateRange(xrange, result.boxStart[0], result.boxEnd[0]);
91-
updateRange(yrange, result.boxStart[1], result.boxEnd[1]);
90+
updateRange(0, result.boxStart[0], result.boxEnd[0]);
91+
updateRange(1, result.boxStart[1], result.boxEnd[1]);
9292
unSetAutoRange();
9393
result.boxEnabled = false;
9494
}
@@ -98,15 +98,17 @@ function createCamera(scene) {
9898
result.boxEnabled = false;
9999

100100
if(buttons) {
101-
var dx = (lastX - x) * (xrange[1] - xrange[0]) /
101+
var dx = (lastX - x) * (dataBox[2] - dataBox[0]) /
102102
(plot.viewBox[2] - plot.viewBox[0]);
103-
var dy = (lastY - y) * (yrange[1] - yrange[0]) /
103+
var dy = (lastY - y) * (dataBox[3] - dataBox[1]) /
104104
(plot.viewBox[3] - plot.viewBox[1]);
105105

106-
xrange[0] += dx;
107-
xrange[1] += dx;
108-
yrange[0] += dy;
109-
yrange[1] += dy;
106+
dataBox[0] += dx;
107+
dataBox[2] += dx;
108+
dataBox[1] += dy;
109+
dataBox[3] += dy;
110+
111+
scene.setRanges(dataBox);
110112

111113
result.lastInputTime = Date.now();
112114
unSetAutoRange();
@@ -120,8 +122,7 @@ function createCamera(scene) {
120122
});
121123

122124
result.wheelListener = mouseWheel(element, function(dx, dy) {
123-
var xrange = scene.xaxis.range,
124-
yrange = scene.yaxis.range,
125+
var dataBox = scene.calcDataBox(),
125126
viewBox = plot.viewBox;
126127

127128
var lastX = result.lastPos[0],
@@ -135,16 +136,18 @@ function createCamera(scene) {
135136
var scale = Math.exp(0.1 * dy / (viewBox[3] - viewBox[1]));
136137

137138
var cx = lastX /
138-
(viewBox[2] - viewBox[0]) * (xrange[1] - xrange[0]) +
139-
xrange[0];
139+
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
140+
dataBox[0];
140141
var cy = lastY /
141-
(viewBox[3] - viewBox[1]) * (yrange[1] - yrange[0]) +
142-
yrange[0];
142+
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
143+
dataBox[1];
144+
145+
dataBox[0] = (dataBox[0] - cx) * scale + cx;
146+
dataBox[2] = (dataBox[2] - cx) * scale + cx;
147+
dataBox[1] = (dataBox[1] - cy) * scale + cy;
148+
dataBox[3] = (dataBox[3] - cy) * scale + cy;
143149

144-
xrange[0] = (xrange[0] - cx) * scale + cx;
145-
xrange[1] = (xrange[1] - cx) * scale + cx;
146-
yrange[0] = (yrange[0] - cy) * scale + cy;
147-
yrange[1] = (yrange[1] - cy) * scale + cy;
150+
scene.setRanges(dataBox);
148151

149152
result.lastInputTime = Date.now();
150153
unSetAutoRange();

src/plots/gl2d/scene2d.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,9 @@ var relayoutCallback = function(scene) {
311311
};
312312

313313
proto.cameraChanged = function() {
314-
var camera = this.camera,
315-
xrange = this.xaxis.range,
316-
yrange = this.yaxis.range;
314+
var camera = this.camera;
317315

318-
this.glplot.setDataBox([
319-
xrange[0], yrange[0],
320-
xrange[1], yrange[1]
321-
]);
316+
this.glplot.setDataBox(this.calcDataBox());
322317

323318
var nextTicks = this.computeTickMarks();
324319
var curTicks = this.glplotOptions.ticks;
@@ -416,9 +411,7 @@ proto.plot = function(fullData, calcData, fullLayout) {
416411

417412
options.ticks = this.computeTickMarks();
418413

419-
var xrange = this.xaxis.range;
420-
var yrange = this.yaxis.range;
421-
options.dataBox = [xrange[0], yrange[0], xrange[1], yrange[1]];
414+
options.dataBox = this.calcDataBox();
422415

423416
options.merge(fullLayout);
424417
glplot.update(options);
@@ -427,6 +420,27 @@ proto.plot = function(fullData, calcData, fullLayout) {
427420
this.glplot.draw();
428421
};
429422

423+
proto.calcDataBox = function() {
424+
var xaxis = this.xaxis,
425+
yaxis = this.yaxis,
426+
xrange = xaxis.range,
427+
yrange = yaxis.range,
428+
xr2l = xaxis.r2l,
429+
yr2l = yaxis.r2l;
430+
431+
return [xr2l(xrange[0]), yr2l(yrange[0]), xr2l(xrange[1]), yr2l(yrange[1])];
432+
};
433+
434+
proto.setRanges = function(dataBox) {
435+
var xaxis = this.xaxis,
436+
yaxis = this.yaxis,
437+
xl2r = xaxis.l2r,
438+
yl2r = yaxis.l2r;
439+
440+
xaxis.range = [xl2r(dataBox[0]), xl2r(dataBox[2])];
441+
yaxis.range = [yl2r(dataBox[1]), yl2r(dataBox[3])];
442+
};
443+
430444
proto.updateTraces = function(fullData, calcData) {
431445
var traceIds = Object.keys(this.traces);
432446
var i, j, fullTrace;

0 commit comments

Comments
 (0)