Skip to content

Instantly share code, notes, and snippets.

@trtg
Created January 7, 2013 23:09
Show Gist options
  • Save trtg/4479439 to your computer and use it in GitHub Desktop.
Save trtg/4479439 to your computer and use it in GitHub Desktop.
d3 tributary reusable bar chart

An inlet to tributary

{"description":"d3 tributary reusable bar chart","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"freq.tsv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"freq2.tsv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"tab":"edit","display_percent":0.7,"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,"hidepanel":false}
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 .01492
C .02780
D .44253
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
//http://bost.ocks.org/mike/chart/
//adding style.css to tributary magically
//gets applied here
var data = tributary.freq;
//convert strings to numbers
data.forEach(function(d) {
d.frequency = +d.frequency;
});
var data2 = tributary.freq2;
//convert strings to numbers
data2.forEach(function(d) {
d.frequency = +d.frequency;
});
//console.log(data)
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 446 - margin.left - margin.right,
height = 500 - 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(" + 600 + "," + margin.top + ")");
var bars = barChart()
.data(data)
.width(378)
.height(322);
bars(g);
bars.data(data2)
.width(628)
bars(g2);
function barChart(){
//default chart parameters
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