Skip to content

Commit 4145990

Browse files
committed
Merge pull request #160 from plotly/plot-index
Make Plotly.plot agnostic to plot modules
2 parents dc90a91 + 7ccd79f commit 4145990

File tree

18 files changed

+411
-287
lines changed

18 files changed

+411
-287
lines changed

src/plot_api/plot_api.js

Lines changed: 10 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,12 @@ Plotly.plot = function(gd, data, layout, config) {
266266
// clean up old scenes that no longer have associated data
267267
// will this be a performance hit?
268268

269-
// ... until subplot of different type play better together
270-
if(gd._fullLayout._hasGL3D) plotGl3d(gd);
271-
if(gd._fullLayout._hasGeo) plotGeo(gd);
272-
if(gd._fullLayout._hasGL2D) plotGl2d(gd);
269+
var plotRegistry = plots.subplotsRegistry;
270+
271+
// TODO incorporate cartesian and polar plots into this paradigm
272+
if(fullLayout._hasGL3D) plotRegistry.gl3d.plot(gd);
273+
if(fullLayout._hasGeo) plotRegistry.geo.plot(gd);
274+
if(fullLayout._hasGL2D) plotRegistry.gl2d.plot(gd);
273275

274276
// in case of traces that were heatmaps or contour maps
275277
// previously, remove them and their colorbars explicitly
@@ -418,109 +420,6 @@ function setPlotContext(gd, config) {
418420
}
419421
}
420422

