Skip to content

Replot on drag #3716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/components/dragelement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var mouseOffset = require('mouse-event-offset');
var hasHover = require('has-hover');
var supportsPassive = require('has-passive-events');

var Registry = require('../../registry');
var Lib = require('../../lib');

var constants = require('../../plots/cartesian/constants');
Expand Down Expand Up @@ -191,12 +190,21 @@ dragElement.init = function init(options) {
dragElement.unhover(gd);
}

if(gd._dragged && options.moveFn && !rightClick) options.moveFn(dx, dy);
if(gd._dragged && options.moveFn && !rightClick) {
gd._dragdata = {
element: element,
dx: dx,
dy: dy
};
options.moveFn(dx, dy);
}

return;
}

function onDone(e) {
delete gd._dragdata;

if(options.dragmode !== false) {
e.preventDefault();
document.removeEventListener('mousemove', onMove);
Expand Down Expand Up @@ -258,10 +266,8 @@ dragElement.init = function init(options) {
}
}

finishDrag(gd);

gd._dragging = false;
gd._dragged = false;

return;
}
};
Expand All @@ -286,11 +292,6 @@ function coverSlip() {

dragElement.coverSlip = coverSlip;

function finishDrag(gd) {
gd._dragging = false;
if(gd._replotPending) Registry.call('plot', gd);
}

function pointerOffset(e) {
return mouseOffset(
e.changedTouches ? e.changedTouches[0] : e,
Expand Down
40 changes: 13 additions & 27 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,8 @@ exports.plot = function(gd, data, layout, config) {
gd.empty = false;
}

if(!gd.layout || graphWasEmpty) gd.layout = helpers.cleanLayout(layout);

// if the user is trying to drag the axes, allow new data and layout
// to come in but don't allow a replot.
if(gd._dragging && !gd._transitioning) {
// signal to drag handler that after everything else is done
// we need to replot, because something has changed
gd._replotPending = true;
return Promise.reject();
} else {
// we're going ahead with a replot now
gd._replotPending = false;
if(!gd.layout || graphWasEmpty) {
gd.layout = helpers.cleanLayout(layout);
}

Plots.supplyDefaults(gd);
Expand Down Expand Up @@ -195,7 +185,7 @@ exports.plot = function(gd, data, layout, config) {
if(gd._context.responsive) {
if(!gd._responsiveChartHandler) {
// Keep a reference to the resize handler to purge it down the road
gd._responsiveChartHandler = function() {Plots.resize(gd);};
gd._responsiveChartHandler = function() { Plots.resize(gd); };

// Listen to window resize
window.addEventListener('resize', gd._responsiveChartHandler);
Expand Down Expand Up @@ -242,10 +232,10 @@ exports.plot = function(gd, data, layout, config) {
return 'gl-canvas gl-canvas-' + d.key.replace('Layer', '');
})
.style({
'position': 'absolute',
'top': 0,
'left': 0,
'overflow': 'visible',
position: 'absolute',
top: 0,
left: 0,
overflow: 'visible',
'pointer-events': 'none'
});
}
Expand Down Expand Up @@ -384,6 +374,7 @@ exports.plot = function(gd, data, layout, config) {
initInteractions,
Plots.addLinks,
Plots.rehover,
Plots.redrag,
// TODO: doAutoMargin is only needed here for axis automargin, which
// happens outside of marginPushers where all the other automargins are
// calculated. Would be much better to separate margin calculations from
Expand Down Expand Up @@ -1402,7 +1393,7 @@ function restyle(gd, astr, val, _traces) {
seq.push(emitAfterPlot);
}

seq.push(Plots.rehover);
seq.push(Plots.rehover, Plots.redrag);

Queue.add(gd,
restyle, [gd, specs.undoit, specs.traces],
Expand Down Expand Up @@ -1911,7 +1902,7 @@ function relayout(gd, astr, val) {
seq.push(emitAfterPlot);
}

seq.push(Plots.rehover);
seq.push(Plots.rehover, Plots.redrag);

Queue.add(gd,
relayout, [gd, specs.undoit],
Expand Down Expand Up @@ -1992,13 +1983,8 @@ function addAxRangeSequence(seq, rangesAltered) {
return Axes.draw(gd, 'redraw');
};

var _clearSelect = function(gd) {
var zoomlayer = gd._fullLayout._zoomlayer;
if(zoomlayer) clearSelect(zoomlayer);
};

seq.push(
_clearSelect,
clearSelect,
subroutines.doAutoRangeAndConstraints,
drawAxes,
subroutines.drawData,
Expand Down Expand Up @@ -2449,7 +2435,7 @@ function update(gd, traceUpdate, layoutUpdate, _traces) {
seq.push(emitAfterPlot);
}

seq.push(Plots.rehover);
seq.push(Plots.rehover, Plots.redrag);

Queue.add(gd,
update, [gd, restyleSpecs.undoit, relayoutSpecs.undoit, restyleSpecs.traces],
Expand Down Expand Up @@ -2852,7 +2838,7 @@ exports.react = function(gd, data, layout, config) {
seq.push(emitAfterPlot);
}

seq.push(Plots.rehover);
seq.push(Plots.rehover, Plots.redrag);

plotDone = Lib.syncOrAsync(seq, gd);
if(!plotDone || !plotDone.then) plotDone = Promise.resolve(gd);
Expand Down
31 changes: 23 additions & 8 deletions src/plots/cartesian/dragbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var d3 = require('d3');
Expand Down Expand Up @@ -38,7 +37,6 @@ var constants = require('./constants');
var MINDRAG = constants.MINDRAG;
var MINZOOM = constants.MINZOOM;


// flag for showing "doubleclick to zoom out" only at the beginning
var SHOWZOOMOUTTIP = true;

Expand Down Expand Up @@ -216,13 +214,30 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
}
}
}

gd._fullLayout._redrag = function() {
var dragDataNow = gd._dragdata;

if(dragDataNow && dragDataNow.element === dragger) {
var dragModeNow = gd._fullLayout.dragmode;

if(!isSelectOrLasso(dragModeNow)) {
recomputeAxisLists();
updateSubplots([0, 0, pw, ph]);
dragOptions.moveFn(dragDataNow.dx, dragDataNow.dy);
}

// TODO should we try to "re-select" under select/lasso modes?
// probably best to wait for https://github.com/plotly/plotly.js/issues/1851
}
};
};

function clearAndResetSelect() {
// clear selection polygon cache (if any)
dragOptions.plotinfo.selection = false;
// clear selection outlines
clearSelect(zoomlayer);
clearSelect(gd);
}

function clickFn(numClicks, evt) {
Expand Down Expand Up @@ -587,8 +602,8 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
else if(yActive === 's') dy = dz(yaxes, 0, -dy);
else if(!yActive) dy = 0;

var x0 = (xActive === 'w') ? dx : 0;
var y0 = (yActive === 'n') ? dy : 0;
var xStart = (xActive === 'w') ? dx : 0;
var yStart = (yActive === 'n') ? dy : 0;

if(links.isSubplotConstrained) {
var i;
Expand All @@ -600,21 +615,21 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
scaleZoom(xaxes[i], 1 - dy / ph);
}
dx = dy * pw / ph;
x0 = dx / 2;
xStart = dx / 2;
}
if(!yActive && xActive.length === 1) {
for(i = 0; i < yaxes.length; i++) {
yaxes[i].range = yaxes[i]._r.slice();
scaleZoom(yaxes[i], 1 - dx / pw);
}
dy = dx * ph / pw;
y0 = dy / 2;
yStart = dy / 2;
}
}

updateMatchedAxRange('x');
updateMatchedAxRange('y');
updateSubplots([x0, y0, pw - dx, ph - dy]);
updateSubplots([xStart, yStart, pw - dx, ph - dy]);
ticksAndAnnotations();
}

Expand Down
25 changes: 15 additions & 10 deletions src/plots/cartesian/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var throttle = require('../../lib/throttle');
var makeEventData = require('../../components/fx/helpers').makeEventData;
var getFromId = require('./axis_ids').getFromId;
var clearGlCanvases = require('../../lib/clear_gl_canvases');

var redrawReglTraces = require('../../plot_api/subroutines').redrawReglTraces;

var constants = require('./constants');
Expand Down Expand Up @@ -465,14 +466,14 @@ function multiTester(list) {

function coerceSelectionsCache(evt, gd, dragOptions) {
var fullLayout = gd._fullLayout;
var zoomLayer = fullLayout._zoomlayer;
var plotinfo = dragOptions.plotinfo;

var selectingOnSameSubplot = (
fullLayout._lastSelectedSubplot &&
fullLayout._lastSelectedSubplot === plotinfo.id
fullLayout._lastSelectedSubplot &&
fullLayout._lastSelectedSubplot === plotinfo.id
);
var hasModifierKey = evt.shiftKey || evt.altKey;

if(selectingOnSameSubplot && hasModifierKey &&
(plotinfo.selection && plotinfo.selection.selectionDefs) && !dragOptions.selectionDefs) {
// take over selection definitions from prev mode, if any
Expand All @@ -484,7 +485,7 @@ function coerceSelectionsCache(evt, gd, dragOptions) {

// clear selection outline when selecting a different subplot
if(!selectingOnSameSubplot) {
clearSelect(zoomLayer);
clearSelect(gd);
fullLayout._lastSelectedSubplot = plotinfo.id;
}
}
Expand Down Expand Up @@ -669,7 +670,7 @@ function updateSelectedState(gd, searchTraces, eventData) {
// before anything else, update preGUI if necessary
for(i = 0; i < searchTraces.length; i++) {
var fullInputTrace = searchTraces[i].cd[0].trace._fullInput;
var tracePreGUI = gd._fullLayout._tracePreGUI[fullInputTrace.uid];
var tracePreGUI = gd._fullLayout._tracePreGUI[fullInputTrace.uid] || {};
if(tracePreGUI.selectedpoints === undefined) {
tracePreGUI.selectedpoints = fullInputTrace._input.selectedpoints || null;
}
Expand Down Expand Up @@ -774,11 +775,15 @@ function fillSelectionItem(selection, searchInfo) {
return selection;
}

function clearSelect(zoomlayer) {
// until we get around to persistent selections, remove the outline
// here. The selection itself will be removed when the plot redraws
// at the end.
zoomlayer.selectAll('.select-outline').remove();
// until we get around to persistent selections, remove the outline
// here. The selection itself will be removed when the plot redraws
// at the end.
function clearSelect(gd) {
var fullLayout = gd._fullLayout || {};
var zoomlayer = fullLayout._zoomlayer;
if(zoomlayer) {
zoomlayer.selectAll('.select-outline').remove();
}
}

module.exports = {
Expand Down
23 changes: 17 additions & 6 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var Lib = require('../lib');
var Color = require('../components/color');
var BADNUM = require('../constants/numerical').BADNUM;

var axisIDs = require('../plots/cartesian/axis_ids');
var axisIDs = require('./cartesian/axis_ids');

var animationAttrs = require('./animation_attributes');
var frameAttrs = require('./frame_attributes');
Expand Down Expand Up @@ -476,6 +476,15 @@ plots.supplyDefaults = function(gd, opts) {
// clean subplots and other artifacts from previous plot calls
plots.cleanPlot(newFullData, newFullLayout, oldFullData, oldFullLayout);

// clear selection outline until we implement persistent selection,
// don't clear them though when drag handlers (e.g. listening to
// `plotly_selecting`) update the graph.
// we should try to come up with a better solution when implementing
// https://github.com/plotly/plotly.js/issues/1851
if(oldFullLayout._zoomlayer && !gd._dragging) {
oldFullLayout._zoomlayer.selectAll('.select-outline').remove();
}

// relink functions and _ attributes to promote consistency between plots
relinkPrivateKeys(newFullLayout, oldFullLayout);

Expand Down Expand Up @@ -779,10 +788,6 @@ plots.cleanPlot = function(newFullData, newFullLayout, oldFullData, oldFullLayou
oldFullLayout._infolayer.select('.cb' + oldUid).remove();
}
}

if(oldFullLayout._zoomlayer) {
oldFullLayout._zoomlayer.selectAll('.select-outline').remove();
}
};

plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
Expand Down Expand Up @@ -1679,10 +1684,10 @@ plots.purge = function(gd) {
// themselves, but may not if there was an error
delete gd._dragging;
delete gd._dragged;
delete gd._dragdata;
delete gd._hoverdata;
delete gd._snapshotInProgress;
delete gd._editing;
delete gd._replotPending;
delete gd._mouseDownTime;
delete gd._legendMouseDownTime;

Expand Down Expand Up @@ -2907,6 +2912,12 @@ plots.rehover = function(gd) {
}
};

plots.redrag = function(gd) {
if(gd._fullLayout._redrag) {
gd._fullLayout._redrag();
}
};

plots.generalUpdatePerTraceModule = function(gd, subplot, subplotCalcData, subplotLayout) {
var traceHashOld = subplot.traceHash;
var traceHash = {};
Expand Down
6 changes: 3 additions & 3 deletions src/plots/polar/polar.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ proto.updateMainDrag = function(fullLayout) {
zb = dragBox.makeZoombox(zoomlayer, lum, cx, cy, path0);
zb.attr('fill-rule', 'evenodd');
corners = dragBox.makeCorners(zoomlayer, cx, cy);
clearSelect(zoomlayer);
clearSelect(gd);
}

// N.B. this sets scoped 'r0' and 'r1'
Expand Down Expand Up @@ -1115,7 +1115,7 @@ proto.updateRadialDrag = function(fullLayout, polarLayout, rngIndex) {
dragOpts.moveFn = moveFn;
dragOpts.doneFn = doneFn;

clearSelect(fullLayout._zoomlayer);
clearSelect(gd);
};

dragOpts.clampFn = function(dx, dy) {
Expand Down Expand Up @@ -1263,7 +1263,7 @@ proto.updateAngularDrag = function(fullLayout) {
dragOpts.moveFn = moveFn;
dragOpts.doneFn = doneFn;

clearSelect(fullLayout._zoomlayer);
clearSelect(gd);
};

// I don't what we should do in this case, skip we now
Expand Down
Loading