|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
|
|
<head> |
|
<style> |
|
|
|
.bar{ |
|
fill: orange; |
|
} |
|
|
|
.bar:hover{ |
|
fill: brown; |
|
} |
|
|
|
.axis { |
|
font: 10px sans-serif; |
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #000; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.tooltip { |
|
position: absolute; |
|
width: 150px; |
|
height: 35px; |
|
pointer-events: none; |
|
} |
|
|
|
</style> |
|
</head> |
|
|
|
<body> |
|
|
|
<script src="http://d3js.org/d3.v3.min.js"></script> |
|
|
|
<script> |
|
// set the dimensions of the canvas |
|
var margin = {top: 20, right: 20, bottom: 70, left: 40}, |
|
width = 600 - margin.left - margin.right, |
|
height = 300 - margin.top - margin.bottom; |
|
|
|
|
|
// set the ranges |
|
var x = d3.scale.ordinal().rangeRoundBands([0, width], .05); |
|
|
|
var y = d3.scale.linear().range([height, 0]); |
|
|
|
//Tooltip |
|
var tooltip = d3.select("body").append("div") |
|
.attr("class", "tooltip"); |
|
|
|
|
|
// define the axis |
|
var xAxis = d3.svg.axis() |
|
.scale(x) |
|
.orient("bottom") |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.orient("left") |
|
.ticks(10); |
|
|
|
|
|
// add the SVG element |
|
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 + ")"); |
|
|
|
|
|
// load the data |
|
d3.json("data.json", function(error, data) { |
|
|
|
data.forEach(function(d) { |
|
d.code = +d.code; |
|
d.grade = +d.grade; |
|
}); |
|
|
|
// scale the range of the data |
|
x.domain(data.map(function(d) { return d.code; })); |
|
y.domain([0, d3.max(data, function(d) { return d.grade; })]); |
|
|
|
// add axis |
|
svg.append("g") |
|
.transition().duration(3000) |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxis) |
|
.selectAll("text") |
|
.style("text-anchor", "end") |
|
.attr("dx", "-.8em") |
|
.attr("dy", "-.55em") |
|
.attr("transform", "rotate(-90)" ); |
|
|
|
svg.append("g") |
|
.transition().duration(3000) |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
|
|
svg.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", 5) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end"); |
|
|
|
var bars = svg.selectAll(".bars") |
|
.data(data); |
|
|
|
//Enter |
|
var barsEnter = bars.enter() |
|
.append("rect") |
|
.attr("class", "bar") |
|
.attr("x", function(d) { return x(d.code); }) |
|
.attr("width", x.rangeBand()) |
|
.attr("y", function(d) { return y(d.grade); }) |
|
.attr("height", function(d) { return height - y(d.grade); }); |
|
|
|
//Exit |
|
bars.exit() |
|
.transition() |
|
.duration(1000) |
|
.attr("width", 0) |
|
.remove(); |
|
|
|
// Add bar chart |
|
svg.selectAll("bar") |
|
.data(data) |
|
.enter() |
|
.append("rect") |
|
.attr("class", "bar") |
|
.attr("x", function(d) { return x(d.code); }) |
|
.attr("width", x.rangeBand()) |
|
.attr("y", function(d) { return y(d.grade); }) |
|
.attr("height", function(d) { return height - y(d.grade); }) |
|
|
|
.on("mouseover", function(d) { |
|
tooltip.transition() |
|
.duration(700) |
|
.style("opacity", 1); |
|
tooltip.html("Student: " +d.code + "<br/> Grade: " + d.grade) |
|
.style("left", (d3.event.pageX + 5) + "px") |
|
.style("top", (d3.event.pageY - 28) + "px"); |
|
}) |
|
.on("mouseout", function(d) { |
|
tooltip.transition() |
|
.duration(500) |
|
.style("opacity", 0); |
|
}); |
|
}); |
|
|
|
</script> |
|
|
|
</body> |