Skip to content

Commit 66259ed

Browse files
authored
Merge pull request #1690 from plotly/drawing-perf-take2
Drawing perf take2
2 parents 3fdb308 + a8fba03 commit 66259ed

File tree

8 files changed

+37
-24
lines changed

8 files changed

+37
-24
lines changed

src/components/drawing/index.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,11 @@ drawing.steps = function(shape) {
542542
};
543543

544544
// off-screen svg render testing element, shared by the whole page
545-
// uses the id 'js-plotly-tester' and stores it in gd._tester
545+
// uses the id 'js-plotly-tester' and stores it in drawing.tester
546546
// makes a hash of cached text items in tester.node()._cache
547547
// so we can add references to rendered text (including all info
548548
// needed to fully determine its bounding rect)
549-
drawing.makeTester = function(gd) {
549+
drawing.makeTester = function() {
550550
var tester = d3.select('body')
551551
.selectAll('#js-plotly-tester')
552552
.data([0]);
@@ -579,16 +579,17 @@ drawing.makeTester = function(gd) {
579579
tester.node()._cache = {};
580580
}
581581

582-
gd._tester = tester;
583-
gd._testref = testref;
582+
drawing.tester = tester;
583+
drawing.testref = testref;
584584
};
585585

586586
// use our offscreen tester to get a clientRect for an element,
587587
// in a reference frame where it isn't translated and its anchor
588588
// point is at (0,0)
589589
// always returns a copy of the bbox, so the caller can modify it safely
590-
var savedBBoxes = [],
591-
maxSavedBBoxes = 10000;
590+
var savedBBoxes = [];
591+
var maxSavedBBoxes = 10000;
592+
592593
drawing.bBox = function(node) {
593594
// cache elements we've already measured so we don't have to
594595
// remeasure the same thing many times
@@ -597,22 +598,24 @@ drawing.bBox = function(node) {
597598
return Lib.extendFlat({}, savedBBoxes[saveNum.value]);
598599
}
599600

600-
var test3 = d3.select('#js-plotly-tester'),
601-
tester = test3.node();
601+
var tester3 = drawing.tester;
602+
var tester = tester3.node();
602603

603604
// copy the node to test into the tester
604605
var testNode = node.cloneNode(true);
605606
tester.appendChild(testNode);
607+
606608
// standardize its position... do we really want to do this?
607609
d3.select(testNode).attr({
608610
x: 0,
609611
y: 0,
610612
transform: ''
611613
});
612614

613-
var testRect = testNode.getBoundingClientRect(),
614-
refRect = test3.select('.js-reference-point')
615-
.node().getBoundingClientRect();
615+
var testRect = testNode.getBoundingClientRect();
616+
var refRect = drawing.testref
617+
.node()
618+
.getBoundingClientRect();
616619

617620
tester.removeChild(testNode);
618621

src/components/sliders/draw.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function keyFunction(opts) {
117117

118118
// Compute the dimensions (mutates sliderOpts):
119119
function findDimensions(gd, sliderOpts) {
120-
var sliderLabels = gd._tester.selectAll('g.' + constants.labelGroupClass)
120+
var sliderLabels = Drawing.tester.selectAll('g.' + constants.labelGroupClass)
121121
.data(sliderOpts.steps);
122122

123123
sliderLabels.enter().append('g')
@@ -154,7 +154,7 @@ function findDimensions(gd, sliderOpts) {
154154

155155
if(sliderOpts.currentvalue.visible) {
156156
// Get the dimensions of the current value label:
157-
var dummyGroup = gd._tester.append('g');
157+
var dummyGroup = Drawing.tester.append('g');
158158

159159
sliderLabels.each(function(stepOpts) {
160160
var curValPrefix = drawCurrentValue(dummyGroup, sliderOpts, stepOpts.label);

src/components/updatemenus/draw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ function findDimensions(gd, menuOpts) {
504504
menuOpts.lx = 0;
505505
menuOpts.ly = 0;
506506

507-
var fakeButtons = gd._tester.selectAll('g.' + constants.dropdownButtonClassName)
507+
var fakeButtons = Drawing.tester.selectAll('g.' + constants.dropdownButtonClassName)
508508
.data(menuOpts.buttons);
509509

510510
fakeButtons.enter().append('g')

src/lib/svg_text_utils.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ var Lib = require('../lib');
1717
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
1818
var stringMappings = require('../constants/string_mappings');
1919

20+
var DOM_PARSER;
21+
22+
exports.getDOMParser = function() {
23+
if(DOM_PARSER) {
24+
return DOM_PARSER;
25+
} else if(window.DOMParser) {
26+
DOM_PARSER = new window.DOMParser();
27+
return DOM_PARSER;
28+
} else {
29+
throw new Error('Cannot initialize DOMParser');
30+
}
31+
};
32+
2033
// Append SVG
2134

2235
d3.selection.prototype.appendSVG = function(_svgString) {
@@ -27,8 +40,9 @@ d3.selection.prototype.appendSVG = function(_svgString) {
2740
'</svg>'
2841
].join('');
2942

30-
var dom = new DOMParser().parseFromString(skeleton, 'application/xml'),
31-
childNode = dom.documentElement.firstChild;
43+
var domParser = exports.getDOMParser();
44+
var dom = domParser.parseFromString(skeleton, 'application/xml');
45+
var childNode = dom.documentElement.firstChild;
3246

3347
while(childNode) {
3448
this.node().appendChild(this.node().ownerDocument.importNode(childNode, true));

src/plot_api/plot_api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ Plotly.plot = function(gd, data, layout, config) {
9393
d3.select(gd).classed('js-plotly-plot', true);
9494

9595
// off-screen getBoundingClientRect testing space,
96-
// in #js-plotly-tester (and stored as gd._tester)
96+
// in #js-plotly-tester (and stored as Drawing.tester)
9797
// so we can share cached text across tabs
98-
Drawing.makeTester(gd);
98+
Drawing.makeTester();
9999

100100
// collect promises for any async actions during plotting
101101
// any part of the plotting code can push to gd._promises, then

src/plots/plots.js

-2
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,6 @@ plots.purge = function(gd) {
11731173

11741174
// these get recreated on Plotly.plot anyway, but just to be safe
11751175
// (and to have a record of them...)
1176-
delete gd._tester;
1177-
delete gd._testref;
11781176
delete gd._promises;
11791177
delete gd._redrawTimer;
11801178
delete gd.firstscatter;

src/traces/carpet/plot.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function plotOne(gd, plotinfo, cd) {
5959
drawGridLines(xa, ya, boundaryLayer, aax, 'a-boundary', aax._boundarylines);
6060
drawGridLines(xa, ya, boundaryLayer, bax, 'b-boundary', bax._boundarylines);
6161

62-
var maxAExtent = drawAxisLabels(gd._tester, xa, ya, trace, t, labelLayer, aax._labels, 'a-label');
63-
var maxBExtent = drawAxisLabels(gd._tester, xa, ya, trace, t, labelLayer, bax._labels, 'b-label');
62+
var maxAExtent = drawAxisLabels(Drawing.tester, xa, ya, trace, t, labelLayer, aax._labels, 'a-label');
63+
var maxBExtent = drawAxisLabels(Drawing.tester, xa, ya, trace, t, labelLayer, bax._labels, 'b-label');
6464

6565
drawAxisTitles(labelLayer, trace, t, xa, ya, maxAExtent, maxBExtent);
6666

test/jasmine/tests/plots_test.js

-2
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,6 @@ describe('Test Plots', function() {
428428
expect(gd.undonum).toBeUndefined();
429429
expect(gd.autoplay).toBeUndefined();
430430
expect(gd.changed).toBeUndefined();
431-
expect(gd._tester).toBeUndefined();
432-
expect(gd._testref).toBeUndefined();
433431
expect(gd._promises).toBeUndefined();
434432
expect(gd._redrawTimer).toBeUndefined();
435433
expect(gd.firstscatter).toBeUndefined();

0 commit comments

Comments
 (0)