421-
function plotGl3d(gd) {
422-
var fullLayout = gd._fullLayout,
423-
fullData = gd._fullData,
424-
sceneIds = plots.getSubplotIds(fullLayout, 'gl3d');
425-
426-
var i, sceneId, fullSceneData, scene, sceneOptions;
427-
428-
fullLayout._paperdiv.style({
429-
width: fullLayout.width + 'px',
430-
height: fullLayout.height + 'px'
431-
});
432-
433-
gd._context.setBackground(gd, fullLayout.paper_bgcolor);
434-
435-
for (i = 0; i < sceneIds.length; i++) {
436-
sceneId = sceneIds[i];
437-
fullSceneData = plots.getSubplotData(fullData, 'gl3d', sceneId);
438-
scene = fullLayout[sceneId]._scene; // ref. to corresp. Scene instance
439-
440-
// If Scene is not instantiated, create one!
441-
if(scene === undefined) {
442-
sceneOptions = {
443-
container: gd.querySelector('.gl-container'),
444-
id: sceneId,
445-
staticPlot: gd._context.staticPlot,
446-
plotGlPixelRatio: gd._context.plotGlPixelRatio
447-
};
448-
scene = new Plotly.Scene(sceneOptions, fullLayout);
449-
fullLayout[sceneId]._scene = scene; // set ref to Scene instance
450-
}
451-
452-
scene.plot(fullSceneData, fullLayout, gd.layout); // takes care of business
453-
}
454-
}
455-
456-
function plotGeo(gd) {
457-
var fullLayout = gd._fullLayout,
458-
fullData = gd._fullData,
459-
geoIds = plots.getSubplotIds(fullLayout, 'geo');
460-
461-
var i, geoId, fullGeoData, geo;
462-
463-
// if 'plotly-geo-assets.js' is not included,
464-
// initialize object to keep reference to every loaded topojson
465-
if(window.PlotlyGeoAssets === undefined) {
466-
window.PlotlyGeoAssets = { topojson : {} };
467-
}
468-
469-
for (i = 0; i < geoIds.length; i++) {
470-
geoId = geoIds[i];
471-
fullGeoData = plots.getSubplotData(fullData, 'geo', geoId);
472-
geo = fullLayout[geoId]._geo;
473-
474-
// If geo is not instantiated, create one!
475-
if(geo === undefined) {
476-
geo = new Plotly.Geo(
477-
{
478-
id: geoId,
479-
container: fullLayout._geocontainer.node(),
480-
topojsonURL: gd._context.topojsonURL
481-
},
482-
fullLayout
483-
);
484-
fullLayout[geoId]._geo = geo;
485-
}
486-
487-
geo.plot(fullGeoData, fullLayout, gd._promises);
488-
}
489-
}
490-
491-
function plotGl2d(gd) {
492-
var fullLayout = gd._fullLayout,
493-
fullData = gd._fullData,
494-
subplotIds = plots.getSubplotIds(fullLayout, 'gl2d');
495-
496-
for(var i = 0; i < subplotIds.length; i++) {
497-
var subplotId = subplotIds[i],
498-
subplotObj = fullLayout._plots[subplotId],
499-
fullSubplotData = plots.getSubplotData(fullData, 'gl2d', subplotId);
500-
var scene;
501-
502-
// ref. to corresp. Scene instance
503-
scene = subplotObj._scene2d;
504-
505-
// If Scene is not instantiated, create one!
506-
if(scene === undefined) {
507-
scene = new Plotly.Scene2D({
508-
container: gd.querySelector('.gl-container'),
509-
id: subplotId,
510-
staticPlot: gd._context.staticPlot,
511-
plotGlPixelRatio: gd._context.plotGlPixelRatio
512-
},
513-
fullLayout
514-
);
515-
516-
// set ref to Scene instance
517-
subplotObj._scene2d = scene;
518-
}
519-
520-
scene.plot(fullSubplotData, fullLayout, gd.layout);
521-
}
522-
}
523-
524423
function plotPolar(gd, data, layout) {
525424
// build or reuse the container skeleton
526425
var plotContainer = d3.select(gd).selectAll('.plot-container')
@@ -866,8 +765,8 @@ function cleanData(data, existingData) {
866765
if(trace.yaxis) trace.yaxis = Plotly.Axes.cleanId(trace.yaxis, 'y');
867766

868767
// scene ids scene1 -> scene
869-
if (trace.scene) {
870-
trace.scene = Plotly.Gl3dLayout.cleanId(trace.scene);
768+
if(plots.traceIs(trace, 'gl3d') && trace.scene) {
769+
trace.scene = plots.subplotsRegistry.gl3d.cleanId(trace.scene);
871770
}
872771

873772
if(!plots.traceIs(trace, 'pie')) {
@@ -2593,10 +2492,8 @@ function makePlotFramework(gd) {
25932492
var gd3 = d3.select(gd),
25942493
fullLayout = gd._fullLayout;
25952494

2596-
/*
2597-
* TODO - find a better place for 3D to initialize axes
2598-
*/
2599-
if(fullLayout._hasGL3D) Plotly.Gl3dLayout.initAxes(gd);
2495+
// TODO - find a better place for 3D to initialize axes
2496+
if(fullLayout._hasGL3D) plots.subplotsRegistry.gl3d.initAxes(gd);
26002497

26012498
// Plot container
26022499
fullLayout._container = gd3.selectAll('.plot-container').data([0]);

src/plot_api/plot_schema.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
'use strict';
1111

1212
var Plotly = require('../plotly');
13+
var Plots = require('../plots/plots');
14+
var Lib = require('../lib');
1315

14-
var extendFlat = Plotly.Lib.extendFlat;
15-
var extendDeep = Plotly.Lib.extendDeep;
16-
var extendDeepAll = Plotly.Lib.extendDeepAll;
16+
var extendFlat = Lib.extendFlat;
17+
var extendDeep = Lib.extendDeep;
18+
var extendDeepAll = Lib.extendDeepAll;
1719

1820
var NESTED_MODULE = '_nestedModules',
1921
COMPOSED_MODULE = '_composedModules',
@@ -38,7 +40,7 @@ var PlotSchema = module.exports = {};
3840

3941

4042
PlotSchema.get = function() {
41-
Plotly.Plots.allTypes
43+
Plots.allTypes
4244
.concat('area') // FIXME polar 'area' attributes
4345
.forEach(getTraceAttributes);
4446

@@ -56,7 +58,7 @@ PlotSchema.crawl = function(attrs, callback) {
5658
callback(attr, attrName, attrs);
5759

5860
if(PlotSchema.isValObject(attr)) return;
59-
if(Plotly.Lib.isPlainObject(attr)) PlotSchema.crawl(attr, callback);
61+
if(Lib.isPlainObject(attr)) PlotSchema.crawl(attr, callback);
6062
});
6163
};
6264

@@ -65,7 +67,7 @@ PlotSchema.isValObject = function(obj) {
6567
};
6668

6769
function getTraceAttributes(type) {
68-
var globalAttributes = Plotly.Plots.attributes,
70+
var globalAttributes = Plots.attributes,
6971
_module = getModule({type: type}),
7072
meta = getMeta(type),
7173
subplotRegistry = getSubplotRegistry(type);
@@ -111,7 +113,7 @@ function getTraceAttributes(type) {
111113
}
112114

113115
function getLayoutAttributes() {
114-
var globalLayoutAttributes = Plotly.Plots.layoutAttributes,
116+
var globalLayoutAttributes = Plots.layoutAttributes,
115117
layoutAttributes = {};
116118

117119
// layout module attributes (+ nested + composed)
@@ -136,7 +138,7 @@ function getLayoutAttributes() {
136138

137139
function getDefs() {
138140
plotSchema.defs = {
139-
valObjects: Plotly.Lib.valObjects,
141+
valObjects: Lib.valObjects,
140142
metaKeys: UNDERSCORE_ATTRS.concat(['description', 'role'])
141143
};
142144
}
@@ -157,7 +159,7 @@ function coupleAttrs(attrsIn, attrsOut, whichAttrs, type) {
157159
nestedAttrs, {}, whichAttrs, type
158160
);
159161

160-
Plotly.Lib.nestedProperty(attrsOut, kk)
162+
Lib.nestedProperty(attrsOut, kk)
161163
.set(extendDeep({}, nestedReference));
162164
});
163165
return;
@@ -180,7 +182,7 @@ function coupleAttrs(attrsIn, attrsOut, whichAttrs, type) {
180182
return;
181183
}
182184

183-
attrsOut[k] = Plotly.Lib.isPlainObject(attrsIn[k]) ?
185+
attrsOut[k] = Lib.isPlainObject(attrsIn[k]) ?
184186
extendDeepAll({}, attrsIn[k]) :
185187
attrsIn[k];
186188
});
@@ -214,7 +216,7 @@ function mergeValTypeAndRole(attrs) {
214216
attrs[attrName + 'src'] = makeSrcAttr(attrName);
215217
}
216218
}
217-
else if(Plotly.Lib.isPlainObject(attr)) {
219+
else if(Lib.isPlainObject(attr)) {
218220
// all attrs container objects get role 'object'
219221
attr.role = 'object';
220222
}
@@ -229,9 +231,14 @@ function getModule(arg) {
229231
if('type' in arg) {
230232
return (arg.type === 'area') ? // FIXME
231233
{ attributes: polarAreaAttrs } :
232-
Plotly.Plots.getModule({type: arg.type});
234+
Plots.getModule({type: arg.type});
233235
}
234-
else if('module' in arg) return Plotly[arg.module];
236+
237+
var subplotsRegistry = Plots.subplotsRegistry,
238+
_module = arg.module;
239+
240+
if(subplotsRegistry[_module]) return subplotsRegistry[_module];
241+
else if('module' in arg) return Plotly[_module];
235242
}
236243

237244
function removeUnderscoreAttrs(attributes) {
@@ -244,7 +251,7 @@ function removeUnderscoreAttrs(attributes) {
244251

245252
function getMeta(type) {
246253
if(type === 'area') return {}; // FIXME
247-
return Plotly.Plots.modules[type].meta || {};
254+
return Plots.modules[type].meta || {};
248255
}
249256

250257
function assignPolarLayoutAttrs(layoutAttributes) {
@@ -261,9 +268,9 @@ function assignPolarLayoutAttrs(layoutAttributes) {
261268
function getSubplotRegistry(traceType) {
262269
if(traceType === 'area') return {}; // FIXME
263270

264-
var subplotsRegistry = Plotly.Plots.subplotsRegistry,
271+
var subplotsRegistry = Plots.subplotsRegistry,
265272
subplotType = Object.keys(subplotsRegistry).filter(function(subplotType) {
266-
return Plotly.Plots.traceIs({type: traceType}, subplotType);
273+
return Plots.traceIs({type: traceType}, subplotType);
267274
})[0];
268275

269276
if(subplotType === undefined) return {};
@@ -272,7 +279,7 @@ function getSubplotRegistry(traceType) {
272279
}
273280

274281
function handleSubplotObjs(layoutAttributes) {
275-
var subplotsRegistry = Plotly.Plots.subplotsRegistry;
282+
var subplotsRegistry = Plots.subplotsRegistry;
276283

277284
Object.keys(layoutAttributes).forEach(function(k) {
278285
Object.keys(subplotsRegistry).forEach(function(subplotType) {

src/plotly.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,23 @@ exports.MathJaxConfig = require('./fonts/mathjax_config');
3333
exports.defaultConfig = require('./plot_api/plot_config');
3434

3535
// plots
36-
exports.Plots = require('./plots/plots');
36+
var Plots = exports.Plots = require('./plots/plots');
37+
38+
var Cartesian = require('./plots/cartesian');
39+
Plots.registerSubplot(Cartesian);
40+
3741
exports.Axes = require('./plots/cartesian/axes');
3842
exports.Fx = require('./plots/cartesian/graph_interact');
39-
exports.Scene = require('./plots/gl3d/scene');
40-
exports.Gl3dLayout = require('./plots/gl3d/layout');
41-
exports.Geo = require('./plots/geo/geo');
42-
exports.GeoLayout = require('./plots/geo/layout');
43-
exports.Scene2D = require('./plots/gl2d/scene2d');
43+
44+
var Geo = require('./plots/geo');
45+
Plots.registerSubplot(Geo);
46+
47+
var Gl3d = require('./plots/gl3d');
48+
Plots.registerSubplot(Gl3d);
49+
50+
var Gl2d = require('./plots/gl2d');
51+
Plots.registerSubplot(Gl2d);
52+
4453
exports.micropolar = require('./plots/polar/micropolar');
4554

4655
// components

src/plots/cartesian/axes.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ var isNumeric = require('fast-isnumeric');
1515

1616
var axes = module.exports = {};
1717

18-
axes.attributes = require('./attributes');
19-
20-
Plotly.Plots.registerSubplot('cartesian', ['xaxis', 'yaxis'], ['x', 'y'], axes.attributes);
21-
2218
axes.layoutAttributes = require('./layout_attributes');
2319

2420
var xAxisMatch = /^xaxis[0-9]*$/,

src/plots/cartesian/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Plotly = require('../../plotly');
13+
14+
15+
exports.name = 'cartesian';
16+
17+
exports.attr = ['xaxis', 'yaxis'];
18+
19+
exports.idRoot = ['x', 'y'];
20+
21+
exports.attributes = require('./attributes');

0 commit comments

Comments
 (0)