This simple bar chart is constructed from a TSV file storing the frequency of letters in the English language. The chart employs conventional margins and a number of D3 features:
forked from mbostock's block: Bar Chart
forked from zanarmstrong's block: Bar Chart: stripped easier
xxxxxxxxxx
<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>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js