Skip to content

Commit b14c659

Browse files
committed
fix x/y cursor position on drag + 🔒
- please note, the bounding box of a polygon inside a circle isn't the same as the bounding box of the circle
1 parent e5ebc4b commit b14c659

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

src/plots/polar/polar.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,14 @@ proto.updateMainDrag = function(fullLayout, polarLayout) {
819819
x0 = startX - bbox.left;
820820
y0 = startY - bbox.top;
821821

822+
// need to offset x/y as bbox center does not
823+
// match origin for asymmetric polygons
824+
if(vangles) {
825+
var offset = findPolygonOffset(radius, sector, vangles);
826+
x0 += offset.left;
827+
y0 += offset.top;
828+
}
829+
822830
switch(dragModeNow) {
823831
case 'zoom':
824832
if(vangles) {
@@ -1443,6 +1451,20 @@ function makePolygon(r, sector, vangles) {
14431451
makeClippedPolygon(r, sector, vangles);
14441452
}
14451453

1454+
function findPolygonOffset(r, sector, vangles) {
1455+
var minX = Infinity;
1456+
var minY = Infinity;
1457+
1458+
for(var i = 0; i < vangles.length; i++) {
1459+
var va = vangles[i];
1460+
if(isAngleInSector(va, sector)) {
1461+
minX = Math.min(minX, r * Math.cos(va));
1462+
minY = Math.min(minY, -r * Math.sin(va));
1463+
}
1464+
}
1465+
return {top: minY + r, left: minX + r};
1466+
}
1467+
14461468
function invertY(pts0) {
14471469
var len = pts0.length;
14481470
var pts1 = new Array(len);

test/jasmine/tests/polar_test.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,86 @@ describe('Test polar *gridshape linear* interactions', function() {
12131213
.then(done);
12141214
});
12151215

1216-
// - test handle position an
1216+
it('should place zoombox handles at correct place on main drag', function(done) {
1217+
var dragCoverNode;
1218+
var p1;
1219+
1220+
// d attr to array of segment [x,y]
1221+
function path2coords(path) {
1222+
if(!path.size()) return [[]];
1223+
return path.attr('d')
1224+
.replace(/Z/g, '')
1225+
.split('M')
1226+
.filter(Boolean)
1227+
.map(function(s) {
1228+
return s.split('L')
1229+
.map(function(s) { return s.split(',').map(Number); });
1230+
})
1231+
.reduce(function(a, b) { return a.concat(b); });
1232+
}
1233+
1234+
function _dragStart(p0, dp) {
1235+
var node = d3.select('.polar > .draglayer > .maindrag').node();
1236+
mouseEvent('mousemove', p0[0], p0[1], {element: node});
1237+
mouseEvent('mousedown', p0[0], p0[1], {element: node});
1238+
1239+
var promise = drag.waitForDragCover().then(function(dcn) {
1240+
dragCoverNode = dcn;
1241+
p1 = [p0[0] + dp[0], p0[1] + dp[1]];
1242+
mouseEvent('mousemove', p1[0], p1[1], {element: dragCoverNode});
1243+
});
1244+
return promise;
1245+
}
12171246

1247+
function _assertAndDragEnd(msg, exp) {
1248+
var zl = d3.select(gd).select('g.zoomlayer');
1249+
1250+
expect(path2coords(zl.select('.zoombox')))
1251+
.toBeCloseTo2DArray(exp.zoombox, 2, msg + ' - zoombox');
1252+
expect(path2coords(zl.select('.zoombox-corners')))
1253+
.toBeCloseTo2DArray(exp.corners, 2, msg + ' - corners');
1254+
1255+
mouseEvent('mouseup', p1[0], p1[1], {element: dragCoverNode});
1256+
return drag.waitForDragCoverRemoval();
1257+
}
1258+
1259+
Plotly.plot(gd, [{
1260+
type: 'scatterpolar',
1261+
r: [1, 2, 3, 2, 3],
1262+
theta: ['a', 'b', 'c', 'd', 'e']
1263+
}], {
1264+
polar: {
1265+
gridshape: 'linear',
1266+
angularaxis: {direction: 'clockwise'}
1267+
},
1268+
width: 400,
1269+
height: 400,
1270+
margin: {l: 50, t: 50, b: 50, r: 50}
1271+
})
1272+
.then(function() { return _dragStart([170, 170], [220, 220]); })
1273+
.then(function() {
1274+
_assertAndDragEnd('drag outward toward bottom right', {
1275+
zoombox: [
1276+
[-142.658, -46.353], [-88.167, 121.352],
1277+
[88.167, 121.352], [142.658, -46.352],
1278+
[0, -150], [-142.658, -46.352],
1279+
[-142.658, -46.352],
1280+
[-88.167, 121.352],
1281+
[88.167, 121.352], [142.658, -46.352],
1282+
[0, -150], [-142.658, -46.352],
1283+
[-49.261, -16.005], [-30.445, 41.904],
1284+
[30.44508691777904, 41.904], [49.261, -16.005],
1285+
[0, -51.796], [-49.261, -16.005]
1286+
],
1287+
corners: [
1288+
[-13.342, -39.630], [-33.567, -24.935],
1289+
[-35.918, -28.171], [-15.693, -42.866],
1290+
[-60.040, -103.905], [-80.266, -89.210],
1291+
[-82.617, -92.446], [-62.392, -107.141]
1292+
]
1293+
});
1294+
})
1295+
.catch(failTest)
1296+
.then(done);
1297+
});
12181298
});

0 commit comments

Comments
 (0)