Skip to content

Instantly share code, notes, and snippets.

@shilpavijay
Last active February 23, 2018 11:00
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 shilpavijay/d2c8c2c4023c1aa2a92c1c2203ec64c3 to your computer and use it in GitHub Desktop.
Save shilpavijay/d2c8c2c4023c1aa2a92c1c2203ec64c3 to your computer and use it in GitHub Desktop.
GeoJSON - GovData
d3.select(window).on("resize", sizeChange);
//Set d3 scale
var color_domain = ['<100','<1000','<10000','<20000','20000 & more'];
var legend_labels = ['<100','<1000','<10000','<20000','20000 & more']
var color = d3.scale.ordinal()
.domain(color_domain)
// .range(['#f1e5e5','#d7b2b2','#bc7f7f','#a14c4c','#7a0000'])
.range(['#ffe38f','#ffcf40','#cbaf59','#bf9b30','#956f00'])
//Set tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
//Set d3 projection, path and svg
var projection = d3.geo.mercator()
.center([78, 27])
.scale(1200);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#map")
.append("svg")
.attr("width", "130%")
.attr("height","100%")
.append("g");
//Wait for data files to download before drawing
queue()
.defer(d3.json, "json/india_states.json")
.defer(d3.csv, "No_of_Road_Acc.csv")
.await(ready);
function ready(error, state, data) {
//Set up for visualizing sample data
function draw(yearSelected) {
var pairStateWithNo = {}; //StateName: NoOfAccidents
var pairStateWithRange = {}; //StateName: Range(<1000)
data.forEach(function(d) {
var accNum = d[yearSelected];
pairStateWithNo[d.States] = accNum;
var range = '';
var len = String(accNum).length;
//Determining the Range of data:
switch(true) {
case (len >= color_domain[2].length - 1) && (accNum < 20000):
range = 3;
break;
case (len >= color_domain[2].length - 1) && (accNum >= 20000):
range = 4;
break;
case (len == 4) && (accNum < 10000):
range = 2;
break;
case (len > 2) && (accNum < 1000):
range = 1;
break;
case (len <= 2):
range = 0;
break;
}
pairStateWithRange[d.States] = color_domain[range];
});
var state_geojson = topojson.feature(state, state.objects.india_states);
//enter()
svg.selectAll(".state")
.data(state_geojson.features)
.enter()
.append("path")
.attr("class", "state")
.attr("d", path)
//update()
svg.selectAll(".state")
.style ( "fill" , function (d) {
var result = pairStateWithRange[d.properties.NAME_1];
if (result!='') { return color(result); }
})
.style("opacity", 0.8)
.on("mouseover", function(d) {
d3.select(this).transition().duration(100).style("opacity", 0.3)
// .style("stroke-width","3px");
div.transition().duration(100)
.style("opacity", 0.9)
div.text(d.properties.NAME_1 + ': ' + pairStateWithNo[d.properties.NAME_1])
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY -30) + "px");
})
.on("mouseout", function() {
d3.select(this)
.transition().duration(50)
.style("opacity", 0.8)
// .style("stroke-width","0.8px");
div.transition().duration(50)
.style("opacity", 0);
});
//exit()
svg.selectAll(".state")
.data(state_geojson.features)
.exit()
.remove();
}
//Default call on load
draw.call(this,"2016");
//Choose from Dropdown
// d3.select('#yearOpts')
// .on('change', function(){
// yearSelected = eval(d3.select(this).property('value'));
// draw.call(this,String(yearSelected));
// });
//Code for the 'Play' Button
years = ['2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'];
d3.select('#play')
.on("click", function() {
var idx = 0;
var playing = false;
if(playing == false){
timer = setInterval(function() {
if (idx < years.length){
yearSelected = years[idx];
draw.call(this,String(yearSelected));
d3.select("#displayYear").text("Year: " + yearSelected);
idx += 1;
}
else {
playing = true;
}
},1500);
}
else {
clearInterval(timer);
playing = false;
}
})
}
//Set up for drawing html legend elements
// legendColorDomain =['<100','20000 & more'];
// var legendColor = d3.scale.ordinal()
// .domain(legendColorDomain)
// .range(d3.schemeYlOrBr[9]);
// var soilColor = d3.interpolateYlOrBr;
var legend = d3.select('.legend-scale')
.append('ul')
.attr('class', 'legend-labels');
var keys = legend.selectAll('li')
.data(color_domain);
keys.enter().append('li')
.text(function(d, i){ return legend_labels[i];})
.append('span')
.style('background', function(d) { return color(d);});
//Function called when window is resized
function sizeChange() {
d3.select("g").attr("transform", "scale(" + $("#container").width()/1000 + ")");
$("svg").height($("#container").width()*0.75);
}
// function sequence() {
// d3.selectAll('.state').transition().duration(500)
// .style("fill", function (d) {
// var result = pairStateWithRange[d.properties.NAME_1];
// if (result!='') { return color(result); }
// });
// }
body {
/*font-family: Arial, sans-serif;*/
font-family: 'Amita';
color: #1f5404;
height: 50%;
width: 65%;
}
.state {
fill: none;
stroke: #636363;
stroke-width: 1px;
stroke-linejoin: round;
}
#container {
margin:2%;
padding:20px;
/*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: 90%;
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: 80%;
color: #999;
clear: both;
}
import pandas as pd
import numpy as np
df = pd.read_csv('No_of_Road_Acc.csv')
# print(df.head())
# nparr = np.array(df)
df.set_index('States', inplace=True)
df=df.fillna(0)
max = df.values.max()
min = df.values.min()
print(max, min)
# python -m http.server
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/acc_styles.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Sofia' rel='stylesheet'>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
</head>
<body onload="sizeChange()">
<div id="container">
<h3>Road Accidents in India</h3>
<div class="col-md-3">
<div class='legend'>
<div class='legend-title'>Number of Accidents</div>
<div class='legend-scale'></div>
<div class='legend-source'>Source: <a href="https://data.gov.in/">https://data.gov.in/</a></div>
</div>
<br/>
<button class="btn btn-info" id="play">Play</button>
<p id="displayYear">Year: 2016</p>
<!-- <select id = "yearOpts" class="selectpicker">
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016" selected="selected">2016</option>
</select> -->
</div>
<div id="map" class="col-md-9"></div>
</div>
<script type="text/javascript" src='js/acc_d3_code.js'></script>
</body>
</html>
States 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
Andhra Pradesh 9800 10981 10026 10953 9979 10634 9887 10681 11211 8353 9621 9267 9134 9729
Arunachal Pradesh 144 84 111 82 88 115 114 128 0 0 0 0 0 75
Assam 784 746 947 946 1010 849 1034 1345 1552 1553 1817 1777 1898 1999
Bihar 1493 1195 1076 1702 1971 1499 2719 2837 3177 2833 2837 2535 3255 2434
Chhattisgarh 2593 2837 3543 3356 3265 3814 3564 3363 3156 3654 3804 3758 3898 3164
Goa 356 323 400 421 536 610 787 925 675 654 422 434 451 356
Gujarat 10242 10229 10133 9071 9630 10167 9210 9177 9252 8188 7731 7437 6739 6309
Haryana 2529 2931 2917 3202 3752 3611 3693 3436 3425 3108 3253 3247 3389 2887
Himachal Pradesh 699 816 787 792 845 597 806 703 742 533 643 698 634 820
Jammu and Kashmir 956 1087 1060 1286 745 971 1054 930 892 948 922 767 837 537
Jharkhand 1146 1476 1580 1438 1766 968 1165 1077 1332 1358 1204 1252 1480 1704
Karnataka 8269 7972 8062 10004 15034 15723 12500 13215 12522 13012 11898 13308 10254 11462
Kerala 5334 5184 7669 5444 7215 6452 6637 6537 6401 6721 6605 6140 6888 7135
Madhya Pradesh 7576 8077 9454 12115 10645 9875 10987 12939 13153 12304 13876 14267 13166 14456
Maharashtra 11618 13853 11831 11957 13402 13307 12230 12767 13149 12846 12029 11760 11184 9052
Manipur 99 128 100 111 137 173 165 136 139 161 135 167 136 117
Meghalaya 91 101 105 109 79 79 122 82 81 86 80 91 117 102
Mizoram 32 13 9 26 36 31 18 29 25 16 40 51 34 27
Nagaland 25 35 77 54 58 19 18 14 13 19 41 94 11 0
Odisha 1324 1466 1466 2088 2198 1964 2386 2062 2129 2333 3433 3507 4074 3328
Punjab 1295 1398 1147 1434 1047 1497 1431 1376 1962 2064 2122 1519 1965 2101
Rajasthan 2430 2596 2380 2175 2870 2581 2913 3119 2625 2723 3029 3774 3638 3695
Sikkim 39 66 101 34 26 36 159 49 170 32 83 71 85 70
Tamil Nadu 13524 16417 17042 17013 17848 24912 18944 20722 20920 21810 20984 21441 23165 23405
Tripura 124 234 73 211 306 438 464 526 424 432 382 374 268 217
Uttarakhand 555 441 416 464 335 269 293 358 378 392 281 391 408 568
Uttar Pradesh 4214 5391 4975 5961 7396 8130 8783 8591 8861 9362 13196 11669 9320 11715
West Bengal 2610 4067 5040 3591 3170 3237 2600 4074 3832 3340 3832 3404 3802 3793
Andaman and Nicobar 0 0 0 0 0 0 0 0 0 0 0 56 32 19
Chandigarh 0 2 0 0 0 100 81 146 0 0 0 0 0 0
Dadra and Nagar Haveli 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Daman and Diu 0 0 0 0 0 0 0 0 0 0 0 0 0 6
Delhi 1062 1045 1300 1592 1133 919 952 916 0 0 0 0 0 0
Lakshadweep 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Puducherry 0 0 0 0 386 395 276 146 41 0 58 152 256 373
States 2016
Andhra Pradesh 5639
Arunachal Pradesh 75
Assam 1999
Bihar 2434
Chhattisgarh 3164
Goa 356
Gujarat 6309
Haryana 2887
Himachal Pradesh 820
Jammu and Kashmir 537
Jharkhand 1704
Karnataka 11462
Kerala 7135
Madhya Pradesh 14456
Maharashtra 9052
Manipur 117
Meghalaya 102
Mizoram 27
Nagaland 0
Odisha 3328
Punjab 2101
Rajasthan 3695
Sikkim 70
Tamil Nadu 23405
Telangana 4090
Tripura 217
Uttarakhand 568
Uttar Pradesh 11715
West Bengal 3793
Andaman and Nicobar 19
Chandigarh 0
Dadra and Nagar Haveli 0
Daman and Diu 6
Delhi 0
Lakshadweep 0
Puducherry 373
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment