Skip to content

Commit a457722

Browse files
committed
allow numbers as *locations* items when using custom *geojson*
1 parent 9a602ba commit a457722

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/traces/choropleth/calc.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@ module.exports = function calc(gd, trace) {
1919
var len = trace._length;
2020
var calcTrace = new Array(len);
2121

22+
var isNonBlankString = function(v) { return v && typeof v === 'string'; };
23+
var isValidLoc;
24+
25+
if(trace.geojson) {
26+
isValidLoc = function(v) { return isNonBlankString(v) || isNumeric(v); };
27+
} else {
28+
isValidLoc = isNonBlankString;
29+
}
30+
2231
for(var i = 0; i < len; i++) {
2332
var calcPt = calcTrace[i] = {};
2433
var loc = trace.locations[i];
2534
var z = trace.z[i];
2635

27-
if(typeof loc === 'string' && isNumeric(z)) {
36+
if(isValidLoc(loc) && isNumeric(z)) {
2837
calcPt.loc = loc;
2938
calcPt.z = z;
3039
} else {

src/traces/choroplethmapbox/defaults.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
1717
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
1818
}
1919

20-
// TODO locations should now allow numbers !!
21-
// update that for `choropleth` too !!
22-
2320
var locations = coerce('locations');
2421
var z = coerce('z');
2522
var geojson = coerce('geojson');
2623

27-
if(!locations || !locations.length ||
24+
if(!Lib.isArrayOrTypedArray(locations) || !locations.length ||
2825
!Lib.isArrayOrTypedArray(z) || !z.length ||
2926
!((typeof geojson === 'string' && geojson !== '') || Lib.isPlainObject(geojson))
3027
) {

test/jasmine/tests/choroplethmapbox_test.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ describe('Test choroplethmapbox defaults:', function() {
7171
]);
7272
expectVisibleFalse();
7373
});
74-
75-
it('should accept typed arrays', function() {
76-
77-
});
7874
});
7975

8076
describe('Test choroplethmapbox convert:', function() {
@@ -219,6 +215,48 @@ describe('Test choroplethmapbox convert:', function() {
219215
expect(Lib.log).toHaveBeenCalledWith('Location with id d does not have a matching feature');
220216
});
221217

218+
describe('should accept numbers as *locations* items', function() {
219+
function _assert(act) {
220+
expect(act.fill.layout.visibility).toBe('visible', 'fill layer visibility');
221+
expect(act.line.layout.visibility).toBe('visible', 'line layer visibility');
222+
expect(act.geojson.features.length).toBe(3, '# of visible features');
223+
expect(extract(act, 'fc'))
224+
.toEqual(['rgb(178, 10, 28)', 'rgb(220, 220, 220)', 'rgb(240, 149, 99)']);
225+
}
226+
227+
it('- regular array case', function() {
228+
var trace = {
229+
locations: [1, 2, 3],
230+
z: [20, 10, 2],
231+
geojson: {
232+
type: 'FeatureCollection',
233+
features: [
234+
{type: 'Feature', id: '1', geometry: {type: 'Polygon', coordinates: []}},
235+
{type: 'Feature', id: '3', geometry: {type: 'Polygon', coordinates: []}},
236+
{type: 'Feature', id: '2', geometry: {type: 'Polygon', coordinates: []}}
237+
]
238+
}
239+
};
240+
_assert(_convert(trace));
241+
});
242+
243+
it('- typed array case', function() {
244+
var trace = {
245+
locations: new Float32Array([1, 2, 3]),
246+
z: new Float32Array([20, 10, 2]),
247+
geojson: {
248+
type: 'FeatureCollection',
249+
features: [
250+
{type: 'Feature', id: 1, geometry: {type: 'Polygon', coordinates: []}},
251+
{type: 'Feature', id: 3, geometry: {type: 'Polygon', coordinates: []}},
252+
{type: 'Feature', id: 2, geometry: {type: 'Polygon', coordinates: []}}
253+
]
254+
}
255+
};
256+
_assert(_convert(trace));
257+
});
258+
});
259+
222260
it('should handle *Feature* on 1-item *FeatureCollection* the same way', function() {
223261
var locations = ['a'];
224262
var z = [1];

0 commit comments

Comments
 (0)