-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add choropleth support #40
Changes from 1 commit
d3fcd8e
d5a5435
07bf5e5
3a5b3e6
bfd6a04
6b47dbc
79fd36a
360ef08
ac8c100
f1bcd21
9fa3853
b11c006
722d747
cb9972b
07b2ca7
8084c7c
cc147a0
29a3c36
6af2548
d80e99e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,6 @@ | ||
{% extends "choropleth.html" %} | ||
|
||
{% block choropleth %} | ||
|
||
var joinData = {{ joinData }}; | ||
|
||
{% if joinData %} | ||
var vectorColorStops = [], | ||
popUpKeys = []; | ||
|
||
// create categorical color stops for features in vector layer | ||
joinData.forEach(function(row, index) { | ||
var red = Math.max(255 - row["{{ colorProperty }}"], 0), | ||
green = Math.max(255 - row["{{ colorProperty }}"], 0), | ||
blue = Math.min(204 + row["{{ colorProperty }}"], 255); | ||
|
||
var color = "rgba(" + red + ", " + green + ", " + blue + ", 1)"; | ||
vectorColorStops.push([row["id"], color]); | ||
|
||
// for adding joined data to pop-up | ||
popUpKeys[row["{{ dataJoinProperty }}"]] = row["{{ colorProperty }}"]; | ||
}); | ||
{% endif %} | ||
{% block choropleth %} | ||
|
||
// Add vector data source | ||
map.addSource("vector-data", { | ||
|
@@ -35,7 +15,7 @@ | |
"source": "vector-data", | ||
"source-layer": "{{ vectorLayer }}", | ||
"paint": { | ||
"fill-color": generatePropertyExpression("match", "{{ vectorJoinColorProperty }}", vectorColorStops, "{{ defaultColor }}"), | ||
"fill-color": generatePropertyExpression("match", "{{ vectorJoinColorProperty }}", {{ vectorColorStops }}, "{{ defaultColor }}"), | ||
"fill-opacity": {{ opacity }} | ||
} | ||
}, "{{ belowLayer }}"); | ||
|
@@ -79,9 +59,23 @@ | |
} | ||
}, "{{belowLayer}}" ); | ||
|
||
{% endblock choropleth %} | ||
{% endblock choropleth %} | ||
|
||
{% block choropleth_popup %} | ||
|
||
// extract JSON property used for data-driven styling to add to popup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this section to create the popup join data before the layer creation in the Also, I changed the syntax for the Here's how I'd recommend doing it: {% block choropleth %}
// extract JSON property used for data-driven styling to add to popup
{% if joinData %}
let joinData = {{ joinData }};
# Create filter for layers from join data
let layerFilter = ['in', "{{ vectorJoinColorProperty }}"]
var popUpKeys = {};
joinData.forEach(function(row, index) {
popUpKeys[row["{{ dataJoinProperty }}"]] = row["{{ colorProperty }}"];
layerFilter.push(row["{{ dataJoinProperty }}"])
});
{% endif %}
// Add vector data source
map.addSource("vector-data", {
type: "vector",
url: "{{ vectorUrl }}",
});
// Add layer from the vector tile source with data-driven style
map.addLayer({
"id": "choropleth-fill",
"type": "fill",
"source": "vector-data",
"source-layer": "{{ vectorLayer }}",
"paint": {
"fill-color": {
"type": "categorical",
"property": "{{ vectorJoinColorProperty }}",
"stops": {{ vectorColorStops }},
"default": "{{ defaultColor }}"
},
"fill-opacity": {{ opacity }}
},
filter: layerFilter
}, "{{ belowLayer }}");
// Add border layer
map.addLayer({
"id": "choropleth-line",
"source": "vector-data",
"source-layer": "{{ vectorLayer }}",
"type": "line",
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
{% if lineDashArray %}
"line-dasharray": {{ lineDashArray }},
{% endif %}
"line-color": "{{ lineColor }}",
"line-width": {{ lineWidth }},
"line-opacity": {{ opacity }}
},
filter: layerFilter
}, "{{ belowLayer }}" );
// Add label layer
map.addLayer({
"id": "choropleth-label",
"source": "vector-data",
"source-layer": "{{ vectorLayer }}",
"type": "symbol",
"layout": {
{% if labelProperty %}
"text-field": "{{ labelProperty }}",
{% endif %}
"text-size" : generateInterpolateExpression('zoom', [[0,8],[22,16]] ),
"text-offset": [0,-1]
},
"paint": {
"text-halo-color": "white",
"text-halo-width": 1
},
filter: layerFilter
}, "{{belowLayer}}" );
{% endblock choropleth %} |
||
{% if joinData %} | ||
|
||
let joinData = {{ joinData }}; | ||
|
||
var popUpKeys = {}; | ||
|
||
joinData.forEach(function(row, index) { | ||
popUpKeys[row["{{ dataJoinProperty }}"]] = row["{{ colorProperty }}"]; | ||
}); | ||
|
||
{% endif %} | ||
|
||
{% block choropleth_popup %} | ||
// Show the popup on mouseover | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add in the logic to remove the popup here on mouseleave. map.on('mouseleave', 'choropleth-fill', function() {
map.getCanvas().style.cursor = '';
popup.remove();
});
// Fly to on click
map.on('click', 'choropleth-fill', function(e) {
map.flyTo({
center: e.lngLat,
zoom: map.getZoom() + 1
});
}); |
||
map.on('mousemove', 'choropleth-fill', function(e) { | ||
map.getCanvas().style.cursor = 'pointer'; | ||
|
@@ -101,4 +95,5 @@ | |
.setHTML(popup_html) | ||
.addTo(map); | ||
}); | ||
{% endblock choropleth_popup %} | ||
|
||
{% endblock choropleth_popup %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add layer filter here (see comment on how to create)