Skip to content

Add clustering to scattermapbox #5334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/traces/scattermapbox/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var colorScaleAttrs = require('../../components/colorscale/attributes');

var extendFlat = require('../../lib/extend').extendFlat;
var overrideAll = require('../../plot_api/edit_types').overrideAll;
var mapboxLayoutAtributes = require('../../plots/mapbox/layout_attributes');

var lineAttrs = scatterGeoAttrs.line;
var markerAttrs = scatterGeoAttrs.marker;
Expand All @@ -26,6 +27,52 @@ module.exports = overrideAll({
lon: scatterGeoAttrs.lon,
lat: scatterGeoAttrs.lat,

cluster: {
enabled: {
valType: 'boolean',
role: 'info',
dflt: false,
description: 'Determines whether clustering is enabled or disabled.'
},
maxzoom: extendFlat({}, mapboxLayoutAtributes.layers.maxzoom, {
description: [
'Sets the maximum zoom level.',
'At zoom levels equal to or greater than the maxzoom, the layer will be hidden.'
].join(' ')
}),
step: {
role: 'info',
valType: 'number',
arrayOk: true,
dflt: -1,
min: -1,
description: [
'Sets the steps for each cluster.'
].join(' ')
},
size: {
role: 'info',
valType: 'number',
arrayOk: true,
dflt: 20,
min: 0,
description: [
'Sets the size for each cluster step.'
].join(' ')
},
color: {
valType: 'color',
arrayOk: true,
role: 'style',
description: [
'Sets the color for each cluster step.'
].join(' ')
},
opacity: extendFlat({}, markerAttrs.opacity, {
dflt: 1
})
},

// locations
// locationmode

Expand Down
56 changes: 50 additions & 6 deletions src/traces/scattermapbox/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ module.exports = function convert(gd, calcTrace) {
var hasText = subTypes.hasText(trace);
var hasCircles = (hasMarkers && trace.marker.symbol === 'circle');
var hasSymbols = (hasMarkers && trace.marker.symbol !== 'circle');
var hasCluster = trace.cluster.enabled;

var fill = initContainer();
var line = initContainer();
var circle = initContainer();
var symbol = initContainer();
var fill = initContainer('fill');
var line = initContainer('line');
var circle = initContainer('circle');
var symbol = initContainer('symbol');

var opts = {
fill: fill,
Expand Down Expand Up @@ -82,6 +83,29 @@ module.exports = function convert(gd, calcTrace) {
var circleOpts = makeCircleOpts(calcTrace);
circle.geojson = circleOpts.geojson;
circle.layout.visibility = 'visible';
if(hasCluster) {
circle.filter = ['!', ['has', 'point_count']];
opts.cluster = {
type: 'circle',
filter: ['has', 'point_count'],
layout: {visibility: 'visible'},
paint: {
'circle-color': arrayifyAttribute(trace.cluster.color, trace.cluster.step),
'circle-radius': arrayifyAttribute(trace.cluster.size, trace.cluster.step),
'circle-opacity': arrayifyAttribute(trace.cluster.opacity, trace.cluster.step),
},
};
opts.clusterCount = {
type: 'symbol',
filter: ['has', 'point_count'],
paint: {},
layout: {
'text-field': '{point_count_abbreviated}',
'text-font': ['Open Sans Regular', 'Arial Unicode MS Regular'],
'text-size': 12
}
};
}

Lib.extendFlat(circle.paint, {
'circle-color': circleOpts.mcc,
Expand All @@ -90,6 +114,10 @@ module.exports = function convert(gd, calcTrace) {
});
}

if(hasCircles && hasCluster) {
circle.filter = ['!', ['has', 'point_count']];
}

if(hasSymbols || hasText) {
symbol.geojson = makeSymbolGeoJSON(calcTrace, gd);

Expand Down Expand Up @@ -150,10 +178,12 @@ module.exports = function convert(gd, calcTrace) {
return opts;
};

function initContainer() {
function initContainer(type) {
return {
type: type,
geojson: geoJsonUtils.makeBlank(),
layout: { visibility: 'none' },
filter: null,
paint: {}
};
}
Expand Down Expand Up @@ -208,7 +238,8 @@ function makeCircleOpts(calcTrace) {

features.push({
type: 'Feature',
geometry: {type: 'Point', coordinates: lonlat},
id: i + 1,
geometry: { type: 'Point', coordinates: lonlat },
properties: props
});
}
Expand Down Expand Up @@ -331,3 +362,16 @@ function blankFillFunc() { return ''; }
function isBADNUM(lonlat) {
return lonlat[0] === BADNUM;
}

function arrayifyAttribute(attribute, step) {
var newAttribute;
if(Lib.isArrayOrTypedArray(attribute) && Lib.isArrayOrTypedArray(step)) {
newAttribute = ['step', ['get', 'point_count'], attribute[0]];
for(var idx = 1; idx < attribute.length; idx++) {
newAttribute.push(step[idx - 1], attribute[idx]);
}
} else {
newAttribute = attribute;
}
return newAttribute;
}
9 changes: 9 additions & 0 deletions src/traces/scattermapbox/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
}
}

var clusterEnabled = coerce('cluster.enabled');
if(clusterEnabled) {
coerce('cluster.maxzoom');
coerce('cluster.step');
coerce('cluster.color', traceOut.marker.color || defaultColor);
coerce('cluster.size');
coerce('cluster.opacity');
}

if(subTypes.hasText(traceOut)) {
handleTextDefaults(traceIn, traceOut, layout, coerce, {noSelect: true});
}
Expand Down
9 changes: 9 additions & 0 deletions src/traces/scattermapbox/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ var Lib = require('../../lib');
var getTraceColor = require('../scatter/get_trace_color');
var fillText = Lib.fillText;
var BADNUM = require('../../constants/numerical').BADNUM;
var LAYER_PREFIX = require('../../plots/mapbox/constants').traceLayerPrefix;

module.exports = function hoverPoints(pointData, xval, yval) {
var cd = pointData.cd;
var trace = cd[0].trace;
var xa = pointData.xa;
var ya = pointData.ya;
var subplot = pointData.subplot;
var clusteredPointsIds = [];
var layer = LAYER_PREFIX + trace.uid + '-circle';

if(trace.cluster.enabled) {
var elems = subplot.map.queryRenderedFeatures(null, {layers: [layer]});
clusteredPointsIds = elems.map(function(elem) {return elem.id;});
}

// compute winding number about [-180, 180] globe
var winding = (xval >= 0) ?
Expand All @@ -34,6 +42,7 @@ module.exports = function hoverPoints(pointData, xval, yval) {
function distFn(d) {
var lonlat = d.lonlat;
if(lonlat[0] === BADNUM) return Infinity;
if(trace.cluster.enabled && clusteredPointsIds.indexOf(d.i + 1) === -1) return Infinity;

var lon = Lib.modHalf(lonlat[0], 360);
var lat = lonlat[1];
Expand Down
Loading