Skip to content

Commit 430986b

Browse files
committed
Legend: fix drag movement when editable: true
The legend container is now a `<g>` and position must be set using `translate` instead of `x` and `y` coordinates. The cursor is set to always be `move`.
1 parent 1f406e5 commit 430986b

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/components/legend/draw.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -321,38 +321,35 @@ module.exports = function draw(gd) {
321321
}
322322

323323
if(gd._context.editable) {
324-
var xf,
325-
yf,
326-
x0,
327-
y0,
328-
lw,
329-
lh;
324+
var xf, yf, x0, y0;
325+
326+
legend.classed('cursor-move', true);
330327

331328
dragElement.init({
332329
element: legend.node(),
333330
prepFn: function() {
334-
x0 = Number(legend.attr('x'));
335-
y0 = Number(legend.attr('y'));
336-
lw = Number(legend.attr('width'));
337-
lh = Number(legend.attr('height'));
338-
setCursor(legend);
331+
// regex pattern for 'translate(123.45px, 543.21px)'
332+
var re = /(.*\()(\d*\.?\d*)([^\d]*)(\d*\.?\d*)([^\d]*)/,
333+
transform = legend.attr('transform')
334+
.replace(re, function(match, p1, p2, p3, p4) {
335+
return [p2, p4].join(' ');
336+
})
337+
.split(' ');
338+
339+
x0 = +transform[0] || 0;
340+
y0 = +transform[1] || 0;
339341
},
340342
moveFn: function(dx, dy) {
341-
var gs = gd._fullLayout._size;
342-
343-
legend.call(Drawing.setPosition, x0+dx, y0+dy);
343+
var newX = x0 + dx,
344+
newY = y0 + dy;
344345

345-
xf = dragElement.align(x0+dx, lw, gs.l, gs.l+gs.w,
346-
opts.xanchor);
347-
yf = dragElement.align(y0+dy+lh, -lh, gs.t+gs.h, gs.t,
348-
opts.yanchor);
346+
var transform = 'translate(' + newX + ', ' + newY + ')';
347+
legend.attr('transform', transform);
349348

350-
var csr = dragElement.getCursor(xf, yf,
351-
opts.xanchor, opts.yanchor);
352-
setCursor(legend, csr);
349+
xf = dragElement.align(newX, 0, gs.l, gs.l+gs.w, opts.xanchor);
350+
yf = dragElement.align(newY, 0, gs.t+gs.h, gs.t, opts.yanchor);
353351
},
354352
doneFn: function(dragged) {
355-
setCursor(legend);
356353
if(dragged && xf !== undefined && yf !== undefined) {
357354
Plotly.relayout(gd, {'legend.x': xf, 'legend.y': yf});
358355
}

0 commit comments

Comments
 (0)