Skip to content

Commit 07ca162

Browse files
committed
ensure that Plots getSubplotIds return ordered arrays of ids
1 parent 9da84df commit 07ca162

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/plots/plots.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,19 @@ plots.findSubplotIds = function findSubplotIds(data, type) {
174174
return subplotIds;
175175
};
176176

177+
/**
178+
* Get the ids of the current subplots.
179+
*
180+
* @param {object} layout plotly full layout object.
181+
* @param {string} type subplot type to look for.
182+
*
183+
* @return {array} list of ordered subplot ids (strings).
184+
*
185+
*/
177186
plots.getSubplotIds = function getSubplotIds(layout, type) {
178-
if(plots.subplotsRegistry[type] === undefined) return [];
187+
var _module = plots.subplotsRegistry[type];
188+
189+
if(_module === undefined) return [];
179190

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

187-
var idRegex = plots.subplotsRegistry[type].idRegex,
198+
var idRegex = _module.idRegex,
188199
layoutKeys = Object.keys(layout),
189-
subplotIds = [],
190-
layoutKey;
200+
subplotIds = [];
191201

192202
for(var i = 0; i < layoutKeys.length; i++) {
193-
layoutKey = layoutKeys[i];
203+
var layoutKey = layoutKeys[i];
204+
194205
if(idRegex.test(layoutKey)) subplotIds.push(layoutKey);
195206
}
196207

197-
return subplotIds;
198-
};
199-
208+
// order the ids
209+
var idLen = _module.idRoot.length;
210+
subplotIds.sort(function(a, b) {
211+
var aNum = +(a.substr(idLen) || 1),
212+
bNum = +(b.substr(idLen) || 1);
213+
return aNum - bNum;
214+
});
200215

201216
return subplotIds;
202217
};

test/jasmine/tests/plots_test.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,11 @@ describe('Test Plots', function() {
102102

103103
describe('Plots.getSubplotIds', function() {
104104
var getSubplotIds = Plots.getSubplotIds;
105-
var layout;
106105

107-
it('returns scene ids', function() {
108-
layout = {
109-
scene: {},
106+
it('returns scene ids in order', function() {
107+
var layout = {
110108
scene2: {},
109+
scene: {},
111110
scene3: {}
112111
};
113112

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

117116
expect(getSubplotIds(layout, 'cartesian'))
118117
.toEqual([]);
118+
expect(getSubplotIds(layout, 'geo'))
119+
.toEqual([]);
120+
expect(getSubplotIds(layout, 'no-valid-subplot-type'))
121+
.toEqual([]);
122+
});
123+
124+
it('returns geo ids in order', function() {
125+
var layout = {
126+
geo2: {},
127+
geo: {},
128+
geo3: {}
129+
};
130+
131+
expect(getSubplotIds(layout, 'geo'))
132+
.toEqual(['geo', 'geo2', 'geo3']);
119133

134+
expect(getSubplotIds(layout, 'cartesian'))
135+
.toEqual([]);
136+
expect(getSubplotIds(layout, 'gl3d'))
137+
.toEqual([]);
120138
expect(getSubplotIds(layout, 'no-valid-subplot-type'))
121139
.toEqual([]);
122140
});
123141

124142
it('returns cartesian ids', function() {
125-
layout = {
143+
var layout = {
126144
_plots: { xy: {}, x2y2: {} }
127145
};
128146

@@ -167,7 +185,6 @@ describe('Test Plots', function() {
167185
ids = findSubplotIds(data, 'gl3d');
168186
expect(ids).toEqual(['scene', 'scene2']);
169187
});
170-
171188
});
172189

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

0 commit comments

Comments
 (0)