Skip to content

Instantly share code, notes, and snippets.

@chuckpr
Last active August 29, 2015 14:11
Show Gist options
  • Save chuckpr/a04b6e28af19bd2893f3 to your computer and use it in GitHub Desktop.
Save chuckpr/a04b6e28af19bd2893f3 to your computer and use it in GitHub Desktop.
responder count bar
Day X13CCPS X13CXPS
1 0 19
3 2 19
7 5 15
14 42 6
30 39 1

Simple bar chart with hover. Learning D3.js...

var margin = {top: 25, right: 30, bottom: 90, left: 80},
width = 600 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var y = d3.scale.linear()
.range([height, 0]);
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], 0.1);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var chart = d3.select(".chart")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("readme.csv", type, function(error, data) {
x.domain(data.map(function(d) { return d.Day; }));
y.domain([0, d3.max(data, function(d) { return d.X13CCPS; })]);
chart.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "ax-title")
.attr("x", width / 2)
.attr("y", margin.bottom / 2)
.text("Day");
chart.append("g")
.attr("class", "axis")
.call(yAxis)
.append("text")
.attr("class", "ax-title")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", -margin.left / 2)
.text("Count");
var bar = chart.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.Day); })
.attr("y", function(d) { return y(d.X13CCPS); })
.attr("height", function(d) { return height - y(d.X13CCPS); })
.attr("width", x.rangeBand())
.attr("fill", "steelblue");
});
function type(d) {
d.X13CCPS = +d.X13CCPS;
d.X13CXPS = +d.X13CXPS;
return d;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: orange;
}
.axis {
font: 16px sans-serif;
}
.ax-title {
font: 24px sans-serif;
text-anchor: middle;
}
</style>
<svg class="chart"></svg>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="bar.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment