|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<style> |
|
body { |
|
margin: 0; |
|
} |
|
#map { |
|
width: 100%; |
|
height: 100vh; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<div id="map"></div> |
|
|
|
<script src="https://d3js.org/d3-collection.v1.min.js"></script> |
|
<script src="https://d3js.org/d3-dispatch.v1.min.js"></script> |
|
<script src="https://d3js.org/d3-dsv.v1.min.js"></script> |
|
<script src="https://d3js.org/d3-request.v1.min.js"></script> |
|
<script src="https://d3js.org/d3-queue.v3.min.js"></script> |
|
|
|
<script src="https://unpkg.com/[email protected]/dist/swiftmap.min.js"></script> |
|
<script> |
|
|
|
// Create the map. |
|
var map = swiftmap.map("#map"); |
|
|
|
// Create a sequential color scheme. |
|
var scheme = swiftmap.schemeSequential() |
|
.from(d => +d.growth_2001_2011.replace("%", "")) |
|
.to(["#ffffe5", "#f7fcb9", "#d9f0a3", "#addd8e", "#78c679", "#41ab5d", "#238443", "#005a32"]) |
|
.breaks("q"); |
|
|
|
d3.queue() |
|
.defer(d3.json, "india_state.json") // geospatial data |
|
.defer(d3.csv, "india_state_population.csv") // tabular data |
|
.await(ready); |
|
|
|
function ready(error, geo, tab){ |
|
|
|
// Add data to the scheme. |
|
scheme.data(tab, d => d.state); |
|
|
|
// Add a polygons layer to your map and draw it. |
|
map |
|
.layerPolygons(geo, d => d.properties.ST_NM) |
|
.draw(); |
|
|
|
// Fill the layer based on your color scheme. |
|
map.layers[0].polygons |
|
.style("fill", scheme); |
|
|
|
// You can easily update the scheme to change the map's styling. |
|
setTimeout(() => { |
|
|
|
// Update the scheme... |
|
scheme |
|
.to(["#b35806", "#f1a340", "#fee0b6", "#f7f7f7", "#d8daeb", "#998ec3", "#542788"]); |
|
|
|
// ...and refill the layer with a five-second transition. |
|
map.layers[0].polygons.transition().duration(5000) |
|
.style("fill", scheme); |
|
|
|
}, 2000); |
|
|
|
// Reszing Swiftmaps is easy. |
|
window.onresize = () => map.resize(); |
|
|
|
} |
|
|
|
</script> |
|
</body> |
|
</html> |