Skip to content

Instantly share code, notes, and snippets.

Forked from mukhtyar/
Last active Aug 29, 2015
What would you like to do?
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=''></script>
<style type="text/css">
body {
font-family: Arial, sans-serif;
.pc {
fill: #bdbdbd;
stroke: #fff;
stroke-width: 0.5px;
.state {
fill: none;
stroke: #636363;
stroke-width: 1px;
stroke-linejoin: round;
#container {
border:2px solid #d0d0d0;
border-radius: 5px;
div.tooltip {
position: absolute;
text-align: center;
padding: 0.5em;
font-size: 10px;
color: #222;
background: #FFF;
border-radius: 2px;
pointer-events: none;
box-shadow: 0px 0px 2px 0px #a6a6a6;
text-shadow:#f5f5f5 0 1px 0;
.legend .legend-title {
text-align: left;
margin-bottom: 5px;
font-weight: bold;
font-size: 90%;
.legend .legend-scale ul {
margin: 0;
margin-bottom: 5px;
padding: 0;
float: left;
list-style: none;
.legend .legend-scale ul li {
font-size: 80%;
list-style: none;
margin-left: 0;
line-height: 18px;
margin-bottom: 2px;
.legend ul.legend-labels li span {
display: block;
float: left;
height: 20px;
width: 20px;
margin-right: 5px;
margin-left: 0;
.legend .legend-source {
font-size: 70%;
color: #999;
clear: both;
<body onload="sizeChange()">
<div id="container">
<h1>Parliamentary Constituencies, 2014</h1>
<div class='legend'>
<div class='legend-title'>Reservation Status</div>
<div class='legend-scale'>
<div class='legend-source'>Source: <a href="#link to source">Name of source</a></div>
<script type="text/javascript">"resize", sizeChange);
//Set d3 scale
var color_domain = ['GEN','SC','ST'];
var legend_labels = ['GEN','SC','ST'];
var color = d3.scale.threshold()
.range(["", "#b2df8a", "#1f78b4","#a6cee3"]);
//Set tooltip
var div ="body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
//Set d3 projection, path and svg
var projection = d3.geo.mercator()
.center([78, 27])
var path = d3.geo.path()
var svg ="#container")
.attr("width", "100%")
//Wait for data files to download before drawing
.defer(d3.json, "india_pc_2014_simplified.topojson")
.defer(d3.json, "india_state_2014_simplified.topojson")
.defer(d3.csv, "results.csv") //sample data
function ready(error, pc, state, data) {
//Set up for visualizing sample data
var pairResultWithId = {};
var pairNameWithId = {};
data.forEach(function(d) {
pairResultWithId[d.ST_CODE + d.PC_CODE] = d.Res;
pairNameWithId[d.ST_CODE + d.PC_CODE] = d.PC_NAME;
//Drawing pc boundaries
var pc_geojson = topojson.feature(pc, pc.objects.india_pc_2014);
.attr("class", "pc")
.attr("d", path)
.style ( "fill" , function (d) {
var result = pairResultWithId[ +];
if (result!='') {return color(result);}
.style("opacity", 0.8)
.on("mouseover", function(d) {"opacity", 1);
.style("opacity", 1)
div.text(pairNameWithId[ +] + " : " + pairResultWithId[ +])
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY -30) + "px");
.on("mouseout", function() {
.style("opacity", 0.8);
.style("opacity", 0);
//Drawing state boundaries
var state_geojson = topojson.feature(state, state.objects.india_state_2014);
.attr("class", "state")
.attr("d", path);
//Set up for drawing html legend elements
var legend ='.legend-scale')
.attr('class', 'legend-labels');
var keys = legend.selectAll('li')
.text(function(d, i){ return legend_labels[i];})
.style('background', function(d) { return color(d); })
//Function called when window is resized
function sizeChange() {"g").attr("transform", "scale(" + $("#container").width()/1000 + ")");
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment