Skip to content

Instantly share code, notes, and snippets.

@davo
Created August 26, 2014 18:47
Show Gist options
  • Save davo/7854e52462147e74bd7f to your computer and use it in GitHub Desktop.
Save davo/7854e52462147e74bd7f to your computer and use it in GitHub Desktop.
Reusable Line Chart
{"description":"Reusable Line Chart","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"data.tvs":{"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},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/EH8r9G7.png"}
letter frequency
A .08167
B .01492
C .02780
D .04253
E .12702
F .02288
G .02022
H .06094
I .06969
J .03153
K .00747
L .04025
M .02517
N .06249
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 = 698 - 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(1186)
.height(792);
bars(g);
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], 0.35);
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")
.ticks(5)
.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;
}
body {
font-family: 'Helvetica', Arial;
font-size: 1em;
background-color: #ffffff;
}
svg {
font-size: 0.6em;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: #00bedb;
}
.x.axis path {
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment