Skip to content

Instantly share code, notes, and snippets.

@patrickberkeley
Forked from mbostock/.block
Last active December 19, 2015 18:48
Show Gist options
  • Save patrickberkeley/6001118 to your computer and use it in GitHub Desktop.
Save patrickberkeley/6001118 to your computer and use it in GitHub Desktop.

A variation of the example bar chart using a slightly more D.R.Y. style. The visual encoding is represented by two functions and their composition:

  • The value accessor returns the value (or property) to encode for a given data object.
  • The scale maps this value to a visual display encoding, such as a pixel position.
  • The map function represents the composition valuescale, mapping from data to display.

Inspired by Andrew Winterman’s post, Tooling for the Lazy Programmer: DRYing up D3.

letter frequency
A .08167
B .01492
C .02780
D .04253
E .12702
F .02288
G .02022
H .06094
I .06973
J .00153
K .00747
L .04025
M .02517
N .06749
O .07507
P .01929
Q .00098
R .05987
S .06333
T .09056
U .02758
V .01037
W .02465
X .00150
Y .01971
Z .00074
AA .08167
AB .01492
AC .02780
AD .04253
AE .12702
AF .02288
AG .02022
AH .06094
AI .06973
AJ .00153
AK .00747
AL .04025
AM .02517
AN .06749
AO .07507
AP .01929
AQ .00098
AR .05987
AS .06333
AT .09056
AU .02758
AV .01037
AW .02465
AX .00150
AY .01971
AZ .00074
BA .08167
BB .01492
BC .02780
BD .04253
BE .12702
BF .02288
BG .02022
BH .06094
BI .06973
BJ .00153
BK .00747
BL .04025
BM .02517
BN .06749
BO .07507
BP .01929
BQ .00098
BR .05987
BS .06333
BT .09056
BU .02758
BV .01037
BW .02465
BX .00150
BY .01971
BZ .00074
CA .08167
CB .01492
CC .02780
CD .04253
CE .12702
CF .02288
CG .02022
CH .06094
CI .06973
CJ .00153
CK .00747
CL .04025
CM .02517
CN .06749
CO .07507
CP .01929
CQ .00098
CR .05987
<!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;
}
.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 = 900 - margin.left - margin.right,
height = 800 - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var xValue = function(d) { return d.letter; }, // data -> value
xScale = d3.scale.ordinal().rangeRoundBands([0, width], .1), // value -> display
xMap = function(d) { return xScale(xValue(d)); }, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var yValue = function(d) { return d.frequency; }, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d)); }, // data -> display
yAxis = d3.svg.axis().scale(yScale).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", type, function(error, data) {
xScale.domain(data.map(xValue));
yScale.domain([0, d3.max(data, yValue)]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
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", xMap)
.attr("width", xScale.rangeBand)
.attr("y", yMap)
.attr("height", function(d) { return height - yMap(d); });
});
function type(d) {
d.frequency = +d.frequency;
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment