@@ -26,11 +26,12 @@ module.exports = function convert(gd, calcTrace) {
2626 var hasText = subTypes . hasText ( trace ) ;
2727 var hasCircles = ( hasMarkers && trace . marker . symbol === 'circle' ) ;
2828 var hasSymbols = ( hasMarkers && trace . marker . symbol !== 'circle' ) ;
29+ var hasCluster = trace . cluster && trace . cluster . enabled ;
2930
30- var fill = initContainer ( ) ;
31- var line = initContainer ( ) ;
32- var circle = initContainer ( ) ;
33- var symbol = initContainer ( ) ;
31+ var fill = initContainer ( 'fill' ) ;
32+ var line = initContainer ( 'line' ) ;
33+ var circle = initContainer ( 'circle' ) ;
34+ var symbol = initContainer ( 'symbol' ) ;
3435
3536 var opts = {
3637 fill : fill ,
@@ -74,6 +75,29 @@ module.exports = function convert(gd, calcTrace) {
7475 var circleOpts = makeCircleOpts ( calcTrace ) ;
7576 circle . geojson = circleOpts . geojson ;
7677 circle . layout . visibility = 'visible' ;
78+ if ( hasCluster ) {
79+ circle . filter = [ '!' , [ 'has' , 'point_count' ] ] ;
80+ opts . cluster = {
81+ type : 'circle' ,
82+ filter : [ 'has' , 'point_count' ] ,
83+ layout : { visibility : 'visible' } ,
84+ paint : {
85+ 'circle-color' : arrayifyAttribute ( trace . cluster . color , trace . cluster . step ) ,
86+ 'circle-radius' : arrayifyAttribute ( trace . cluster . size , trace . cluster . step ) ,
87+ 'circle-opacity' : arrayifyAttribute ( trace . cluster . opacity , trace . cluster . step ) ,
88+ } ,
89+ } ;
90+ opts . clusterCount = {
91+ type : 'symbol' ,
92+ filter : [ 'has' , 'point_count' ] ,
93+ paint : { } ,
94+ layout : {
95+ 'text-field' : '{point_count_abbreviated}' ,
96+ 'text-font' : [ 'Open Sans Regular' , 'Arial Unicode MS Regular' ] ,
97+ 'text-size' : 12
98+ }
99+ } ;
100+ }
77101
78102 Lib . extendFlat ( circle . paint , {
79103 'circle-color' : circleOpts . mcc ,
@@ -82,6 +106,10 @@ module.exports = function convert(gd, calcTrace) {
82106 } ) ;
83107 }
84108
109+ if ( hasCircles && hasCluster ) {
110+ circle . filter = [ '!' , [ 'has' , 'point_count' ] ] ;
111+ }
112+
85113 if ( hasSymbols || hasText ) {
86114 symbol . geojson = makeSymbolGeoJSON ( calcTrace , gd ) ;
87115
@@ -142,10 +170,12 @@ module.exports = function convert(gd, calcTrace) {
142170 return opts ;
143171} ;
144172
145- function initContainer ( ) {
173+ function initContainer ( type ) {
146174 return {
175+ type : type ,
147176 geojson : geoJsonUtils . makeBlank ( ) ,
148177 layout : { visibility : 'none' } ,
178+ filter : null ,
149179 paint : { }
150180 } ;
151181}
@@ -200,7 +230,8 @@ function makeCircleOpts(calcTrace) {
200230
201231 features . push ( {
202232 type : 'Feature' ,
203- geometry : { type : 'Point' , coordinates : lonlat } ,
233+ id : i + 1 ,
234+ geometry : { type : 'Point' , coordinates : lonlat } ,
204235 properties : props
205236 } ) ;
206237 }
@@ -323,3 +354,17 @@ function blankFillFunc() { return ''; }
323354function isBADNUM ( lonlat ) {
324355 return lonlat [ 0 ] === BADNUM ;
325356}
357+
358+ function arrayifyAttribute ( values , step ) {
359+ var newAttribute ;
360+ if ( Lib . isArrayOrTypedArray ( values ) && Lib . isArrayOrTypedArray ( step ) ) {
361+ newAttribute = [ 'step' , [ 'get' , 'point_count' ] , values [ 0 ] ] ;
362+
363+ for ( var idx = 1 ; idx < values . length ; idx ++ ) {
364+ newAttribute . push ( step [ idx - 1 ] , values [ idx ] ) ;
365+ }
366+ } else {
367+ newAttribute = values ;
368+ }
369+ return newAttribute ;
370+ }
0 commit comments