Created
July 11, 2018 08:54
-
-
Save ramiroaznar/3000c714aac5a44b290b73b436601a34 to your computer and use it in GitHub Desktop.
Add legend from metadata | CARTO.js v4
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Add legend from metadata | CARTO</title> | |
<meta name="viewport" content="initial-scale=1.0"> | |
<meta charset="utf-8"> | |
<!-- Include Leaflet --> | |
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script> | |
<link href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" rel="stylesheet"> | |
<!-- Include CARTO.js --> | |
<script src="https://libs.cartocdn.com/carto.js/v4.0.16/carto.min.js"></script> | |
<!-- Include jQuery --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Open+Sans:300,400,600" rel="stylesheet"> | |
<link href="style.css" rel="stylesheet"> | |
</head> | |
<body> | |
<div id="map"> | |
<!-- legend --> | |
<div id="legend"> | |
<h1>Legend</h1> | |
<div id="legend-content"> | |
</div> | |
</div> | |
<script typ="text/javascript" src="script.js"></script> | |
</body> | |
</html> | |
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
// add Leaflet map to map container | |
const map = L.map('map').setView([30, 0], 3); | |
// add basemap | |
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png', { | |
maxZoom: 18 | |
}).addTo(map); | |
// declare CARTO client | |
const client = new carto.Client({ | |
apiKey: 'default_public', | |
username: 'cartojs-test' | |
}); | |
// configure and add layer | |
const source = new carto.source.Dataset(` | |
ne_10m_populated_places_simple | |
`); | |
const style = new carto.style.CartoCSS(` | |
#layer { | |
marker-width: 4; | |
marker-fill: ramp([featurecla], cartocolor(Bold), category); | |
marker-opacity: 0.7; | |
marker-line-color: ramp([featurecla], cartocolor(Bold), category); | |
marker-line-opacity: 0.9; | |
} | |
`); | |
const layer = new carto.layer.Layer(source, style); | |
client.addLayer(layer); | |
client.getLeafletLayer().addTo(map); | |
// configure and add category dataview | |
const categoryDataview = new carto.dataview.Category(source, 'featurecla', { | |
limit: 5, | |
operation: carto.operation.COUNT | |
}); | |
client.addDataview(categoryDataview); | |
// add bbox filter | |
const bboxFilter = new carto.filter.BoundingBoxLeaflet(map); | |
categoryDataview.addFilter(bboxFilter); | |
// add legend elements | |
const legend = $("#legend-content"); | |
layer.on('metadataChanged', renderCategoryLegend); | |
function renderCategoryLegend(metadata){ | |
console.log(metadata); | |
metadata.styles.forEach(function (styleMetadata) { | |
if (styleMetadata.getProperty() == 'marker-fill') { | |
let categories = styleMetadata.getCategories(); | |
console.log(categories) | |
for (category of categories){ | |
legend.append(`<li><div class="circle" style="background:${category.value}"></div> ${category.name}</li>`) | |
} | |
} | |
}); | |
} |
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
html, | |
body { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
position: relative; | |
} | |
#map { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
#legend { | |
position: absolute; | |
bottom: 12px; | |
left: 12px; | |
height: auto; | |
width: auto; | |
padding: 20px 24px; | |
background: white; | |
box-shadow: 0 0 16px rgba(0, 0, 0, 0.12); | |
border-radius: 4px; | |
opacity: 0.7; | |
z-index: 1; | |
display: block; | |
list-style-type: none; | |
z-index: 999; | |
} | |
#legend h1 { | |
font: 800 12px/16px 'Montserrat'; | |
text-transform: uppercase; | |
color: #2D3C43; | |
margin-bottom: 12px; | |
} | |
#legend-content{ | |
font: 800 12px/16px 'Open Sans'; | |
margin-bottom: 6px; | |
} | |
#legend-content > li { | |
margin-bottom: 6px; | |
display: flex; | |
align-items: center; | |
text-transform: uppercase; | |
} | |
#legend-content > li:last-child { | |
margin-bottom: 0; | |
} | |
.circle { | |
width: 8px; | |
height: 8px; | |
border-radius: 50%; | |
margin-right: 8px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment