|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<head> |
|
<link href="https://fonts.googleapis.com/css?family=Fira+Sans:300,300i,400,500,700" rel="stylesheet"> |
|
</head> |
|
<style> |
|
body { |
|
margin: 15px; |
|
background-color: #F1F3F3; |
|
font-family: 'Fira Sans', sans-serif; |
|
} |
|
.bar { |
|
fill: #6F257F; |
|
} |
|
.bar:hover { |
|
fill: #CC0058; |
|
transition: fill 0.15s; |
|
} |
|
text { |
|
font-family: 'Fira Sans', sans-serif; |
|
font-weight: 500; |
|
font-size: 15px; |
|
color: #081F2C; |
|
} |
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #BDC2C6; |
|
stroke-width: 1px; |
|
shape-rendering: crispEdges; |
|
} |
|
.x path { |
|
display: none; |
|
} |
|
.y line { |
|
display: none; |
|
} |
|
.toolTip { |
|
position: absolute; |
|
pointer-events: none; |
|
display: none; |
|
min-width: 50px; |
|
height: auto; |
|
background: none repeat scroll 0 0 #ffffff; |
|
padding: 9px 14px 6px 14px; |
|
border-radius: 2px; |
|
text-align: center; |
|
line-height: 1.3; |
|
color: #5B6770; |
|
box-shadow: 0px 3px 9px rgba(0, 0, 0, .15); |
|
} |
|
.toolTip:after { |
|
content: ""; |
|
width: 0; |
|
height: 0; |
|
border-left: 12px solid transparent; |
|
border-right: 12px solid transparent; |
|
border-top: 12px solid white; |
|
position: absolute; |
|
bottom: -10px; |
|
left: 50%; |
|
margin-left: -12px; |
|
} |
|
.toolTip span { |
|
font-weight: 500; |
|
color: #081F2C; |
|
} |
|
</style> |
|
<svg width="960" height="500"></svg> |
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
var svg = d3.select("svg"), |
|
margin = {top: 20, right: 50, bottom: 30, left: 100}, |
|
width = +svg.attr("width") - margin.left - margin.right, |
|
height = +svg.attr("height") - margin.top - margin.bottom; |
|
|
|
var tooltip = d3.select("body").append("div").attr("class", "toolTip"); |
|
|
|
var x = d3.scaleLinear().range([0, width]); |
|
var y = d3.scaleBand().range([height, 0]); |
|
|
|
var g = svg.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
d3.json("data.json", function(error, data) { |
|
if (error) throw error; |
|
|
|
data.sort(function(a, b) { return a.value - b.value; }); |
|
|
|
x.domain([0, d3.max(data, function(d) { return d.value; })]); |
|
y.domain(data.map(function(d) { return d.area; })).padding(0.1); |
|
|
|
g.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x).ticks(5).tickFormat(function(d) { return parseInt(d / 1000); }).tickSizeInner([-height])); |
|
|
|
g.append("g") |
|
.attr("class", "y axis") |
|
.call(d3.axisLeft(y)); |
|
|
|
g.selectAll(".bar") |
|
.data(data) |
|
.enter().append("rect") |
|
.attr("class", "bar") |
|
.attr("x", 0) |
|
.attr("height", y.bandwidth()) |
|
.attr("y", function(d) { return y(d.area); }) |
|
.attr("width", function(d) { return x(d.value); }) |
|
.on("mousemove", function(d){ |
|
// Replace hard coded vals (50, 90) with 50% of the tooltip wioth and height + a top buffer |
|
tooltip |
|
.style("left", d3.event.pageX - 50 + "px") |
|
.style("top", d3.event.pageY - 90 + "px") |
|
.style("display", "inline-block") |
|
.html((d.area) + "<br><span>" + "£" + (d.value) + "</span>"); |
|
}) |
|
.on("mouseout", function(d){ tooltip.style("display", "none");}); |
|
}); |
|
</script> |