Skip to content

Instantly share code, notes, and snippets.

@scottkildall
Created November 8, 2015 00:12
Show Gist options
  • Save scottkildall/60327e09b4e7ef36f66d to your computer and use it in GitHub Desktop.
Save scottkildall/60327e09b4e7ef36f66d to your computer and use it in GitHub Desktop.
Bar Chart: stripped easier
letter frequency
A .08167
B .01492
C .02782
D .04253
E .12702
F .02288
G .02015
H .06094
I .06966
J .00153
K .00772
L .04025
M .02406
N .06749
O .07507
P .01929
Q .00095
R .05987
S .06327
T .09056
U .02758
V .00978
W .02360
X .00150
Y .01974
Z .00074
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
// INSTRUCTIONS: everywhere there is "xxxxx", replace with code
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var xScale = 10;
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) {
if (error) throw error;
console.log(width);
console.log(data.length);
// increment for each
xScale = width/data.length;
var letters = data.map(function(d) { return d.letter; });
var maxFrequency = d3.max(data, function(d) { return d.frequency; })
yScale = d3.scale.linear().domain([0, maxFrequency]).range([0,height])
// frequency is 0-1
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", mapX)
.attr("width", 20)
.attr("y", mapY)
.attr("height", mapH);
});
function mapX(d,i) {
//console.log(i*10);
return i * xScale;
}
function mapY(d,i) {
//-- in theory we'd map the frequency max
return height - yScale(d.frequency)
}
function mapH(d,i) {
//-- in theory we'd map the frequency max
return yScale(d.frequency);
}
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