Commit ca26ff5f authored by Rodrigo Tapia-McClung's avatar Rodrigo Tapia-McClung

Add legend to map

parent 3a51ff91
...@@ -24,13 +24,15 @@ let timeParse, ...@@ -24,13 +24,15 @@ let timeParse,
glmap, glmap,
osmLayer, cartoLightLayer, cartoDarkLayer, osmLayer, cartoLightLayer, cartoDarkLayer,
timeLayer, timeLayer,
layerControl; layerControl,
scale;
// define empty objects and indicators // define empty objects and indicators
let maxIndicators = {}, let maxIndicators = {},
minIndicators = {}, minIndicators = {},
indicators = ["area", "perimeter", "costa", "df"], indicators = ["area", "perimeter", "costa", "df"],
indicatorsNames = ["Área", "Perímetro", "Desarrollo de la línea de costa", "Dimensión fractal"]; indicatorsNames = ["Área", "Perímetro", "Desarrollo de la línea de costa", "Dimensión fractal"],
indicatorsUnits = ["m\u00B2", "m", "", ""];
let cols = []; let cols = [];
indicators.forEach( (indicator) => { indicators.forEach( (indicator) => {
...@@ -359,7 +361,8 @@ const updateMap = (mapData) => { ...@@ -359,7 +361,8 @@ const updateMap = (mapData) => {
let option = $("#indicatorSelect").val(), // option selected from dropdrown let option = $("#indicatorSelect").val(), // option selected from dropdrown
min = values.minIndicators, min = values.minIndicators,
max = values.maxIndicators; max = values.maxIndicators;
styleTiles(option, min, max); styleTiles(option, min, max)
.then(legend.addTo(map)); // add legend control -> it updates
//TODO: update charts //TODO: update charts
}); });
...@@ -422,7 +425,8 @@ const populateMap = (mapData) => { ...@@ -422,7 +425,8 @@ const populateMap = (mapData) => {
// style currentTiles // style currentTiles
let option = $("#indicatorSelect").val(); // option selected from dropdrown let option = $("#indicatorSelect").val(); // option selected from dropdrown
styleTiles(option, minIndicators, maxIndicators); styleTiles(option, minIndicators, maxIndicators)
.then(legend.addTo(map)); // add legend control -> it updates
let baseLayers = { let baseLayers = {
"Carto Dark": cartoDarkLayer, "Carto Dark": cartoDarkLayer,
...@@ -596,8 +600,8 @@ L.timeDimension.layer.Tile = (layer, options) => { ...@@ -596,8 +600,8 @@ L.timeDimension.layer.Tile = (layer, options) => {
$("#indicatorSelect").on("change", function() { $("#indicatorSelect").on("change", function() {
// style currentTiles // style currentTiles
let option = this.value; // option selected from dropdrown let option = this.value; // option selected from dropdrown
styleTiles(option, minIndicators, maxIndicators); styleTiles(option, minIndicators, maxIndicators)
// .then(legend.addTo(map)); // add legend control -> it updates .then(legend.addTo(map)); // add legend control -> it updates
// FIXME: re-adding control updates its contents... why? // FIXME: re-adding control updates its contents... why?
// Highlight plot title according to selected option // Highlight plot title according to selected option
//indicators.forEach( indicator => { //indicators.forEach( indicator => {
...@@ -609,7 +613,7 @@ const styleTiles = (option, minIndicators, maxIndicators) => { ...@@ -609,7 +613,7 @@ const styleTiles = (option, minIndicators, maxIndicators) => {
// define color scale domain based on min-max values for selected indicator // define color scale domain based on min-max values for selected indicator
let domain = [minIndicators[option], maxIndicators[option]]; let domain = [minIndicators[option], maxIndicators[option]];
//console.log(domain) //console.log(domain)
let scale = chroma.scale("PuBu").padding([0.5, 0]).domain(domain).classes(5); scale = chroma.scale("PuBu").padding([0.5, 0]).domain(domain).classes(5);
Object.keys(currentTiles).forEach(layer => { Object.keys(currentTiles).forEach(layer => {
let color = [ let color = [
'interpolate', 'interpolate',
...@@ -623,5 +627,27 @@ const styleTiles = (option, minIndicators, maxIndicators) => { ...@@ -623,5 +627,27 @@ const styleTiles = (option, minIndicators, maxIndicators) => {
return Promise.resolve(scale); return Promise.resolve(scale);
} }
// TODO: add layer control let legend = L.control({
position: "bottomright"
});
legend.onAdd = () => {
let div = L.DomUtil.create("div", "info legend leaflet-bar");
let option = $("#indicatorSelect").val();
let optionIndex = indicators.indexOf(option);
let legendText = indicatorsUnits[optionIndex] == "" ? indicatorsNames[optionIndex] :
indicatorsNames[optionIndex] + " (" + indicatorsUnits[optionIndex] + ")";
var html = "<h6>" + legendText + "</h6><ul>";
let classes = scale.classes();
classes.forEach( (c, idx, array) => {
if (idx != array.length - 1) {
html += "<li><i style=\"background:" + scale(c).hex() + "\"></i> " +
" " + d3.format(",.4~s")(classes[idx]) + " - " + d3.format(",.4~s")(classes[idx+1]) + "</li>";
}
});
html += "</ul>";
div.innerHTML = html;
return div;
};
// TODO: add charts // TODO: add charts
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment