Skip to content

Instantly share code, notes, and snippets.

@michaeljblum
Last active April 14, 2018 07:06
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 michaeljblum/843864cb4ccc57f7d61ed2c526012990 to your computer and use it in GitHub Desktop.
Save michaeljblum/843864cb4ccc57f7d61ed2c526012990 to your computer and use it in GitHub Desktop.
Counties of Laos
<!DOCTYPE html>
<style>
h1 {
font-family:arial;
font-size:32px;
color:#333;
}
#info {
position:absolute;
top: 10px;
left: 10px;
}
.county-borders {
fill: none;
stroke: rgba(255,255,255,0.5);
stroke-width: 0.25px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
</style>
<body>
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var margin = {top : 0, left: 0, right: 0, bottom:0};
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
var tooltip = d3.select("body").append("div").style("opacity","0").style("position","absolute");
svg.append('rect')
.attr('class', 'background')
.attr('width', width)
.attr('height', height)
.attr("fill","#191930");
function dragged(){
d3.select().attr("transform","translate("+d3.event.x+","+d3.event.y+")");
}
var tooltip = d3.select("body").append("div").style("opacity","0").style("position","absolute");
svg.append("text")
.attr("x", (width / 4.2))
.attr("y", (height / 1.2))
.attr("text-anchor", "middle")
.style("font-size", "18px")
.style("fill","lightyellow")
.style("text-decoration","underline")
.text("The Counties of the Lao People's Democratic Republic");
var projection = d3.geoMercator()
.scale(width*3.2)
.rotate([-104.4331, -18.20])
.translate([width / 2, height / 2]);
var path = d3.geoPath()
.projection(projection);
d3.json("laos.json", function(error, data) {
if (error) throw error;
console.log(data);
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(data, data.objects.county).features)
.enter().append("path")
.attr("d", path)
.attr("fill","blue")
.attr("stroke","rgba(250,20,20,0.5)")
.on("mousemove",function(d){
this.style.fill = "red"
tooltip.style("opacity","1")
.style("left",(d3.event.pageX + width/4.5) +"px")
.style("top",d3.event.pageY+"px")
.style("color","lightyellow")
.style("font-size","14px")
tooltip.html(d.properties["name:eng_x_preferred"])})
.on("mouseout", function(){ this.style.fill = "blue"});
svg.append("path")
.attr("class", "county-borders")
.attr("d", path(topojson.mesh(data, data.objects.county, function(a, b) { return a !== b; })));
});
</script>
</body>
</html>
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