Skip to content

Commit 6ab22f4

Browse files
easbarThomas Butz
and
Thomas Butz
authored
Color gradient for numeric path details, remove d3 (graphhopper#2259)
Co-authored-by: Thomas Butz <[email protected]>
1 parent ff030b7 commit 6ab22f4

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

web-bundle/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
}
2525
},
2626
"dependencies": {
27-
"d3": "5.11.0",
2827
"flatpickr": "4.4.6",
2928
"jquery": "3.5.0",
3029
"leaflet": "1.5.1",

web-bundle/src/main/resources/com/graphhopper/maps/js/main-template.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
global.d3 = require('d3');
21
var Flatpickr = require('flatpickr');
32
require('flatpickr/dist/l10n');
43

web-bundle/src/main/resources/com/graphhopper/maps/js/map.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ module.exports.addElevation = function (geoJsonFeature, details, selectedDetail,
292292
expandCallback: function (expand) {
293293
expandElevationDiagram = expand;
294294
},
295+
mappings: {},
295296
selectedAttributeIdx: 0,
296297
chooseSelectionCallback: detailSelected
297298
};
@@ -307,7 +308,9 @@ module.exports.addElevation = function (geoJsonFeature, details, selectedDetail,
307308
console.log(detailIdx, detailKey);
308309
if (detailKey === selectedDetail)
309310
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+
311314
}
312315
if (selectedDetailIdx >= 0)
313316
options.selectedAttributeIdx = selectedDetailIdx;
@@ -337,6 +340,67 @@ module.exports.addElevation = function (geoJsonFeature, details, selectedDetail,
337340
elevationControl.addData(GHFeatureCollection);
338341
};
339342

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+
340404
function sliceFeatureCollection(detail, detailKey, geoJsonFeature){
341405

342406
var feature = {

0 commit comments

Comments
 (0)