Skip to content

Instantly share code, notes, and snippets.

@jdesilvio
Created September 13, 2015 02:44
Show Gist options
  • Save jdesilvio/d0734dc7da45d177b996 to your computer and use it in GitHub Desktop.
Save jdesilvio/d0734dc7da45d177b996 to your computer and use it in GitHub Desktop.
D3.js Bar Chart - Modified from Mike Bostock's example
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.incomplete {
opacity: 0.5;
}
.bar:hover {
fill: #BBB;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
div.tooltip {
position: absolute;
text-align: left;
padding: 10px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
h1 {
text-align: center;
font: 36px sans-serif;
}
body {
text-align: center;
}
</style>
<body>
<h1>Title</h1>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></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 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")
.ticks(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 + ")");
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.json("data.json", function(error, data) {
test = data;
if (error) throw error;
x.domain(data.map(function(d) { return d.month; }));
y.domain([0, d3.max(data, function(d) { return d.checkouts; })]);
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("Checkouts");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("class", function(d) { if (x(d.month) === x(data[data.length -1].month)) { return "bar incomplete"; } else { return "bar"; }})
.attr("x", function(d) { return x(d.month); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.checkouts); })
.attr("height", function(d) { return height - y(d.checkouts); })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(
"<strong>Top 5 Users:</strong>" + "</br>" +
d.topUsers[0] + "<br/>" +
d.topUsers[1] + "<br/>" +
d.topUsers[2] + "<br/>" +
d.topUsers[3] + "<br/>" +
d.topUsers[4] + "<br/>" + "</br>" +
"<strong>Top 5 Titles:</strong>" + "</br>" +
d.topTitles[0] + "<br/>" +
d.topTitles[1] + "<br/>" +
d.topTitles[2] + "<br/>" +
d.topTitles[3] + "<br/>" +
d.topTitles[4]
)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment