Skip to content

Instantly share code, notes, and snippets.

@artidataio
Last active February 17, 2020 16:48
Show Gist options
  • Save artidataio/4a25aff39864ec088d0a61a7d31a6f44 to your computer and use it in GitHub Desktop.
Save artidataio/4a25aff39864ec088d0a61a7d31a6f44 to your computer and use it in GitHub Desktop.
Singapore Planning Area
license: mit
<!doctype html>
<html>
<head>
<title>Singapore Planning Area</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="https://d3js.org/d3-geo.v1.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="tooltip" class="hidden">
<p><span id="value"></p>
</div>
<script>
var margin = {top: 10, right: 10, bottom: 10, left: 10},
padding = {top: 10, right: 10, bottom: 10, left: 10},
vizWidth = 960,
vizHeight = 500,
plotWidth = vizWidth - margin.left - margin.right,
plotHeight = vizHeight - margin.top - margin.bottom,
panelWidth = plotWidth - padding.left - padding.right,
panelHeight = plotHeight - padding.top - padding.bottom;
var viz = d3.select("body").append("svg")
.classed("viz",true)
.attr("width", vizWidth)
.attr("height", vizHeight);
var plot = viz.append("g")
.attr("class","plot")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var panel = plot.append("g")
.attr("class","panel")
.attr("transform", "translate(" + padding.left + "," + padding.top + ")");
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("display", "none");
//Important Functions
function drawTooltip(d) {
console.log(d);
var xPosition = d3.event.pageX;
var yPosition = d3.event.pageY;
d3.select("#tooltip")
.classed("hidden",false)
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.text(d.properties.PLN_AREA_N);
}
function mouseout() {
d3.select("#tooltip").classed("hidden", true);
d3.select(this).classed("highlight",false)
}
d3.json("sg plan area 20170903.json", function(sg) {
var projection = d3.geoMercator().fitSize([panelWidth,panelHeight],sg),
geoPath = d3.geoPath(projection);
var areas = panel.selectAll("path")
.data(sg.features)
.enter()
.append("path")
.attr("d",geoPath)
.classed("area",true)
.on('mouseover', function(d) {
d3.select(this).classed("highlight",true);
drawTooltip(d);})
.on('mouseout',mouseout);
});
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
.viz{
background-color: #deebf7;
}
#tooltip {
font-size: 12px;
position: absolute;
width: auto;
height: auto;
padding: 2.5px;
border:1px solid black;
background: rgb(250, 250, 250);
background: rgba(250, 250, 250, 0.8);
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
path.area{
stroke-width:0.3;
fill: #ffffbf;
stroke: black;
}
path.area.highlight{
fill:#78c679;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment