Skip to content

Instantly share code, notes, and snippets.

@brett-miller
Forked from mbostock/.block
Last active July 25, 2016 16:59
Show Gist options
  • Select an option

  • Save brett-miller/fac5ed22b1c9db412ee5f801b8f17776 to your computer and use it in GitHub Desktop.

Select an option

Save brett-miller/fac5ed22b1c9db412ee5f801b8f17776 to your computer and use it in GitHub Desktop.
Letter Frequency
license: gpl-3.0

D3 2.10 adds support for optional outer padding with d3.scale.ordinal. This parameter allows you to control the outer padding (before the first bar and after the last bar) separately from the inner padding between bars. In this case, the inner padding is 10% and the outer padding is 20%.

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], 0.1, 0.2);

See also this updated version with an axis title.

<!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="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], 0.1, 0.2);
var y = d3.scale.linear()
.range([height, 0]);
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 + ")");
var xAxis = svg.append("g")
.attr("class", "x axis");
var yAxis = svg.append("g")
.attr("class", "y axis");
data=[
{"date": "7/15/16", "price":134},
{"date": "7/16/16", "price":124},
{"date": "7/17/16", "price":143},
{"date": "7/18/16", "price":112},
{"date": "7/19/16", "price":142},
{"date": "7/20/16", "price":108},
{"date": "7/21/16", "price":148},
{"date": "7/22/16", "price":132},
{"date": "7/23/16", "price":110},
{"date": "7/24/16", "price":138},
{"date": "7/25/16", "price":132},
{"date": "7/26/16", "price":120},
{"date": "7/27/16", "price":115},
{"date": "7/28/16", "price":106},
{"date": "7/29/16", "price":135},
{"date": "7/30/16", "price":139},
{"date": "7/31/16", "price":108},
{"date": "8/1/16", "price": 137},
{"date": "8/2/16", "price": 140},
{"date": "8/3/16", "price": 109},
{"date": "8/4/16", "price": 150},
{"date": "8/5/16", "price": 103},
{"date": "8/6/16", "price": 99},
{"date": "8/7/16", "price": 103},
{"date": "8/8/16", "price": 136},
{"date": "8/9/16", "price": 120},
{"date": "8/10/16", "price":120},
{"date": "8/11/16", "price":107},
{"date": "8/12/16", "price":108}]
function updateChart(data) {
priceFormatter=d3.format(["$",""])
dateFormatter=d3.time.format("%d");
dataParsed = data.map(function(d) { return { "day": dateFormatter(new Date(d.date)), "price": d.price, "priceFormatted": priceFormatter(d.price) } } );
x.domain(dataParsed.map(function(d) { return d.day }));
y.domain(d3.extent(dataParsed.map(function(d){ return d.price })));
xAxis
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));
yAxis
.call(d3.svg.axis().scale(y).orient("left"));
svg.selectAll(".bar")
.data(dataParsed)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.day); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.price); })
.attr("height", function(d) { return height - y(d.price); });
}
updateChart(data);
</script>
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment