Skip to content

Commit 8bd4c97

Browse files
authored
Merge pull request #1291 from plotly/geo-ternary-visible-false-1st-trace
Combine geo and ternary genaral update pattern
2 parents 3b85f7f + 83c2722 commit 8bd4c97

File tree

8 files changed

+277
-157
lines changed

8 files changed

+277
-157
lines changed

src/plots/geo/geo.js

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ var d3 = require('d3');
1515

1616
var Color = require('../../components/color');
1717
var Drawing = require('../../components/drawing');
18-
var Axes = require('../../plots/cartesian/axes');
19-
var Fx = require('../../plots/cartesian/graph_interact');
18+
var Plots = require('../plots');
19+
var Axes = require('../cartesian/axes');
20+
var Fx = require('../cartesian/graph_interact');
2021

2122
var addProjectionsToD3 = require('./projections');
2223
var createGeoScale = require('./set_scale');
@@ -170,66 +171,13 @@ proto.plot = function(geoCalcData, fullLayout, promises) {
170171
};
171172

172173
proto.onceTopojsonIsLoaded = function(geoCalcData, geoLayout) {
173-
var i;
174-
175174
this.drawLayout(geoLayout);
176175

177-
var traceHashOld = this.traceHash;
178-
var traceHash = {};
179-
180-
for(i = 0; i < geoCalcData.length; i++) {
181-
var calcData = geoCalcData[i],
182-
trace = calcData[0].trace;
183-
184-
traceHash[trace.type] = traceHash[trace.type] || [];
185-
traceHash[trace.type].push(calcData);
186-
}
187-
188-
var moduleNamesOld = Object.keys(traceHashOld);
189-
var moduleNames = Object.keys(traceHash);
190-
191-
// when a trace gets deleted, make sure that its module's
192-
// plot method is called so that it is properly
193-
// removed from the DOM.
194-
for(i = 0; i < moduleNamesOld.length; i++) {
195-
var moduleName = moduleNamesOld[i];
196-
197-
if(moduleNames.indexOf(moduleName) === -1) {
198-
var fakeCalcTrace = traceHashOld[moduleName][0],
199-
fakeTrace = fakeCalcTrace[0].trace;
200-
201-
fakeTrace.visible = false;
202-
traceHash[moduleName] = [fakeCalcTrace];
203-
}
204-
}
205-
206-
moduleNames = Object.keys(traceHash);
207-
208-
for(i = 0; i < moduleNames.length; i++) {
209-
var moduleCalcData = traceHash[moduleNames[i]],
210-
_module = moduleCalcData[0][0].trace._module;
211-
212-
_module.plot(this, filterVisible(moduleCalcData), geoLayout);
213-
}
214-
215-
this.traceHash = traceHash;
176+
Plots.generalUpdatePerTraceModule(this, geoCalcData, geoLayout);
216177

217178
this.render();
218179
};
219180

220-
function filterVisible(calcDataIn) {
221-
var calcDataOut = [];
222-
223-
for(var i = 0; i < calcDataIn.length; i++) {
224-
var calcTrace = calcDataIn[i],
225-
trace = calcTrace[0].trace;
226-
227-
if(trace.visible === true) calcDataOut.push(calcTrace);
228-
}
229-
230-
return calcDataOut;
231-
}
232-
233181
proto.updateFx = function(hovermode) {
234182
this.showHover = (hovermode !== false);
235183

src/plots/geo/index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ 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!
52-
if(geo === undefined) {
52+
if(!geo) {
5353
geo = new Geo({
5454
id: geoId,
5555
graphDiv: gd,
@@ -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: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ plots.hasSimpleAPICommandBindings = commandModule.hasSimpleAPICommandBindings;
6363
plots.findSubplotIds = function findSubplotIds(data, type) {
6464
var subplotIds = [];
6565

66-
if(plots.subplotsRegistry[type] === undefined) return subplotIds;
66+
if(!plots.subplotsRegistry[type]) return subplotIds;
6767

6868
var attr = plots.subplotsRegistry[type].attr;
6969

@@ -90,7 +90,7 @@ plots.findSubplotIds = function findSubplotIds(data, type) {
9090
plots.getSubplotIds = function getSubplotIds(layout, type) {
9191
var _module = plots.subplotsRegistry[type];
9292

93-
if(_module === undefined) return [];
93+
if(!_module) return [];
9494

9595
// layout must be 'fullLayout' here
9696
if(type === 'cartesian' && (!layout._has || !layout._has('cartesian'))) return [];
@@ -124,14 +124,14 @@ plots.getSubplotIds = function getSubplotIds(layout, type) {
124124
* Get the data trace(s) associated with a given subplot.
125125
*
126126
* @param {array} data plotly full data array.
127-
* @param {object} layout plotly full layout object.
128-
* @param {string} subplotId subplot ids to look for.
127+
* @param {string} type subplot type to look for.
128+
* @param {string} subplotId subplot id to look for.
129129
*
130130
* @return {array} list of trace objects.
131131
*
132132
*/
133133
plots.getSubplotData = function getSubplotData(data, type, subplotId) {
134-
if(plots.subplotsRegistry[type] === undefined) return [];
134+
if(!plots.subplotsRegistry[type]) return [];
135135

136136
var attr = plots.subplotsRegistry[type].attr,
137137
subplotData = [],
@@ -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]) 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
@@ -2027,3 +2052,67 @@ plots.doCalcdata = function(gd, traces) {
20272052
calcdata[i] = cd;
20282053
}
20292054
};
2055+
2056+
plots.generalUpdatePerTraceModule = function(subplot, subplotCalcData, subplotLayout) {
2057+
var traceHashOld = subplot.traceHash,
2058+
traceHash = {},
2059+
i;
2060+
2061+
function filterVisible(calcDataIn) {
2062+
var calcDataOut = [];
2063+
2064+
for(var i = 0; i < calcDataIn.length; i++) {
2065+
var calcTrace = calcDataIn[i],
2066+
trace = calcTrace[0].trace;
2067+
2068+
if(trace.visible === true) calcDataOut.push(calcTrace);
2069+
}
2070+
2071+
return calcDataOut;
2072+
}
2073+
2074+
// build up moduleName -> calcData hash
2075+
for(i = 0; i < subplotCalcData.length; i++) {
2076+
var calcTraces = subplotCalcData[i],
2077+
trace = calcTraces[0].trace;
2078+
2079+
// skip over visible === false traces
2080+
// as they don't have `_module` ref
2081+
if(trace.visible) {
2082+
traceHash[trace.type] = traceHash[trace.type] || [];
2083+
traceHash[trace.type].push(calcTraces);
2084+
}
2085+
}
2086+
2087+
var moduleNamesOld = Object.keys(traceHashOld);
2088+
var moduleNames = Object.keys(traceHash);
2089+
2090+
// when a trace gets deleted, make sure that its module's
2091+
// plot method is called so that it is properly
2092+
// removed from the DOM.
2093+
for(i = 0; i < moduleNamesOld.length; i++) {
2094+
var moduleName = moduleNamesOld[i];
2095+
2096+
if(moduleNames.indexOf(moduleName) === -1) {
2097+
var fakeCalcTrace = traceHashOld[moduleName][0],
2098+
fakeTrace = fakeCalcTrace[0].trace;
2099+
2100+
fakeTrace.visible = false;
2101+
traceHash[moduleName] = [fakeCalcTrace];
2102+
}
2103+
}
2104+
2105+
// update list of module names to include 'fake' traces added above
2106+
moduleNames = Object.keys(traceHash);
2107+
2108+
// call module plot method
2109+
for(i = 0; i < moduleNames.length; i++) {
2110+
var moduleCalcData = traceHash[moduleNames[i]],
2111+
_module = moduleCalcData[0][0].trace._module;
2112+
2113+
_module.plot(subplot, filterVisible(moduleCalcData), subplotLayout);
2114+
}
2115+
2116+
// update moduleName -> calcData hash
2117+
subplot.traceHash = traceHash;
2118+
};

src/plots/ternary/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ exports.supplyLayoutDefaults = require('./layout/defaults');
3232

3333
exports.plot = function plotTernary(gd) {
3434
var fullLayout = gd._fullLayout,
35-
fullData = gd._fullData,
35+
calcData = gd.calcdata,
3636
ternaryIds = Plots.getSubplotIds(fullLayout, 'ternary');
3737

3838
for(var i = 0; i < ternaryIds.length; i++) {
3939
var ternaryId = ternaryIds[i],
40-
fullTernaryData = Plots.getSubplotData(fullData, 'ternary', ternaryId),
40+
ternaryCalcData = Plots.getSubplotCalcData(calcData, 'ternary', ternaryId),
4141
ternary = fullLayout[ternaryId]._subplot;
4242

4343
// If ternary is not instantiated, create one!
44-
if(ternary === undefined) {
44+
if(!ternary) {
4545
ternary = new Ternary({
4646
id: ternaryId,
4747
graphDiv: gd,
@@ -53,7 +53,7 @@ exports.plot = function plotTernary(gd) {
5353
fullLayout[ternaryId]._subplot = ternary;
5454
}
5555

56-
ternary.plot(fullTernaryData, fullLayout, gd._promises);
56+
ternary.plot(ternaryCalcData, fullLayout, gd._promises);
5757
}
5858
};
5959

src/plots/ternary/ternary.js

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var Color = require('../../components/color');
1818
var Drawing = require('../../components/drawing');
1919
var setConvert = require('../cartesian/set_convert');
2020
var extendFlat = require('../../lib/extend').extendFlat;
21+
var Plots = require('../plots');
2122
var Axes = require('../cartesian/axes');
2223
var dragElement = require('../../components/dragelement');
2324
var Titles = require('../../components/titles');
@@ -44,59 +45,14 @@ proto.init = function(fullLayout) {
4445
this.traceHash = {};
4546
};
4647

47-
proto.plot = function(ternaryData, fullLayout) {
48+
proto.plot = function(ternaryCalcData, fullLayout) {
4849
var _this = this,
4950
ternaryLayout = fullLayout[_this.id],
50-
graphSize = fullLayout._size,
51-
i;
52-
53-
if(Lib.getPlotDiv(_this.plotContainer.node()) !== _this.graphDiv) {
54-
// someone deleted the framework - remake it
55-
// TODO: this is getting deleted in (cartesian) makePlotFramework
56-
// turn that into idiomatic d3 (enter/exit, the piece I didn't know
57-
// before was ordering selections) so we don't need this.
58-
_this.init(_this.graphDiv._fullLayout);
59-
_this.makeFramework();
60-
}
51+
graphSize = fullLayout._size;
6152

6253
_this.adjustLayout(ternaryLayout, graphSize);
6354

64-
var traceHashOld = _this.traceHash;
65-
var traceHash = {};
66-
67-
for(i = 0; i < ternaryData.length; i++) {
68-
var trace = ternaryData[i];
69-
70-
traceHash[trace.type] = traceHash[trace.type] || [];
71-
traceHash[trace.type].push(trace);
72-
}
73-
74-
var moduleNamesOld = Object.keys(traceHashOld);
75-
var moduleNames = Object.keys(traceHash);
76-
77-
// when a trace gets deleted, make sure that its module's
78-
// plot method is called so that it is properly
79-
// removed from the DOM.
80-
for(i = 0; i < moduleNamesOld.length; i++) {
81-
var moduleName = moduleNamesOld[i];
82-
83-
if(moduleNames.indexOf(moduleName) === -1) {
84-
var fakeModule = traceHashOld[moduleName][0];
85-
fakeModule.visible = false;
86-
traceHash[moduleName] = [fakeModule];
87-
}
88-
}
89-
90-
moduleNames = Object.keys(traceHash);
91-
92-
for(i = 0; i < moduleNames.length; i++) {
93-
var moduleData = traceHash[moduleNames[i]];
94-
var _module = moduleData[0]._module;
95-
96-
_module.plot(_this, Lib.filterVisible(moduleData), ternaryLayout);
97-
}
98-
99-
_this.traceHash = traceHash;
55+
Plots.generalUpdatePerTraceModule(_this, ternaryCalcData, ternaryLayout);
10056

10157
_this.layers.plotbg.select('path').call(Color.fill, ternaryLayout.bgcolor);
10258
};

0 commit comments

Comments
 (0)