Skip to content

Commit a7cca2e

Browse files
committed
move getSubplotCalcdata method into Plots
1 parent 92119bb commit a7cca2e

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

src/plots/geo/index.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports.plot = function plotGeo(gd) {
4545

4646
for(var i = 0; i < geoIds.length; i++) {
4747
var geoId = geoIds[i],
48-
geoCalcData = getSubplotCalcData(calcData, geoId),
48+
geoCalcData = Plots.getSubplotCalcData(calcData, 'geo', geoId),
4949
geo = fullLayout[geoId]._subplot;
5050

5151
// If geo is not instantiated, create one!
@@ -102,16 +102,3 @@ exports.toSVG = function(gd) {
102102
.appendChild(geoFramework.node());
103103
}
104104
};
105-
106-
function getSubplotCalcData(calcData, id) {
107-
var subplotCalcData = [];
108-
109-
for(var i = 0; i < calcData.length; i++) {
110-
var calcTrace = calcData[i],
111-
trace = calcTrace[0].trace;
112-
113-
if(trace.geo === id) subplotCalcData.push(calcTrace);
114-
}
115-
116-
return subplotCalcData;
117-
}

src/plots/mapbox/index.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ exports.plot = function plotMapbox(gd) {
5656

5757
for(var i = 0; i < mapboxIds.length; i++) {
5858
var id = mapboxIds[i],
59-
subplotCalcData = getSubplotCalcData(calcData, id),
59+
subplotCalcData = Plots.getSubplotCalcData(calcData, 'mapbox', id),
6060
opts = fullLayout[id],
6161
mapbox = opts._subplot;
6262

@@ -118,19 +118,6 @@ exports.toSVG = function(gd) {
118118
}
119119
};
120120

121-
function getSubplotCalcData(calcData, id) {
122-
var subplotCalcData = [];
123-
124-
for(var i = 0; i < calcData.length; i++) {
125-
var calcTrace = calcData[i],
126-
trace = calcTrace[0].trace;
127-
128-
if(trace.subplot === id) subplotCalcData.push(calcTrace);
129-
}
130-
131-
return subplotCalcData;
132-
}
133-
134121
function findAccessToken(gd, mapboxIds) {
135122
var fullLayout = gd._fullLayout,
136123
context = gd._context;

src/plots/plots.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,31 @@ plots.getSubplotData = function getSubplotData(data, type, subplotId) {
157157
return subplotData;
158158
};
159159

160+
/**
161+
* Get calcdata traces(s) associated with a given subplot
162+
*
163+
* @param {array} calcData (as in gd.calcdata)
164+
* @param {string} type subplot type
165+
* @param {string} subplotId subplot id to look for
166+
*
167+
* @return {array} array of calcdata traces
168+
*/
169+
plots.getSubplotCalcData = function(calcData, type, subplotId) {
170+
if(plots.subplotsRegistry[type] === undefined) return [];
171+
172+
var attr = plots.subplotsRegistry[type].attr;
173+
var subplotCalcData = [];
174+
175+
for(var i = 0; i < calcData.length; i++) {
176+
var calcTrace = calcData[i],
177+
trace = calcTrace[0].trace;
178+
179+
if(trace[attr] === subplotId) subplotCalcData.push(calcTrace);
180+
}
181+
182+
return subplotCalcData;
183+
};
184+
160185
// in some cases the browser doesn't seem to know how big
161186
// the text is at first, so it needs to draw it,
162187
// then wait a little, then draw it again

test/jasmine/tests/plots_test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,37 @@ describe('Test Plots', function() {
549549
});
550550
});
551551
});
552+
553+
describe('Plots.getSubplotCalcData', function() {
554+
var trace0 = { geo: 'geo2' };
555+
var trace1 = { subplot: 'ternary10' };
556+
var trace2 = { subplot: 'ternary10' };
557+
558+
var cd = [
559+
[{ trace: trace0 }],
560+
[{ trace: trace1 }],
561+
[{ trace: trace2}]
562+
];
563+
564+
it('should extract calcdata traces associated with subplot (1)', function() {
565+
var out = Plots.getSubplotCalcData(cd, 'geo', 'geo2');
566+
expect(out).toEqual([[{ trace: trace0 }]]);
567+
});
568+
569+
it('should extract calcdata traces associated with subplot (2)', function() {
570+
var out = Plots.getSubplotCalcData(cd, 'ternary', 'ternary10');
571+
expect(out).toEqual([[{ trace: trace1 }], [{ trace: trace2 }]]);
572+
});
573+
574+
it('should return [] when no calcdata traces where found', function() {
575+
var out = Plots.getSubplotCalcData(cd, 'geo', 'geo');
576+
expect(out).toEqual([]);
577+
});
578+
579+
it('should return [] when subplot type is invalid', function() {
580+
var out = Plots.getSubplotCalcData(cd, 'non-sense', 'geo2');
581+
expect(out).toEqual([]);
582+
});
583+
});
584+
552585
});

0 commit comments

Comments
 (0)