Skip to content

Commit 5aef0e7

Browse files
committed
Merge pull request #505 from plotly/annotation-dragging
Annotations: fixed dragging
2 parents aaa3e3c + 6abb696 commit 5aef0e7

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

src/components/annotations/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ annotations.draw = function(gd, index, opt, value) {
498498

499499
var annX = Math.round(annPosPx.x - outerwidth / 2),
500500
annY = Math.round(annPosPx.y - outerheight / 2);
501-
ann.attr('transform', 'translate(' + annX + ', ' + annY + ')');
501+
ann.call(Lib.setTranslate, annX, annY);
502502

503503
var annbase = 'annotations['+index+']';
504504

@@ -591,8 +591,10 @@ annotations.draw = function(gd, index, opt, value) {
591591
dragElement.init({
592592
element: arrowdrag.node(),
593593
prepFn: function() {
594-
annx0 = Number(ann.attr('x'));
595-
anny0 = Number(ann.attr('y'));
594+
var pos = Lib.getTranslate(ann);
595+
596+
annx0 = pos.x;
597+
anny0 = pos.y;
596598
update = {};
597599
if(xa && xa.autorange) {
598600
update[xa._name+'.autorange'] = true;
@@ -607,7 +609,7 @@ annotations.draw = function(gd, index, opt, value) {
607609
var annxy0 = applyTransform(annx0, anny0),
608610
xcenter = annxy0[0] + dx,
609611
ycenter = annxy0[1] + dy;
610-
ann.call(Drawing.setPosition, xcenter, ycenter);
612+
ann.call(Lib.setTranslate, xcenter, ycenter);
611613

612614
update[annbase+'.x'] = xa ?
613615
(options.x + dx / xa._m) :
@@ -648,12 +650,14 @@ annotations.draw = function(gd, index, opt, value) {
648650
dragElement.init({
649651
element: ann.node(),
650652
prepFn: function() {
651-
x0 = Number(ann.attr('x'));
652-
y0 = Number(ann.attr('y'));
653+
var pos = Lib.getTranslate(ann);
654+
655+
x0 = pos.x;
656+
y0 = pos.y;
653657
update = {};
654658
},
655659
moveFn: function(dx, dy) {
656-
ann.call(Drawing.setPosition, x0 + dx, y0 + dy);
660+
ann.call(Lib.setTranslate, x0 + dx, y0 + dy);
657661
var csr = 'pointer';
658662
if(options.showarrow) {
659663
update[annbase+'.ax'] = options.ax + dx;
@@ -679,7 +683,7 @@ annotations.draw = function(gd, index, opt, value) {
679683
heightFraction, 0, 1, options.yanchor);
680684
}
681685
if(!xa || !ya) {
682-
csr = dragElement.cursor(
686+
csr = dragElement.getCursor(
683687
xa ? 0.5 : update[annbase + '.x'],
684688
ya ? 0.5 : update[annbase + '.y'],
685689
options.xanchor, options.yanchor
@@ -691,7 +695,7 @@ annotations.draw = function(gd, index, opt, value) {
691695
x1 = xy1[0] + dx,
692696
y1 = xy1[1] + dy;
693697

694-
ann.call(Drawing.setPosition, x1, y1);
698+
ann.call(Lib.setTranslate, x0 + dx, y0 + dy);
695699

696700
anng.attr({
697701
transform: 'rotate(' + textangle + ',' +

test/jasmine/tests/config_test.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ describe('config argument', function() {
5454
{ x: [1,2,3], y: [3,2,1] }
5555
], {
5656
width: 600,
57-
height: 400
57+
height: 400,
58+
annotations: [
59+
{ text: 'testing', x: 1, y: 1, showarrow: true }
60+
]
5861
}, { editable: true })
5962
.then(done);
6063
});
@@ -78,6 +81,24 @@ describe('config argument', function() {
7881
expect(editBox.getAttribute('contenteditable')).toBe('true');
7982
}
8083

84+
function checkIfDraggable(elClass) {
85+
var el = document.getElementsByClassName(elClass)[0];
86+
87+
var elBox = el.getBoundingClientRect(),
88+
elX = elBox.left + elBox.width / 2,
89+
elY = elBox.top + elBox.height / 2;
90+
91+
mouseEvent('mousedown', elX, elY);
92+
mouseEvent('mousemove', elX - 20, elY + 20);
93+
94+
var movedBox = el.getBoundingClientRect();
95+
96+
expect(movedBox.left).toBe(elBox.left - 20);
97+
expect(movedBox.top).toBe(elBox.top + 20);
98+
99+
mouseEvent('mouseup', elX - 20, elY + 20);
100+
}
101+
81102
it('should make titles editable', function() {
82103
checkIfEditable('gtitle', 'Click to enter Plot title');
83104
});
@@ -94,22 +115,21 @@ describe('config argument', function() {
94115
checkIfEditable('legendtext', 'trace 0');
95116
});
96117

97-
it('should make legends draggable', function() {
98-
99-
var legend = document.getElementsByClassName('legend')[0],
100-
legendBox = legend.getBoundingClientRect(),
101-
legendX = legendBox.left + legendBox.width / 2,
102-
legendY = legendBox.top + legendBox.height / 2;
103-
104-
mouseEvent('mousedown', legendX, legendY);
105-
mouseEvent('mousemove', legendX - 20, legendY + 20);
106-
mouseEvent('mouseup', legendX - 20, legendY + 20);
118+
it('should make annotation labels editable', function() {
119+
checkIfEditable('annotation-text-g', 'testing');
120+
});
107121

108-
var movedlegendBox = legend.getBoundingClientRect();
122+
it('should make annotation labels draggable', function() {
123+
checkIfDraggable('annotation-text-g');
124+
});
109125

110-
expect(movedlegendBox.left).not.toBe(legendBox.left);
111-
expect(movedlegendBox.top).not.toBe(legendBox.top);
126+
it('should make annotation arrows draggable', function() {
127+
checkIfDraggable('annotation-arrow-g');
128+
});
112129

130+
it('should make legends draggable', function() {
131+
checkIfDraggable('legend');
113132
});
133+
114134
});
115135
});

0 commit comments

Comments
 (0)