Skip to content

Commit eb7ba35

Browse files
committed
add Axes.redrawComponents
- to DRY up pan/scroll and axes transition (next commit) annotations/shapes/images redraw calls. - add ax._imgIndices stash (similar to _annIndices/_shapeIndices), to make redrawComponents loop just once over the axis list.
1 parent fd121f4 commit eb7ba35

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

src/components/images/defaults.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ function imageDefaults(imageIn, imageOut, fullLayout) {
5252
var axLetter = axLetters[i];
5353
var axRef = Axes.coerceRef(imageIn, imageOut, gdMock, axLetter, 'paper');
5454

55+
if(axRef !== 'paper') {
56+
var ax = Axes.getFromId(gdMock, axRef);
57+
ax._imgIndices.push(imageOut._index);
58+
}
59+
5560
Axes.coercePosition(imageOut, gdMock, coerce, axRef, axLetter, 0);
5661
}
5762

src/plots/cartesian/axes.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,39 @@ axes.cleanPosition = function(pos, gd, axRef) {
132132
return cleanPos(pos);
133133
};
134134

135+
axes.redrawComponents = function(gd, axIds) {
136+
axIds = axIds ? axIds : axes.listIds(gd);
137+
138+
var fullLayout = gd._fullLayout;
139+
140+
function _redrawOneComp(moduleName, methodName, stashName, shortCircuit) {
141+
var method = Registry.getComponentMethod(moduleName, methodName);
142+
var stash = {};
143+
144+
for(var i = 0; i < axIds.length; i++) {
145+
var ax = fullLayout[axes.id2name(axIds[i])];
146+
var indices = ax[stashName];
147+
148+
for(var j = 0; j < indices.length; j++) {
149+
var ind = indices[j];
150+
151+
if(!stash[ind]) {
152+
method(gd, ind);
153+
stash[ind] = 1;
154+
// once is enough for images (which doesn't use the `i` arg anyway)
155+
if(shortCircuit) return;
156+
}
157+
}
158+
}
159+
}
160+
161+
// annotations and shapes 'draw' method is slow,
162+
// use the finer-grained 'drawOne' method instead
163+
_redrawOneComp('annotations', 'drawOne', '_annIndices');
164+
_redrawOneComp('shapes', 'drawOne', '_shapeIndices');
165+
_redrawOneComp('images', 'draw', '_imgIndices', true);
166+
};
167+
135168
var getDataConversions = axes.getDataConversions = function(gd, trace, target, targetArray) {
136169
var ax;
137170

src/plots/cartesian/dragbox.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
490490

491491
// viewbox redraw at first
492492
updateSubplots(scrollViewBox);
493-
ticksAndAnnotations(ns, ew);
493+
ticksAndAnnotations();
494494

495495
// then replot after a delay to make sure
496496
// no more scrolling is coming
@@ -522,7 +522,7 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
522522
if(xActive) dragAxList(xaxes, dx);
523523
if(yActive) dragAxList(yaxes, dy);
524524
updateSubplots([xActive ? -dx : 0, yActive ? -dy : 0, pw, ph]);
525-
ticksAndAnnotations(yActive, xActive);
525+
ticksAndAnnotations();
526526
return;
527527
}
528528

@@ -594,12 +594,12 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
594594
}
595595

596596
updateSubplots([x0, y0, pw - dx, ph - dy]);
597-
ticksAndAnnotations(yActive, xActive);
597+
ticksAndAnnotations();
598598
}
599599

600600
// Draw ticks and annotations (and other components) when ranges change.
601601
// Also records the ranges that have changed for use by update at the end.
602-
function ticksAndAnnotations(ns, ew) {
602+
function ticksAndAnnotations() {
603603
var activeAxIds = [];
604604
var i;
605605

@@ -627,25 +627,7 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
627627
updates[ax._name + '.range[1]'] = ax.range[1];
628628
}
629629

630-
function redrawObjs(objArray, method, shortCircuit) {
631-
for(i = 0; i < objArray.length; i++) {
632-
var obji = objArray[i];
633-
634-
if((ew && activeAxIds.indexOf(obji.xref) !== -1) ||
635-
(ns && activeAxIds.indexOf(obji.yref) !== -1)) {
636-
method(gd, i);
637-
// once is enough for images (which doesn't use the `i` arg anyway)
638-
if(shortCircuit) return;
639-
}
640-
}
641-
}
642-
643-
// annotations and shapes 'draw' method is slow,
644-
// use the finer-grained 'drawOne' method instead
645-
646-
redrawObjs(gd._fullLayout.annotations || [], Registry.getComponentMethod('annotations', 'drawOne'));
647-
redrawObjs(gd._fullLayout.shapes || [], Registry.getComponentMethod('shapes', 'drawOne'));
648-
redrawObjs(gd._fullLayout.images || [], Registry.getComponentMethod('images', 'draw'), true);
630+
Axes.redrawComponents(gd, activeAxIds);
649631
}
650632

651633
function doubleClick() {

src/plots/cartesian/layout_defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
156156
axLayoutOut._traceIndices = traces.map(function(t) { return t._expandedIndex; });
157157
axLayoutOut._annIndices = [];
158158
axLayoutOut._shapeIndices = [];
159+
axLayoutOut._imgIndices = [];
159160
axLayoutOut._subplotsWith = [];
160161
axLayoutOut._counterAxes = [];
161162

test/jasmine/tests/annotations_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ describe('animating annotations', function() {
16031603
{
16041604
annotations: [{text: 'hello'}],
16051605
shapes: [{fillcolor: 'rgb(170, 170, 170)'}],
1606-
images: [{source: img1}]
1606+
images: [{source: img1, xref: 'x', yref: 'y'}]
16071607
}
16081608
).then(function() {
16091609
assertAnnotations(['hello']);

0 commit comments

Comments
 (0)