Created
September 20, 2015 03:04
-
-
Save jalapic/cf35b0212d18993ca07c to your computer and use it in GitHub Desktop.
Scatterplot, transition + diagonal line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<title>scatterplot|transition</title> | |
<style type="text/css"> | |
h1{ | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 26px; | |
font-style: bold; | |
} | |
body { | |
background-color: #F9FFFF; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 16px; | |
font-style: bold; | |
} | |
svg { | |
background-color: #F9FFFF; | |
} | |
.mycirc { | |
stroke-width: 2; | |
stroke: #411dae; | |
fill: #3adddb; | |
} | |
.mycirc:hover { | |
stroke: #170a3d; | |
fill: #181a8f; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: black; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-style: bold; | |
font-size: 11px; | |
} | |
</style> | |
<h1>Stop and Frisk Totals by Race for NYC in 2014 </h1> | |
</head> | |
<body> | |
<p>Original data available at <a href="http://www.nyc.gov/html/nypd/html/analysis_and_planning/stop_question_and_frisk_report.shtml">NYC.gov</a>.</p> | |
<script type="text/javascript"> | |
var w = 700; | |
var h = 500; | |
var padding = [ 20, 20, 50, 80 ]; //Top, right, bottom, left | |
// scale x data linearly | |
var xScale = d3.scale.linear() | |
.range([ padding[3], w - padding[1]]) | |
; | |
// scale y data ordinally | |
var yScale = d3.scale.linear() | |
.range([ padding[0], h - padding[2] ]); | |
// add x-axis | |
var xAxis = d3.svg.axis() | |
.scale(xScale) | |
.orient("bottom") | |
// .tickFormat(d3.format("d")); //remove comma | |
; | |
// add y-axis | |
var yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("left"); | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
d3.csv("stopfrisksum.txt", function(data) { | |
// inform domain to widthScale - x data | |
xScale.domain([ 0, d3.max(data, function(d) { | |
return +d.Brooklyn; | |
}) ]).nice(); | |
// inform domain to heightScale - y axis | |
yScale.domain([ | |
d3.max(data, function(d) { | |
return +d.Queens; | |
}) | |
,0 | |
]) | |
.nice(); | |
var circles = svg.selectAll("circle") | |
.data(data) | |
.enter() | |
.append("circle"); | |
// initial characterization of circles | |
circles.attr("class", "mycirc") | |
.attr("cx", function(d){return xScale(d.Brooklyn);}) | |
.attr("cy", function(d) {return yScale(d.Queens);}) | |
.attr("r", .1) | |
.append("title") | |
.text(function(d) { | |
return d.race + " individuals were stopped and frisked " + d.total + " times in NYC in 2014"; | |
}); | |
// transition entrance of cicles | |
circles.sort(function(a, b) { | |
return d3.ascending(+a.total, +b.total); | |
}) | |
.transition() | |
.delay(function(d, i) { | |
return i * 250; | |
}) | |
.duration(2000) | |
.attr("r", 5); | |
// add x-axis | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + (h - padding[2]) + ")") | |
.call(xAxis); | |
// add y-axis | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + padding[3] + ",0)") | |
.call(yAxis); | |
// add diagonal line | |
svg.append("line") | |
.attr("x1", xScale(0)) | |
.attr("y1", yScale(0)) | |
.attr("x2", xScale(6000)) | |
.attr("y2", yScale(6000)) | |
.attr("stroke-width", 2) | |
.attr("stroke", "red") | |
.attr("stroke-dasharray", "5,5") //style of svg-line | |
; | |
// text label for the x axis | |
svg.append("text") | |
.attr("x", (w + padding[3]) /2 ) | |
.attr("y", h - (padding[3])/7) | |
.attr("style","font-size:16px; font-weight: bold;") // to bold title | |
.style("text-anchor", "middle") | |
.text("Brooklyn Total"); | |
// Add the text label for the Y axis | |
svg.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", padding[3]/3) | |
.attr("x", (-h+padding[0])/2) | |
.attr("style","font-size:16px; font-weight: bold;") // to bold title | |
.style("text-anchor", "middle") | |
.text("Queens Total"); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"race","Bronx","Brooklyn","Manhattan","Queens","Staten_Island","total" | |
"AMERICAN INDIAN/ALASKAN NATIVE",16,23,14,115,17,185 | |
"ASIAN/PACIFIC ISLANDER",39,332,138,1594,70,2173 | |
"BLACK",3182,8696,3434,5688,2468,23468 | |
"BLACK-HISPANIC",839,547,661,478,158,2683 | |
"OTHER",143,160,239,107,57,706 | |
"WHITE",199,1243,711,1715,1259,5127 | |
"WHITE-HISPANIC",2115,1978,1563,2919,735,9310 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment