Skip to content

Instantly share code, notes, and snippets.

@jgaffuri
Last active December 24, 2019 09:54
Show Gist options
  • Save jgaffuri/e3adbfd8fa2a1ba5096117834af399ad to your computer and use it in GitHub Desktop.
Save jgaffuri/e3adbfd8fa2a1ba5096117834af399ad to your computer and use it in GitHub Desktop.
Coastal margin with CSS stroke-width
license: EUPL-1.2
height: 800
scrolling: no
border: no
redirect: https://observablehq.com/@jgaffuri/coastal-margin-with-css-stroke-width

This is an example of Nuts2json API showing European Union countries with D3.js with a coastal margin.

<!DOCTYPE html>
<svg></svg>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-request.v1.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
<script src="https://d3js.org/d3-color.v1.min.js"></script>
<script src="https://d3js.org/d3-interpolate.v1.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script>
d3.json("https://raw.githubusercontent.com/eurostat/Nuts2json/master/2016/3035/20M/0.json",
function (error, nuts) {
if (error) throw error;
//prepare SVG element
var width = 900, height = width * (nuts.bbox[3] - nuts.bbox[1]) / (nuts.bbox[2] - nuts.bbox[0]),
svg = d3.select("svg").attr("width", width).attr("height", height),
path = d3.geoPath().projection(d3.geoIdentity()
.reflectY(true).fitSize([width, height], topojson.feature(nuts, nuts.objects.gra)));
//draw coastal margin
for (var i = 1; i >= 0; i -= 0.1)
svg.append("g").selectAll("path").data(topojson.feature(nuts, nuts.objects.nutsbn).features)
.enter().append("path")/*.filter(function(bn){ return bn.properties.co === 'T'; })*/.attr("d", path)
.attr("class", "cmarg")
.style("stroke-width", (i * 140) + "px")
.style("stroke", d3.interpolateBlues(1-Math.sqrt(i)));
//draw regions
svg.append("g").selectAll("path").data(topojson.feature(nuts, nuts.objects.nutsrg).features)
.enter().append("path").attr("d", path)
.attr("class", "rg");
//draw boundaries
svg.append("g").selectAll("path").data(topojson.feature(nuts, nuts.objects.nutsbn).features)
.enter().append("path").filter(function (bn) { return bn.properties.co === 'F'; }).attr("d", path)
.attr("class", "bn");
});
</script>
.cmarg {
fill: none;
stroke-linecap: round;
stroke-linejoin: round;
}
.rg {
fill: #e9e9e9;
}
.rg:hover {
fill: #ff7f00;
}
.bn {
fill: none;
stroke-width: 1px;
stroke: rgb(75, 75, 75);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment