Skip to content

Instantly share code, notes, and snippets.

@mattmakesmaps
Forked from mbostock/.block
Last active December 18, 2015 10:39
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 mattmakesmaps/5769860 to your computer and use it in GitHub Desktop.
Save mattmakesmaps/5769860 to your computer and use it in GitHub Desktop.
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 2 columns, instead of 1. in line 1.
letter frequency
1 .01167
2 .01492
3 .02780
4 .01253
5 .02702
6 .02288
7 .02022
8 .02094
9 .03973
10 .01153
11 .01747
12 .04025
13 .02517
14 .04749
15 .05507
16 .01929
17 .01098
18 .05987
19 .04333
20 .04056
21 .02758
22 .01037
23 .02465
24 .01150
25 .01971
26 .20074
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.fartbar {
fill: red;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://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 formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);
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.tsv("data.tsv", function(error, data) {
data.forEach(function(d) {
d.frequency = +d.frequency;
});
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.style("text-anchor", "middle")
.attr("x", 480)
.attr("y", 25)
.text("Matt's Age");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); });
d3.select(svg.selectAll(".bar")[0][25]).attr("class", "fartbar");
function makeAxisLabels(inElement, inText, inAnchor, inX, inY, inFill) {
inElement.append("text")
.text(inText)
.attr("x", inX)
.attr("y", inY)
.attr("text-anchor", inAnchor)
.attr("font-family", "sans-serif")
.attr("font-size", "16px")
.attr("fill", inFill);
}
makeAxisLabels(svg, "My exposure to fart jokes", "middle", 400, 200, "steelblue");
makeAxisLabels(svg, "before Aaron made mapfart", "middle", 400, 225, "steelblue");
makeAxisLabels(svg, "My exposure to fart jokes", "end", 850, 100, "red");
makeAxisLabels(svg, "after Aaron made mapfart", "end", 850, 125, "red");
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment