Skip to content

Replace _has<PlotType> variables with '_has()' fullLayout method #491

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 27 commits into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
88f552a
mv gd._modules -> fullLayout._modules
etpinard Apr 27, 2016
525e3c7
lint
etpinard Apr 27, 2016
876ce4b
attach has (plot type) method to fullLayout,
etpinard Apr 27, 2016
f45ffec
add tempory block _has() -> _hasCartesian etc.
etpinard Apr 27, 2016
4e0c6da
rm _has??? attributes
etpinard Apr 27, 2016
93493a6
make 'pie' a layout category,
etpinard Apr 27, 2016
e63ba4a
check for hasCartesian && !hasPie for 'hovermode',
etpinard Apr 28, 2016
b655cb0
Revert "check for hasCartesian && !hasPie for 'hovermode',"
etpinard May 3, 2016
f476e05
split pie trace base plot module from cartesian:
etpinard May 3, 2016
1c41c2e
rm (now useless) trace module layoutCategories
etpinard May 3, 2016
313150e
add fill unique lib function,
etpinard May 6, 2016
392619c
fill fullLayout._basePlotModules list in default trace loop,
etpinard May 6, 2016
3787d47
loop over basePlotModules instead of subplotRegistry in:
etpinard May 6, 2016
68b12c4
make sure to add cartesian base plot module to orphan axis graphs:
etpinard May 6, 2016
a41427c
move 3d axes init step from makePlotFrameork -> Gl3d.plot
etpinard May 6, 2016
a48d071
check for non-cartesian/pie/ternary base plot module in restyle,
etpinard May 6, 2016
a78521c
handle a few polar-only cases:
etpinard May 6, 2016
aa2ec8b
sub has??? -> has('???')
etpinard May 6, 2016
77fb7e2
loop over (trace) modules in trace supplyLayoutDefaults:
etpinard May 6, 2016
a141d78
robustify pie clean step:
etpinard May 6, 2016
dc297ca
put ref of hasPlotType method on Plots:
etpinard May 6, 2016
e3f72f0
move block of _has?? = _has('') after all defaults are filled:
etpinard May 6, 2016
c59a960
update tests
etpinard May 6, 2016
9f94494
add info about data/layout and fullData/fullLayout
etpinard May 6, 2016
9837d8b
Merge branch 'master' into has-plot-type
etpinard May 6, 2016
0c7b2f8
rename Lib.fillUnique -> Lib.pushUnique
etpinard May 9, 2016
5e6759b
rm unnecessary boolean type coercion
etpinard May 9, 2016
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
2 changes: 1 addition & 1 deletion src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ function toggleHover(gd) {
var fullLayout = gd._fullLayout;

var onHoverVal;
if(fullLayout._hasCartesian) {
if(fullLayout._hasCartesian && !fullLayout._hasPie) {
onHoverVal = fullLayout._isHoriz ? 'y' : 'x';
}
else onHoverVal = 'closest';
Expand Down
2 changes: 1 addition & 1 deletion src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Plotly.plot = function(gd, data, layout, config) {
if(!recalc) return;

var subplots = Plots.getSubplotIds(fullLayout, 'cartesian'),
modules = gd._modules;
modules = fullLayout._modules;

// position and range calculations for traces that
// depend on each other ie bars (stacked or grouped)
Expand Down
2 changes: 1 addition & 1 deletion src/plots/cartesian/graph_interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fx.supplyLayoutDefaults = function(layoutIn, layoutOut, fullData) {
coerce('dragmode');

var hovermodeDflt;
if(layoutOut._hasCartesian) {
if(layoutOut._hasCartesian && !layoutOut._hasPie) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... as pie plots are now hasCartesian and hasPie.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is not really the same logic... what if you have both pie and cartesian for real?

I know pie is my own doing, but why does it need to have cartesian as its basePlotModule? Can we rip that out into its own thing? That's probably a bigger project, but as it is it's not only hacky but tough to get the logic right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rip that out into its own thing

Good call.

That's probably a bigger project

It should be pretty easy I think. I'll give it a shot in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy. Done in f476e05

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, that's a lot easier than I was afraid it would be 💥

// flag for 'horizontal' plots:
// determines the state of the mode bar 'compare' hovermode button
var isHoriz = layoutOut._isHoriz = fx.isHoriz(fullData);
Expand Down
2 changes: 1 addition & 1 deletion src/plots/cartesian/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.plot = function(gd) {
var fullLayout = gd._fullLayout,
subplots = Plots.getSubplotIds(fullLayout, 'cartesian'),
calcdata = gd.calcdata,
modules = gd._modules;
modules = fullLayout._modules;

function getCdSubplot(calcdata, subplot) {
var cdSubplot = [];
Expand Down
25 changes: 1 addition & 24 deletions src/plots/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,30 +168,7 @@ module.exports = {
role: 'info',
description: 'Determines whether or not a legend is drawn.'
},
_hasCartesian: {
valType: 'boolean',
dflt: false
},
_hasGL3D: {
valType: 'boolean',
dflt: false
},
_hasGeo: {
valType: 'boolean',
dflt: false
},
_hasPie: {
valType: 'boolean',
dflt: false
},
_hasGL2D: {
valType: 'boolean',
dflt: false
},
_hasTernary: {
valType: 'boolean',
dflt: false
},

_composedModules: {
'*': 'Fx'
},
Expand Down
96 changes: 57 additions & 39 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ plots.getSubplotIds = function getSubplotIds(layout, type) {
if(type === 'cartesian' && !layout._hasCartesian) return [];
if(type === 'gl2d' && !layout._hasGL2D) return [];
if(type === 'cartesian' || type === 'gl2d') {
return Object.keys(layout._plots);
return Object.keys(layout._plots || {});
}

var idRegex = _module.idRegex,
Expand Down Expand Up @@ -435,22 +435,29 @@ plots.sendDataToCloud = function(gd) {
return false;
};

// Fill in default values:
//
// gd.data, gd.layout:
// are precisely what the user specified
//
// gd._fullData, gd._fullLayout:
// are complete descriptions of how to draw the plot
//
// gd._fullLayout._modules
// is a list of all the trace modules required to draw the plot
//
plots.supplyDefaults = function(gd) {
// fill in default values:
// gd.data, gd.layout:
// are precisely what the user specified
// gd._fullData, gd._fullLayout:
// are complete descriptions of how to draw the plot
var oldFullLayout = gd._fullLayout || {},
newFullLayout = gd._fullLayout = {},
newLayout = gd.layout || {},
oldFullData = gd._fullData || [],
newLayout = gd.layout || {};

var oldFullData = gd._fullData || [],
newFullData = gd._fullData = [],
newData = gd.data || [],
modules = gd._modules = [];
newData = gd.data || [];

var i, trace, fullTrace, _module, axList, ax;
var modules = newFullLayout._modules = [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense (and is more useful) to have _modules onto fullLayout.


var i, _module;

// first fill in what we can of layout without looking at data
// because fullData needs a few things from layout
Expand All @@ -461,24 +468,28 @@ plots.supplyDefaults = function(gd) {

// then do the data
for(i = 0; i < newData.length; i++) {
trace = newData[i];

fullTrace = plots.supplyDataDefaults(trace, i, newFullLayout);
var fullTrace = plots.supplyDataDefaults(newData[i], i, newFullLayout);
newFullData.push(fullTrace);

// detect plot type
if(plots.traceIs(fullTrace, 'cartesian')) newFullLayout._hasCartesian = true;
else if(plots.traceIs(fullTrace, 'gl3d')) newFullLayout._hasGL3D = true;
else if(plots.traceIs(fullTrace, 'geo')) newFullLayout._hasGeo = true;
else if(plots.traceIs(fullTrace, 'pie')) newFullLayout._hasPie = true;
else if(plots.traceIs(fullTrace, 'gl2d')) newFullLayout._hasGL2D = true;
else if(plots.traceIs(fullTrace, 'ternary')) newFullLayout._hasTernary = true;
else if('r' in fullTrace) newFullLayout._hasPolar = true;
// detect polar
if('r' in fullTrace) newFullLayout._hasPolar = true;

// fill in modules list
_module = fullTrace._module;
if(_module && modules.indexOf(_module)===-1) modules.push(_module);
if(_module && modules.indexOf(_module) === -1) modules.push(_module);
}

// attach helper method
newFullLayout._has = hasPlotType.bind(newFullLayout);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdtusz I know this could be more verbose, but I really like the look of e.g. fullLayout._has('geo')


// temporary block (before replace all _has??? with _hasPlotType() ?
newFullLayout._hasCartesian = newFullLayout._has('cartesian');
newFullLayout._hasGeo = newFullLayout._has('geo');
newFullLayout._hasGL3D = newFullLayout._has('gl3d');
newFullLayout._hasGL2D = newFullLayout._has('gl2d');
newFullLayout._hasTernary = newFullLayout._has('ternary');
newFullLayout._hasPie = newFullLayout._has('pie');
Copy link
Contributor Author

@etpinard etpinard Apr 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this block ⏫ will be gone once I get some 👍 s

EDIT: No. we need keep this to preserve backward compatibility (and not break the old plot.ly workspace).


// special cases that introduce interactions between traces
for(i = 0; i < modules.length; i++) {
_module = modules[i];
Expand All @@ -497,32 +508,46 @@ plots.supplyDefaults = function(gd) {
// clean subplots and other artifacts from previous plot calls
plots.cleanPlot(newFullData, newFullLayout, oldFullData, oldFullLayout);

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

plots.doAutoMargin(gd);

// can't quite figure out how to get rid of this... each axis needs
// a reference back to the DOM object for just a few purposes
axList = Plotly.Axes.list(gd);
var axList = Plotly.Axes.list(gd);
for(i = 0; i < axList.length; i++) {
ax = axList[i];
var ax = axList[i];
ax._gd = gd;
ax.setScale();
}

// update object references in calcdata
if((gd.calcdata || []).length === newFullData.length) {
for(i = 0; i < newFullData.length; i++) {
trace = newFullData[i];
var trace = newFullData[i];
(gd.calcdata[i][0] || {}).trace = trace;
}
}
};

// helper function to be bound to fullLayout to check
// whether a certain plot type or layout categories is present on plot
function hasPlotType(category) {
var modules = this._modules || [];

for(var i = 0; i < modules.length; i++) {
var _module = modules[i];

if(_module.basePlotModule.name === category) return true;

var layoutCategories = _module.layoutCategories || [];
if(layoutCategories.indexOf(category) !== -1) return true;
}

return false;
}

plots.cleanPlot = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var i, j;

Expand Down Expand Up @@ -713,18 +738,12 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut) {
coerce('separators');
coerce('hidesources');
coerce('smith');
coerce('_hasCartesian');
coerce('_hasGL3D');
coerce('_hasGeo');
coerce('_hasPie');
coerce('_hasGL2D');
coerce('_hasTernary');
};

plots.supplyLayoutModuleDefaults = function(layoutIn, layoutOut, fullData) {
var i, _module;

// TODO incorporate into subplotRegistry
// TODO incorporate into subplotsRegistry
Plotly.Axes.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏫ important


// plot module layout defaults
Expand Down Expand Up @@ -792,7 +811,6 @@ plots.purge = function(gd) {

// these get recreated on Plotly.plot anyway, but just to be safe
// (and to have a record of them...)
delete gd._modules;
delete gd._tester;
delete gd._testref;
delete gd._promises;
Expand All @@ -810,7 +828,7 @@ plots.purge = function(gd) {
};

plots.style = function(gd) {
var _modules = gd._modules;
var _modules = gd._fullLayout._modules;

for(var i = 0; i < _modules.length; i++) {
var _module = _modules[i];
Expand Down
1 change: 1 addition & 0 deletions src/traces/pie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Pie.moduleType = 'trace';
Pie.name = 'pie';
Pie.basePlotModule = require('../../plots/cartesian');
Pie.categories = ['pie', 'showLegend'];
Pie.layoutCategories = ['pie'];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMPORTANT pie plots now have _has('cartesian') AND _has('pie') return true.

Pie.meta = {
description: [
'A data visualized by the sectors of the pie is set in `values`.',
Expand Down
19 changes: 11 additions & 8 deletions src/traces/scatterternary/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
var scatterStyle = require('../scatter/style');


module.exports = function style(graphDiv) {
for(var i = 0; i < graphDiv._modules.length; i++) {
// we're just going to call scatter style... if we already
// called it, don't need to redo.
// Later though we may want differences, or we may make style
// more specific in its scope, then we can remove this.
if(graphDiv._modules[i].name === 'scatter') return;
module.exports = function style(gd) {
var modules = gd._fullLayout._modules;

// we're just going to call scatter style... if we already
// called it, don't need to redo.
// Later though we may want differences, or we may make style
// more specific in its scope, then we can remove this.
for(var i = 0; i < modules.length; i++) {
if(modules[i].name === 'scatter') return;
}
scatterStyle(graphDiv);

scatterStyle(gd);
};
1 change: 0 additions & 1 deletion test/jasmine/tests/plots_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ describe('Test Plots', function() {
expect(gd.undonum).toBeUndefined();
expect(gd.autoplay).toBeUndefined();
expect(gd.changed).toBeUndefined();
expect(gd._modules).toBeUndefined();
expect(gd._tester).toBeUndefined();
expect(gd._testref).toBeUndefined();
expect(gd._promises).toBeUndefined();
Expand Down