Skip to content

Instantly share code, notes, and snippets.

@davo
Created November 19, 2012 16:54
Show Gist options
  • Save davo/4111829 to your computer and use it in GitHub Desktop.
Save davo/4111829 to your computer and use it in GitHub Desktop.
Reusable Bar Char
{"description":"Reusable Bar Char","endpoint":"","display":"svg","public":true,"require":[],"tab":"edit","display_percent":0.6280948026772978,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
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
letter frequency
A .18167
B .21492
C .32780
D .44253
E .22702
F .12288
G .32022
H .46094
I .06973
J .00153
K .00747
L .04025
M .22517
N .16749
O .07507
P .01929
Q .30098
R .05987
S .06333
T .09056
U .02758
V .01037
W .02465
X .00150
Y .01971
Z .00074
//http://bost.ocks.org/mike/chart/
//simple bar chart example: http://bl.ocks.org/3885304
var data = tributary.freq;
data.forEach(function(d) {
d.frequency = +d.frequency;
});
var data2 = tributary.freq2;
data2.forEach(function(d) {
d.frequency = +d.frequency;
});
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 405 - margin.left - margin.right,
height = 367 - margin.top - margin.bottom;
var svg = d3.select("svg");
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var g2 = svg.append("g")
.attr("transform", "translate(" + 523 + "," + margin.top + ")");
var bars = barChart()
.data(data)
.width(328)
.height(322);
bars(g);
bars.data(data2)
.width(317)
bars(g2)
function barChart() {
var data = [];
var width = 300;
var height = 300;
var chart = function(g) {
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);
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
g.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
g.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");
g.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); });
}
chart.data = function(value) {
if(!arguments.length) return data;
data = value;
return chart;
}
chart.width = function(value) {
if(!arguments.length) return width;
width = value;
return chart;
}
chart.height = function(value) {
if(!arguments.length) return height;
height = value;
return chart;
}
return chart;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment