@@ -292,6 +292,7 @@ module.exports.addElevation = function (geoJsonFeature, details, selectedDetail,
292
292
expandCallback : function ( expand ) {
293
293
expandElevationDiagram = expand ;
294
294
} ,
295
+ mappings : { } ,
295
296
selectedAttributeIdx : 0 ,
296
297
chooseSelectionCallback : detailSelected
297
298
} ;
@@ -307,7 +308,9 @@ module.exports.addElevation = function (geoJsonFeature, details, selectedDetail,
307
308
console . log ( detailIdx , detailKey ) ;
308
309
if ( detailKey === selectedDetail )
309
310
selectedDetailIdx = detailIdx ;
310
- GHFeatureCollection . push ( sliceFeatureCollection ( details [ detailKey ] , detailKey , geoJsonFeature ) )
311
+ GHFeatureCollection . push ( sliceFeatureCollection ( details [ detailKey ] , detailKey , geoJsonFeature ) ) ;
312
+ options . mappings [ detailKey ] = getColorMapping ( details [ detailKey ] ) ;
313
+
311
314
}
312
315
if ( selectedDetailIdx >= 0 )
313
316
options . selectedAttributeIdx = selectedDetailIdx ;
@@ -337,6 +340,67 @@ module.exports.addElevation = function (geoJsonFeature, details, selectedDetail,
337
340
elevationControl . addData ( GHFeatureCollection ) ;
338
341
} ;
339
342
343
+ function getColorMapping ( detail ) {
344
+ var detailInfo = analyzeDetail ( detail ) ;
345
+ if ( detailInfo . numeric === true && detailInfo . minVal !== detailInfo . maxVal ) {
346
+ // for numeric details we use a color gradient, taken from here: https://uigradients.com/#Superman
347
+ var colorMin = [ 0 , 153 , 247 ] ;
348
+ var colorMax = [ 241 , 23 , 18 ] ;
349
+ return function ( data ) {
350
+ var factor = ( data - detailInfo . minVal ) / ( detailInfo . maxVal - detailInfo . minVal ) ;
351
+ var color = [ ] ;
352
+ for ( var i = 0 ; i < 3 ; i ++ )
353
+ color . push ( colorMin [ i ] + factor * ( colorMax [ i ] - colorMin [ i ] ) ) ;
354
+ return {
355
+ 'text' : data ,
356
+ 'color' : 'rgb(' + color [ 0 ] + ', ' + color [ 1 ] + ', ' + color [ 2 ] + ')'
357
+ }
358
+ }
359
+ } else {
360
+ // for discrete encoded values we use discrete colors
361
+ var values = detail . map ( function ( d ) {
362
+ return d [ 2 ]
363
+ } ) ;
364
+ return function ( data ) {
365
+ // we choose a color-blind friendly palette from here: https://personal.sron.nl/~pault/#sec:qualitative
366
+ // see also this: https://thenode.biologists.com/data-visualization-with-flying-colors/research/
367
+ var palette = [ '#332288' , '#88ccee' , '#44aa99' , '#117733' , '#999933' , '#ddcc77' , '#cc6677' , '#882255' , '#aa4499' ] ;
368
+ var missingColor = '#dddddd' ;
369
+ var index = values . indexOf ( data ) % palette . length ;
370
+ var color = data === 'missing' || data === 'unclassified'
371
+ ? missingColor
372
+ : palette [ index ] ;
373
+ return {
374
+ 'text' : data ,
375
+ 'color' : color
376
+ }
377
+ }
378
+ }
379
+ }
380
+
381
+ function analyzeDetail ( detail ) {
382
+ // we check if all detail values are numeric
383
+ var numbers = new Set ( ) ;
384
+ var minVal , maxVal ;
385
+ var numberCount = 0 ;
386
+ for ( var i = 0 ; i < detail . length ; i ++ ) {
387
+ var val = detail [ i ] [ 2 ] ;
388
+ if ( typeof val === "number" ) {
389
+ if ( ! minVal ) minVal = val ;
390
+ if ( ! maxVal ) maxVal = val ;
391
+ numbers . add ( val ) ;
392
+ numberCount ++ ;
393
+ minVal = Math . min ( val , minVal ) ;
394
+ maxVal = Math . max ( val , maxVal ) ;
395
+ }
396
+ }
397
+ return {
398
+ numeric : numberCount === detail . length ,
399
+ minVal : minVal ,
400
+ maxVal : maxVal
401
+ }
402
+ }
403
+
340
404
function sliceFeatureCollection ( detail , detailKey , geoJsonFeature ) {
341
405
342
406
var feature = {
0 commit comments