Skip to content

Instantly share code, notes, and snippets.

@niallmackenzie
Last active October 22, 2015 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niallmackenzie/2373ddc18ad7bae1d1e5 to your computer and use it in GitHub Desktop.
Save niallmackenzie/2373ddc18ad7bae1d1e5 to your computer and use it in GitHub Desktop.
Rollover Test
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.municipality {
fill: #ccc;
}
.municipality :hover {
fill: orange;
}
.mesh {
fill: none;
stroke: #fff;
stroke-width: .5px;
stroke-linejoin: round;
}
.county-boundary {
fill: none;
stroke: #fff;
stroke-width: 2.5px;
pointer-events: none;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.mercator()
// Create a path generator
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("swedenFullKommun.json", function(error, swedenFullKommun) {
if (error) throw error;
var komms = topojson.feature(swedenFullKommun, swedenFullKommun.objects.swedenFullKommun3),
jkKom = komms.features.filter(function(d) { return d.properties.KnKod == '0665'; })[0];
// Create a unit projection
projection
.scale(1)
.translate([0, 0]);
// Compute the bounds of a feature of interest, then derive scale & translate
var b = path.bounds(jkKom),
// The first (boundary percetage) value is a great cheat to control the zoom
s = .25 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];
// Update the projection to use computed scale & translate
projection
.scale(s)
.translate(t);
svg.append("g")
.attr("class", "municipality")
.selectAll("path")
.data(topojson.feature(swedenFullKommun, swedenFullKommun.objects.swedenFullKommun3).features)
.enter().append("path")
.attr("d", path)
.append("title")
.text(function(d) { return d.properties.KnNamn; });
svg.append("path")
.datum(topojson.mesh(swedenFullKommun, swedenFullKommun.objects.swedenFullKommun3, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(swedenFullKommun, swedenFullKommun.objects.swedenFullKommun3, function(a, b) { return a.properties.LnKod !== b.properties.LnKod; }))
.attr("class", "county-boundary")
.attr("d", path);
});
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment