Skip to content

Commit 687a2e2

Browse files
committed
refactor calcdata filtering
more efficient, DRY, and without the hack of basePlot.attr='type'
1 parent 363180b commit 687a2e2

File tree

10 files changed

+85
-60
lines changed

10 files changed

+85
-60
lines changed

src/plots/cartesian/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var d3 = require('d3');
1313
var Lib = require('../../lib');
1414
var Plots = require('../plots');
15+
var getModuleCalcData = require('../get_calcdata').getModuleCalcData;
1516

1617
var axisIds = require('./axis_ids');
1718
var constants = require('./constants');
@@ -127,15 +128,7 @@ function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback
127128
if(_module.basePlotModule.name !== 'cartesian') continue;
128129

129130
// plot all traces of this type on this subplot at once
130-
var cdModule = [];
131-
for(var k = 0; k < cdSubplot.length; k++) {
132-
var cd = cdSubplot[k],
133-
trace = cd[0].trace;
134-
135-
if((trace._module === _module) && (trace.visible === true)) {
136-
cdModule.push(cd);
137-
}
138-
}
131+
var cdModule = getModuleCalcData(cdSubplot, _module);
139132

140133
_module.plot(gd, plotinfo, cdModule, transitionOpts, makeOnCompleteCallback);
141134
}

src/plots/geo/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var createGeo = require('./geo');
1313
var Plots = require('../../plots/plots');
14+
var getSubplotCalcData = require('../../plots/get_calcdata').getSubplotCalcData;
1415
var counterRegex = require('../../lib').counterRegex;
1516

1617
var GEO = 'geo';
@@ -44,7 +45,7 @@ exports.plot = function plotGeo(gd) {
4445

4546
for(var i = 0; i < geoIds.length; i++) {
4647
var geoId = geoIds[i];
47-
var geoCalcData = Plots.getSubplotCalcData(calcData, GEO, geoId);
48+
var geoCalcData = getSubplotCalcData(calcData, GEO, geoId);
4849
var geoLayout = fullLayout[geoId];
4950
var geo = geoLayout._subplot;
5051

src/plots/get_calcdata.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright 2012-2017, 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+
'use strict';
10+
11+
var Registry = require('../registry');
12+
13+
/**
14+
* Get calcdata traces(s) associated with a given subplot
15+
*
16+
* @param {array} calcData (as in gd.calcdata)
17+
* @param {string} type subplot type
18+
* @param {string} subplotId subplot id to look for
19+
*
20+
* @return {array} array of calcdata traces
21+
*/
22+
exports.getSubplotCalcData = function(calcData, type, subplotId) {
23+
var basePlotModule = Registry.subplotsRegistry[type];
24+
if(!basePlotModule) return [];
25+
26+
var attr = basePlotModule.attr;
27+
var subplotCalcData = [];
28+
29+
for(var i = 0; i < calcData.length; i++) {
30+
var calcTrace = calcData[i];
31+
var trace = calcTrace[0].trace;
32+
33+
if(trace[attr] === subplotId) subplotCalcData.push(calcTrace);
34+
}
35+
36+
return subplotCalcData;
37+
};
38+
39+
exports.getModuleCalcData = function(calcdata, typeOrModule) {
40+
var moduleCalcData = [];
41+
var _module = typeof typeOrModule === 'string' ? Registry.getModule(typeOrModule) : typeOrModule;
42+
if(!_module) return moduleCalcData;
43+
44+
for(var i = 0; i < calcdata.length; i++) {
45+
var cd = calcdata[i];
46+
var trace = cd[0].trace;
47+
48+
if((trace._module === _module) && (trace.visible === true)) moduleCalcData.push(cd);
49+
}
50+
51+
return moduleCalcData;
52+
};

src/plots/mapbox/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var mapboxgl = require('mapbox-gl');
1313

1414
var Lib = require('../../lib');
1515
var Plots = require('../plots');
16+
var getSubplotCalcData = require('../../plots/get_calcdata').getSubplotCalcData;
1617
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
1718

1819
var createMapbox = require('./mapbox');
@@ -58,7 +59,7 @@ exports.plot = function plotMapbox(gd) {
5859

5960
for(var i = 0; i < mapboxIds.length; i++) {
6061
var id = mapboxIds[i],
61-
subplotCalcData = Plots.getSubplotCalcData(calcData, MAPBOX, id),
62+
subplotCalcData = getSubplotCalcData(calcData, MAPBOX, id),
6263
opts = fullLayout[id],
6364
mapbox = opts._subplot;
6465

src/plots/plots.js

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

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

src/plots/ternary/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var Ternary = require('./ternary');
1313

1414
var Plots = require('../../plots/plots');
15+
var getSubplotCalcData = require('../../plots/get_calcdata').getSubplotCalcData;
1516
var counterRegex = require('../../lib').counterRegex;
1617
var TERNARY = 'ternary';
1718

@@ -36,7 +37,7 @@ exports.plot = function plotTernary(gd) {
3637

3738
for(var i = 0; i < ternaryIds.length; i++) {
3839
var ternaryId = ternaryIds[i],
39-
ternaryCalcData = Plots.getSubplotCalcData(calcData, TERNARY, ternaryId),
40+
ternaryCalcData = getSubplotCalcData(calcData, TERNARY, ternaryId),
4041
ternary = fullLayout[ternaryId]._subplot;
4142

4243
// If ternary is not instantiated, create one!

src/traces/parcoords/base_plot.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
'use strict';
1010

1111
var d3 = require('d3');
12-
var Plots = require('../../plots/plots');
12+
var getModuleCalcData = require('../../plots/get_calcdata').getModuleCalcData;
1313
var parcoordsPlot = require('./plot');
1414
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
1515

16-
exports.name = 'parcoords';
16+
var PARCOORDS = 'parcoords';
1717

18-
exports.attr = 'type';
18+
exports.name = PARCOORDS;
1919

2020
exports.plot = function(gd) {
21-
var calcData = Plots.getSubplotCalcData(gd.calcdata, 'parcoords', 'parcoords');
21+
var calcData = getModuleCalcData(gd.calcdata, PARCOORDS);
2222
if(calcData.length) parcoordsPlot(gd, calcData);
2323
};
2424

2525
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
26-
var hadParcoords = (oldFullLayout._has && oldFullLayout._has('parcoords'));
27-
var hasParcoords = (newFullLayout._has && newFullLayout._has('parcoords'));
26+
var hadParcoords = (oldFullLayout._has && oldFullLayout._has(PARCOORDS));
27+
var hasParcoords = (newFullLayout._has && newFullLayout._has(PARCOORDS));
2828

2929
if(hadParcoords && !hasParcoords) {
3030
oldFullLayout._paperdiv.selectAll('.parcoords').remove();

src/traces/sankey/base_plot.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@
99
'use strict';
1010

1111
var overrideAll = require('../../plot_api/edit_types').overrideAll;
12-
var Plots = require('../../plots/plots');
12+
var getModuleCalcData = require('../../plots/get_calcdata').getModuleCalcData;
1313
var plot = require('./plot');
1414
var fxAttrs = require('../../components/fx/layout_attributes');
1515

16-
exports.name = 'sankey';
16+
var SANKEY = 'sankey';
1717

18-
exports.attr = 'type';
18+
exports.name = SANKEY;
1919

2020
exports.baseLayoutAttrOverrides = overrideAll({
2121
hoverlabel: fxAttrs.hoverlabel
2222
}, 'plot', 'nested');
2323

2424
exports.plot = function(gd) {
25-
var calcData = Plots.getSubplotCalcData(gd.calcdata, 'sankey', 'sankey');
25+
var calcData = getModuleCalcData(gd.calcdata, SANKEY);
2626
if(calcData.length) plot(gd, calcData);
2727
};
2828

2929
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
30-
var hadPlot = (oldFullLayout._has && oldFullLayout._has('sankey'));
31-
var hasPlot = (newFullLayout._has && newFullLayout._has('sankey'));
30+
var hadPlot = (oldFullLayout._has && oldFullLayout._has(SANKEY));
31+
var hasPlot = (newFullLayout._has && newFullLayout._has(SANKEY));
3232

3333
if(hadPlot && !hasPlot) {
3434
oldFullLayout._paperdiv.selectAll('.sankey').remove();

src/traces/table/base_plot.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88

99
'use strict';
1010

11-
var Plots = require('../../plots/plots');
11+
var getModuleCalcData = require('../../plots/get_calcdata').getModuleCalcData;
1212
var tablePlot = require('./plot');
1313

14-
exports.name = 'table';
14+
var TABLE = 'table';
1515

16-
exports.attr = 'type';
16+
exports.name = TABLE;
1717

1818
exports.plot = function(gd) {
19-
var calcData = Plots.getSubplotCalcData(gd.calcdata, 'table', 'table');
19+
var calcData = getModuleCalcData(gd.calcdata, TABLE);
2020
if(calcData.length) tablePlot(gd, calcData);
2121
};
2222

2323
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
24-
var hadTable = (oldFullLayout._has && oldFullLayout._has('table'));
25-
var hasTable = (newFullLayout._has && newFullLayout._has('table'));
24+
var hadTable = (oldFullLayout._has && oldFullLayout._has(TABLE));
25+
var hasTable = (newFullLayout._has && newFullLayout._has(TABLE));
2626

2727
if(hadTable && !hasTable) {
2828
oldFullLayout._paperdiv.selectAll('.table').remove();

test/jasmine/tests/plots_test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ describe('Test Plots', function() {
596596
});
597597
});
598598

599-
describe('Plots.getSubplotCalcData', function() {
599+
describe('getSubplotCalcData', function() {
600+
var getSubplotCalcData = require('@src/plots/get_calcdata').getSubplotCalcData;
601+
600602
var trace0 = { geo: 'geo2' };
601603
var trace1 = { subplot: 'ternary10' };
602604
var trace2 = { subplot: 'ternary10' };
@@ -608,22 +610,22 @@ describe('Test Plots', function() {
608610
];
609611

610612
it('should extract calcdata traces associated with subplot (1)', function() {
611-
var out = Plots.getSubplotCalcData(cd, 'geo', 'geo2');
613+
var out = getSubplotCalcData(cd, 'geo', 'geo2');
612614
expect(out).toEqual([[{ trace: trace0 }]]);
613615
});
614616

615617
it('should extract calcdata traces associated with subplot (2)', function() {
616-
var out = Plots.getSubplotCalcData(cd, 'ternary', 'ternary10');
618+
var out = getSubplotCalcData(cd, 'ternary', 'ternary10');
617619
expect(out).toEqual([[{ trace: trace1 }], [{ trace: trace2 }]]);
618620
});
619621

620622
it('should return [] when no calcdata traces where found', function() {
621-
var out = Plots.getSubplotCalcData(cd, 'geo', 'geo');
623+
var out = getSubplotCalcData(cd, 'geo', 'geo');
622624
expect(out).toEqual([]);
623625
});
624626

625627
it('should return [] when subplot type is invalid', function() {
626-
var out = Plots.getSubplotCalcData(cd, 'non-sense', 'geo2');
628+
var out = getSubplotCalcData(cd, 'non-sense', 'geo2');
627629
expect(out).toEqual([]);
628630
});
629631
});

0 commit comments

Comments
 (0)