Skip to content

A slightly less ambitious take on orphan subplots and 'delete last trace' #289

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 18 commits into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
ensure that Plots getSubplotIds return ordered arrays of ids
  • Loading branch information
etpinard committed Feb 26, 2016
commit 07ca162f454282eb443e42435ad08dc2e64a8171
31 changes: 23 additions & 8 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,19 @@ plots.findSubplotIds = function findSubplotIds(data, type) {
return subplotIds;
};

/**
* Get the ids of the current subplots.
*
* @param {object} layout plotly full layout object.
* @param {string} type subplot type to look for.
*
* @return {array} list of ordered subplot ids (strings).
*
*/
plots.getSubplotIds = function getSubplotIds(layout, type) {
if(plots.subplotsRegistry[type] === undefined) return [];
var _module = plots.subplotsRegistry[type];

if(_module === undefined) return [];

// layout must be 'fullLayout' here
if(type === 'cartesian' && !layout._hasCartesian) return [];
Expand All @@ -184,19 +195,23 @@ plots.getSubplotIds = function getSubplotIds(layout, type) {
return Object.keys(layout._plots);
}

var idRegex = plots.subplotsRegistry[type].idRegex,
var idRegex = _module.idRegex,
layoutKeys = Object.keys(layout),
subplotIds = [],
layoutKey;
subplotIds = [];

for(var i = 0; i < layoutKeys.length; i++) {
layoutKey = layoutKeys[i];
var layoutKey = layoutKeys[i];

if(idRegex.test(layoutKey)) subplotIds.push(layoutKey);
}

return subplotIds;
};

// order the ids
var idLen = _module.idRoot.length;
subplotIds.sort(function(a, b) {
var aNum = +(a.substr(idLen) || 1),
bNum = +(b.substr(idLen) || 1);
return aNum - bNum;
});

return subplotIds;
};
Expand Down
29 changes: 23 additions & 6 deletions test/jasmine/tests/plots_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ describe('Test Plots', function() {

describe('Plots.getSubplotIds', function() {
var getSubplotIds = Plots.getSubplotIds;
var layout;

it('returns scene ids', function() {
layout = {
scene: {},
it('returns scene ids in order', function() {
var layout = {
scene2: {},
scene: {},
scene3: {}
};

Expand All @@ -116,13 +115,32 @@ describe('Test Plots', function() {

expect(getSubplotIds(layout, 'cartesian'))
.toEqual([]);
expect(getSubplotIds(layout, 'geo'))
.toEqual([]);
expect(getSubplotIds(layout, 'no-valid-subplot-type'))
.toEqual([]);
});

it('returns geo ids in order', function() {
var layout = {
geo2: {},
geo: {},
geo3: {}
};

expect(getSubplotIds(layout, 'geo'))
.toEqual(['geo', 'geo2', 'geo3']);

expect(getSubplotIds(layout, 'cartesian'))
.toEqual([]);
expect(getSubplotIds(layout, 'gl3d'))
.toEqual([]);
expect(getSubplotIds(layout, 'no-valid-subplot-type'))
.toEqual([]);
});

it('returns cartesian ids', function() {
layout = {
var layout = {
_plots: { xy: {}, x2y2: {} }
};

Expand Down Expand Up @@ -167,7 +185,6 @@ describe('Test Plots', function() {
ids = findSubplotIds(data, 'gl3d');
expect(ids).toEqual(['scene', 'scene2']);
});

});

describe('Plots.register, getModule, and traceIs', function() {
Expand Down