Skip to content

Commit c065286

Browse files
committed
introduce cartesian.plot method:
- handles plot across cartesian subplots, in a similar way to how gl3d, geo and gl2d module operate.
1 parent c537cc8 commit c065286

File tree

2 files changed

+100
-91
lines changed

2 files changed

+100
-91
lines changed

src/plot_api/plot_api.js

+13-91
Original file line numberDiff line numberDiff line change
@@ -245,61 +245,15 @@ Plotly.plot = function(gd, data, layout, config) {
245245
return Plotly.Axes.doTicks(gd, 'redraw');
246246
}
247247

248+
// Now plot the data
248249
function drawData() {
249-
// Now plot the data
250-
var calcdata = gd.calcdata,
251-
subplots = Plots.getSubplotIds(fullLayout, 'cartesian'),
252-
modules = gd._modules;
253-
254-
var i, j, trace, subplot, subplotInfo,
255-
cdSubplot, cdError, cdModule, _module;
256-
257-
function getCdSubplot(calcdata, subplot) {
258-
var cdSubplot = [];
259-
260-
for(var i = 0; i < calcdata.length; i++) {
261-
var cd = calcdata[i];
262-
var trace = cd[0].trace;
263-
264-
if(trace.xaxis + trace.yaxis === subplot) {
265-
cdSubplot.push(cd);
266-
}
267-
}
268-
269-
return cdSubplot;
270-
}
271-
272-
function getCdModule(cdSubplot, _module) {
273-
var cdModule = [];
274-
275-
for(var i = 0; i < cdSubplot.length; i++) {
276-
var cd = cdSubplot[i];
277-
var trace = cd[0].trace;
278-
279-
if((trace._module === _module) && (trace.visible === true)) {
280-
cdModule.push(cd);
281-
}
282-
}
283-
284-
return cdModule;
285-
}
286-
287-
// clean up old scenes that no longer have associated data
288-
// will this be a performance hit?
289-
290-
var plotRegistry = Plots.subplotsRegistry;
291-
292-
// TODO incorporate cartesian and polar plots into this paradigm
293-
if(fullLayout._hasGL3D) plotRegistry.gl3d.plot(gd);
294-
if(fullLayout._hasGeo) plotRegistry.geo.plot(gd);
295-
if(fullLayout._hasGL2D) plotRegistry.gl2d.plot(gd);
250+
var calcdata = gd.calcdata;
296251

297252
// in case of traces that were heatmaps or contour maps
298253
// previously, remove them and their colorbars explicitly
299-
for (i = 0; i < calcdata.length; i++) {
300-
trace = calcdata[i][0].trace;
301-
302-
var isVisible = (trace.visible === true),
254+
for(var i = 0; i < calcdata.length; i++) {
255+
var trace = calcdata[i][0].trace,
256+
isVisible = (trace.visible === true),
303257
uid = trace.uid;
304258

305259
if(!isVisible || !Plots.traceIs(trace, '2dMap')) {
@@ -315,49 +269,17 @@ Plotly.plot = function(gd, data, layout, config) {
315269
}
316270
}
317271

318-
for (i = 0; i < subplots.length; i++) {
319-
subplot = subplots[i];
320-
subplotInfo = fullLayout._plots[subplot];
321-
cdSubplot = getCdSubplot(calcdata, subplot);
322-
cdError = [];
323-
324-
// remove old traces, then redraw everything
325-
// TODO: use enter/exit appropriately in the plot functions
326-
// so we don't need this - should sometimes be a big speedup
327-
if(subplotInfo.plot) subplotInfo.plot.selectAll('g.trace').remove();
328-
329-
for(j = 0; j < modules.length; j++) {
330-
_module = modules[j];
331-
332-
if(_module.basePlotModule.name !== 'cartesian') continue;
333-
334-
if(!_module.plot && (_module.name === 'pie')) continue;
335-
336-
// plot all traces of this type on this subplot at once
337-
cdModule = getCdModule(cdSubplot, _module);
338-
_module.plot(gd, subplotInfo, cdModule);
339-
Lib.markTime('done ' + (cdModule[0] && cdModule[0][0].trace.type));
340-
341-
// collect the traces that may have error bars
342-
if(cdModule[0] && cdModule[0][0].trace && Plots.traceIs(cdModule[0][0].trace, 'errorBarsOK')) {
343-
cdError = cdError.concat(cdModule);
344-
}
345-
}
272+
var plotRegistry = Plots.subplotsRegistry;
346273

347-
// finally do all error bars at once
348-
if(fullLayout._hasCartesian) {
349-
ErrorBars.plot(gd, subplotInfo, cdError);
350-
Lib.markTime('done ErrorBars');
351-
}
274+
if(fullLayout._hasGL3D) plotRegistry.gl3d.plot(gd);
275+
if(fullLayout._hasGeo) plotRegistry.geo.plot(gd);
276+
if(fullLayout._hasGL2D) plotRegistry.gl2d.plot(gd);
277+
if(fullLayout._hasCartesian || fullLayout._hasPie) {
278+
plotRegistry.cartesian.plot(gd);
352279
}
353280

354-
// now draw stuff not on subplots (ie, only pies at the moment)
355-
if(fullLayout._hasPie) {
356-
var Pie = Plots.getModule('pie');
357-
var cdPie = getCdModule(calcdata, Pie);
358-
359-
if(cdPie.length) Pie.plot(gd, cdPie);
360-
}
281+
// clean up old scenes that no longer have associated data
282+
// will this be a performance hit?
361283

362284
// styling separate from drawing
363285
Plots.style(gd);

src/plots/cartesian/index.js

+87
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
13+
var Plots = require('../plots');
14+
var ErrorBars = require('../../components/errorbars');
15+
1216

1317
exports.name = 'cartesian';
1418

@@ -27,3 +31,86 @@ exports.attrRegex = {
2731
x: /^xaxis([2-9]|[1-9][0-9]+)?$/,
2832
y: /^yaxis([2-9]|[1-9][0-9]+)?$/
2933
};
34+
35+
exports.plot = function(gd) {
36+
var fullLayout = gd._fullLayout,
37+
subplots = Plots.getSubplotIds(fullLayout, 'cartesian'),
38+
calcdata = gd.calcdata,
39+
modules = gd._modules;
40+
41+
function getCdSubplot(calcdata, subplot) {
42+
var cdSubplot = [];
43+
44+
for(var i = 0; i < calcdata.length; i++) {
45+
var cd = calcdata[i];
46+
var trace = cd[0].trace;
47+
48+
if(trace.xaxis + trace.yaxis === subplot) {
49+
cdSubplot.push(cd);
50+
}
51+
}
52+
53+
return cdSubplot;
54+
}
55+
56+
function getCdModule(cdSubplot, _module) {
57+
var cdModule = [];
58+
59+
for(var i = 0; i < cdSubplot.length; i++) {
60+
var cd = cdSubplot[i];
61+
var trace = cd[0].trace;
62+
63+
if((trace._module === _module) && (trace.visible === true)) {
64+
cdModule.push(cd);
65+
}
66+
}
67+
68+
return cdModule;
69+
}
70+
71+
for(var i = 0; i < subplots.length; i++) {
72+
var subplot = subplots[i],
73+
subplotInfo = fullLayout._plots[subplot],
74+
cdSubplot = getCdSubplot(calcdata, subplot),
75+
cdError = [];
76+
77+
// remove old traces, then redraw everything
78+
// TODO: use enter/exit appropriately in the plot functions
79+
// so we don't need this - should sometimes be a big speedup
80+
if(subplotInfo.plot) subplotInfo.plot.selectAll('g.trace').remove();
81+
82+
for(var j = 0; j < modules.length; j++) {
83+
var _module = modules[j];
84+
85+
// skip over non-cartesian trace modules
86+
if(_module.basePlotModule.name !== 'cartesian') continue;
87+
88+
// skip over pies, there are drawn below
89+
if(_module.name === 'pie') continue;
90+
91+
// plot all traces of this type on this subplot at once
92+
var cdModule = getCdModule(cdSubplot, _module);
93+
_module.plot(gd, subplotInfo, cdModule);
94+
Lib.markTime('done ' + (cdModule[0] && cdModule[0][0].trace.type));
95+
96+
// collect the traces that may have error bars
97+
if(cdModule[0] && cdModule[0][0].trace && Plots.traceIs(cdModule[0][0].trace, 'errorBarsOK')) {
98+
cdError = cdError.concat(cdModule);
99+
}
100+
}
101+
102+
// finally do all error bars at once
103+
if(fullLayout._hasCartesian) {
104+
ErrorBars.plot(gd, subplotInfo, cdError);
105+
Lib.markTime('done ErrorBars');
106+
}
107+
}
108+
109+
// now draw stuff not on subplots (ie, only pies at the moment)
110+
if(fullLayout._hasPie) {
111+
var Pie = Plots.getModule('pie');
112+
var cdPie = getCdModule(calcdata, Pie);
113+
114+
if(cdPie.length) Pie.plot(gd, cdPie);
115+
}
116+
};

0 commit comments

Comments
 (0)