|
<!doctype html> |
|
|
|
<html> |
|
<head> |
|
<meta charset="utf-8"> |
|
<title>Diving</title> |
|
<style type="text/css"> |
|
* { |
|
margin: 0; |
|
padding: 0; |
|
font-family: sans-serif; |
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #000; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.dot { |
|
stroke: #000; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
|
|
<div id="chart"></div> |
|
<script src="//d3js.org/d3.v3.min.js"></script> |
|
<script> |
|
|
|
var margin = {top: 20, right: 20, bottom: 30, left: 40}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var x = d3.scale.linear() |
|
.range([0, width]); |
|
|
|
var y = d3.scale.linear() |
|
.range([height, 0]); |
|
|
|
var color = d3.scale.category20(); |
|
|
|
var xAxis = d3.svg.axis() |
|
.scale(x) |
|
.orient("bottom"); |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.orient("left"); |
|
|
|
var svg = d3.select("body").append("svg") |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
d3.json("data.json", function(error, data) { |
|
|
|
// Preprocess data |
|
data.forEach(function(d) { |
|
|
|
// coerce to int |
|
d.injuries = +d.injuries; |
|
|
|
// transform '#m#s' to minutes and coerce to int |
|
var t = d.writhing_time.split('m'); |
|
d.writhing_time = (+(t[0] * 60) + +(t[1].split('s'))[0]) / 60; |
|
}); |
|
|
|
x.domain(d3.extent(data, function(d) { return d.injuries; })).nice(); |
|
y.domain(d3.extent(data, function(d) { return d.writhing_time; })).nice(); |
|
|
|
// Draw X axis |
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxis) |
|
.append("text") |
|
.attr("class", "label") |
|
.attr("x", width) |
|
.attr("y", -6) |
|
.style("text-anchor", "end") |
|
.text("\"Injuries\" Per Game"); |
|
|
|
// Draw Y axis |
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
.append("text") |
|
.attr("class", "label") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", 6) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end") |
|
.text("Total Minutes Spent Writhing Per Game") |
|
|
|
// Draw points |
|
svg.selectAll(".dot") |
|
.data(data) |
|
.enter().append("circle") |
|
.attr("class", "dot") |
|
.attr("r", 3.5) |
|
.attr("cx", function(d) { return x(d.injuries); }) |
|
.attr("cy", function(d) { return y(d.writhing_time); }) |
|
.style("fill", function(d) { return color(d.country); }); |
|
|
|
|
|
// Draw labels |
|
var labels = svg.append("g").selectAll("text") |
|
.data(data) |
|
.enter().append("text") |
|
.text( function(d) { return d.country; }) |
|
.attr("x", function(d) { return x(d.injuries) + 5; }) |
|
.attr("y", function(d) { return y(d.writhing_time) + 3; }) |
|
.style("text-anchor","start") |
|
.style("font-size","10px"); |
|
}); |
|
</script> |
|
</body> |
|
</html> |