Skip to content

Instantly share code, notes, and snippets.

@junkwhinger
Last active August 29, 2015 14:25
Show Gist options
  • Save junkwhinger/6d2e86d240405195a02b to your computer and use it in GitHub Desktop.
Save junkwhinger/6d2e86d240405195a02b to your computer and use it in GitHub Desktop.
convo_length freq
0 538
1 974
2 299
3 115
4 52
5 39
6 35
7 33
8 17
9 9
10 1
<!DOCTYPE html>
<meta charset="utf-8">
<head><h1>The Hacking Team: communication length of emails regarding 'SKA'</h1></head>
<style>
body {
font: 10px sans-serif;
}
.axis {
font: 11px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var percent = d3.format("0.2%");
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");
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.csv("data.csv", type, function(error, data) {
if (error) throw error;
var sum = d3.sum(data,function(d) {return d.freq; })
console.log(sum)
x.domain(data.map(function(d) { return d.convo_length; }));
y.domain([0, d3.max(data, function(d) { return d.freq; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.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");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.convo_length); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.freq); })
.attr("height", function(d) { return height - y(d.freq); })
.on("mouseover", function(d,i) {
d3.select(this)
.style("fill", "red")
})
.on("mouseout", function(d,i) {
d3.select(this)
.style("fill", "steelblue")
});
svg.selectAll(".barText")
.data(data)
.enter().append("text")
.attr("class", "barText")
.attr("x", function(d) { return x(d.convo_length) + x.rangeBand()/2; })
.attr("y", function(d) { return y(d.freq) - 5; })
.attr("text-anchor","middle")
.text(function(d) {return percent(d.freq / sum) });
});
function type(d) {
d.freq = +d.freq;
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment