Skip to content

Instantly share code, notes, and snippets.

@shawnmusgrave
Created November 4, 2015 15:23
Show Gist options
  • Save shawnmusgrave/657d6415fcdb2c227dbe to your computer and use it in GitHub Desktop.
Save shawnmusgrave/657d6415fcdb2c227dbe to your computer and use it in GitHub Desktop.
Knight D3 MOOC: Module 1 — bar chart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>RMV road test examiners in New England</title>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 32px;
margin: 0;
}
#container {
width: 660px;
margin-left: auto;
margin-right: auto;
margin-top: 50px;
padding: 15px;
background-color: white;
box-shadow: 3px 3px 5px 6px #ccc;
}
.attrib {
font-size: 12px;
font-style: italic;
text-anchor: end;
}
svg {
background-color: white;
}
rect {
fill: #9e1a1e;
}
g.bar {
cursor: pointer;
}
g.bar text {
font-size: 20px;
font-weight: bold;
fill: white;
text-anchor: end;
opacity: 0;
}
g.bar:hover text {
opacity: 1;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 18px;
}
.y.axis path,
.y.axis line {
opacity: 0;
}
</style>
</head>
<body>
<div id="container">
<h1>Road test examiners in New England</h1>
</div>
<script type="text/javascript">
var w = 650;
var h = 300;
var padding = [ 20, 10, 50, 150 ]; //Top, right, bottom, left
// not setting .domain for either scale, since don't have data yet
var widthScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1);
// create axes — again, only scales, not tick marks/labels
var xAxis = d3.svg.axis()
.scale(widthScale)
.ticks(4)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
var svg = d3.select("#container")
.append("svg")
.attr("width", w)
.attr("height", h);
// remember: need to allow read permission
d3.csv("necir_rmv_examiners.csv", function(data) {
// + is shortcut for string --> numeric
data.sort(function(a, b) {
return d3.ascending(+a.Examiners, +b.Examiners);
});
// loop through and find max value, set upper domain to that
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.Examiners;
}) ]);
// use number of entries in data for heightScale
heightScale.domain(data.map(function(d) { return d.State; } ));
//Bind data to groups (not bars directly)
var groups = svg.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("class", "bar");
//append one rect to svg for each data entry
var rects = groups.append("rect");
// assign x, y, width, height, fill, title text for each rect
rects.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.State);
})
.attr("height", heightScale.rangeBand())
.attr("width", 0);
//Add a text element to each group
groups.append("text")
.attr("x", function(d) {
return padding[3] + widthScale(d.Examiners) - 8;
})
.attr("y", function(d) {
return heightScale(d.State) + 22;
})
.text(function(d) {
return d.Examiners;
});
rects.transition()
.delay(function(d,i){
return i*75})
.ease("linear")
.duration(750)
.attr("width", function(d) {
return widthScale(d.Examiners);
})
.attr("height", heightScale.rangeBand());
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
});
svg.append("text")
.attr("class", "attrib")
.text("Data: State motor vehicle registries or departments.")
.attr("x", w)
.attr("y", h-4)
</script>
</body>
</html>
State Examiners
Rhode Island 10
Connecticut 52
Maine 29
Vermont 27
Massachusetts 42
New Hampshire 21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment