Created
April 6, 2018 10:43
-
-
Save rozklad/10d3b4a03b1ebf552f98bd7c3d95147f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* GetColorStops | |
*/ | |
function getColorStops(data) { | |
// Read all relevant values | |
// and filter those that are not numbers | |
const results = data.features | |
.map(element => element.properties && element.properties.total ? element.properties.total[configuration.year] : 0) | |
.filter(Number); | |
// Define limits using quantile logic | |
const limits = chroma | |
.limits(results, 'q', 20 - 1) | |
.filter((v, i, a) => a.indexOf(v) === i); | |
// Create color scale | |
const colorScale = chroma | |
.scale(['#94CED9', '#116474']) | |
.mode('lch') | |
.colors(20); | |
// Assign color to every country | |
const stops = data.features.map(element => { | |
let color = '#116474'; | |
if (element.properties.total && element.properties.total[configuration.year]) { | |
for (let i = 0; i < limits.length; i++) { | |
if (element.properties.total[configuration.year] >= limits[i]) { | |
color = colorScale[i]; | |
} | |
} | |
} | |
return [element.properties.name, color]; | |
}).filter(element => typeof element[0] !== 'undefined'); | |
// Draw color scale | |
drawColorScale(colorScale); | |
return stops; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment