Skip to content

Instantly share code, notes, and snippets.

@victorkashirin
Last active August 29, 2015 14:08
Show Gist options
  • Save victorkashirin/01511ccd4a7dde2c931f to your computer and use it in GitHub Desktop.
Save victorkashirin/01511ccd4a7dde2c931f to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
text-align: center;
}
.state {
fill: none;
}
.container {
position: relative;
}
.interstate {
fill: none;
stroke: white;
stroke-width: .2;
stroke-linejoin: round;
}
.tooltip {
background-color: rgba(255, 255, 255, 0.9);
border: 1px solid #ddd;
box-shadow: 1px 1px 1px #ddd;
font-family: Arial, sans-serif;
font-size: 12px;
min-width: 120px;
padding: 4px;
position: absolute;
text-align: center;
}
.tooltip-name {
color: #333;
text-align: right;
width: 35%;
padding: 3px;
font-weight: bold;
}
.tooltip-value {
color: #333;
text-align: left;
width: 65%;
padding: 2px;
}
#legend {
padding: 1.5em 0 0 1.5em;
text-align: center;
}
.key {
border-top-width: 15px;
border-top-style: solid;
font-size: .75em;
width: 10%;
padding-left: 0;
padding-right: 0;
display: inline;
}
.list-inline {
margin-top: 15px;
}
.list-inline > li {
padding-left: 7px;
padding-right: 7px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.tile.v0.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/colorbrewer.v1.min.js"></script>
<script>
var width = 580,
height = 650;
var projection = d3.geo.mercator()
.center([93.0, 25.0])
.scale(1100);
var path = d3.geo.path()
.projection(projection);
var container = d3.select("body").append("div").attr("class", "container").style("width", "580px");
var svg = container.append("svg")
.attr("width", width)
.attr("height", height);
var legend = container.append("div")
.attr("id", "legend")
.text("Value legend").append('ul').attr('class', 'list-inline');
var tooltip = container.append("div")
.attr("class", "tooltip")
.html('')
.style("opacity", 0.95)
.style('display', 'none')
.style("left", (width - 270) + "px")
.style("top", (height / 7) + "px");
var color_scale = d3.scale.quantize().range(colorbrewer.Blues[7]);
d3.json("india.json", function (error, topology) {
color_scale.domain(d3.extent(topology.objects.IND_adm3.geometries, function (d) {
return d.properties.value
}));
var keys = legend.html("").selectAll('li')
.data(color_scale.range()).enter()
.append('li')
.attr('class', 'key')
.style('border-top-color', String)
.text(function (d) {
var r = color_scale.invertExtent(d);
return d3.round(r[0]);
});
svg.append("g").selectAll("path")
.data(topojson.feature(topology, topology.objects.IND_adm3).features)
.enter().append("path")
.attr("d", path)
.style('fill', function (d) {
return color_scale(d.properties.value)
})
.attr("class", "state")
.on("mouseover", function (d) {
d3.select(this).style('stroke-width', 1.5).style('stroke', 'black');
var table = tooltip.style("display", "block").append("div").attr('class', 'tooltip_table');
var row_1 = table.append("tr");
row_1.append("td").attr("class", "tooltip-name").text("District:");
row_1.append("td").attr("class", "tooltip-value").text(d.properties.name_2);
var row_2 = table.append("tr");
row_2.append("td").attr("class", "tooltip-name").text("Town:");
row_2.append("td").attr("class", "tooltip-value").text(d.properties.name_3);
var row_3 = table.append("tr");
row_3.append("td").attr("class", "tooltip-name").text("Value:");
row_3.append("td").attr("class", "tooltip-value").text(d.properties.value);
})
.on("mouseout", function (d) {
d3.select(this).style('stroke-width', .1).style('stroke', 'white');
tooltip.style("display", "none").html("");
});
svg.append("path")
.datum(topojson.mesh(topology, topology.objects.IND_adm3, function (a, b) {
return a !== b;
}))
.attr("class", "interstate")
.attr("d", path);
});
</script>
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment