Based on http://bl.ocks.org/mbostock/3885705. Updated for the BA Dev Day talk
Sortable bar chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
letter | frequency | |
---|---|---|
A | .08167 | |
B | .01492 | |
C | .02780 | |
D | .04253 | |
E | .12702 | |
F | .02288 | |
G | .02022 | |
H | .06094 | |
I | .06973 | |
J | .00153 | |
K | .00747 | |
L | .04025 | |
M | .02517 | |
N | .06349 | |
O | .07507 | |
P | .01929 | |
Q | .00098 | |
R | .05987 | |
S | .06333 | |
T | .09056 | |
U | .02758 | |
V | .01037 | |
W | .02465 | |
X | .00150 | |
Y | .01971 | |
Z | .00074 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// d3.tip | |
// Copyright (c) 2013 Justin Palmer | |
// | |
// Tooltips for d3.js SVG visualizations | |
(function (root, factory) { | |
if (typeof define === 'function' && define.amd) { | |
// AMD. Register as an anonymous module with d3 as a dependency. | |
define(['d3'], factory) | |
} else { | |
// Browser global. | |
root.d3.tip = factory(root.d3) | |
} | |
}(this, function (d3) { | |
// Public - contructs a new tooltip | |
// | |
// Returns a tip | |
return function() { | |
var direction = d3_tip_direction, | |
offset = d3_tip_offset, | |
html = d3_tip_html, | |
node = initNode(), | |
svg = null, | |
point = null, | |
target = null | |
function tip(vis) { | |
svg = getSVGNode(vis) | |
point = svg.createSVGPoint() | |
document.body.appendChild(node) | |
} | |
// Public - show the tooltip on the screen | |
// | |
// Returns a tip | |
tip.show = function() { | |
var args = Array.prototype.slice.call(arguments) | |
if(args[args.length - 1] instanceof SVGElement) target = args.pop() | |
var content = html.apply(this, args), | |
poffset = offset.apply(this, args), | |
dir = direction.apply(this, args), | |
nodel = d3.select(node), | |
i = directions.length, | |
coords, | |
scrollTop = document.documentElement.scrollTop || document.body.scrollTop, | |
scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft | |
nodel.html(content) | |
.style({ opacity: 1, 'pointer-events': 'all' }) | |
while(i--) nodel.classed(directions[i], false) | |
coords = direction_callbacks.get(dir).apply(this) | |
nodel.classed(dir, true).style({ | |
top: (coords.top + poffset[0]) + scrollTop + 'px', | |
left: (coords.left + poffset[1]) + scrollLeft + 'px' | |
}) | |
return tip | |
} | |
// Public - hide the tooltip | |
// | |
// Returns a tip | |
tip.hide = function() { | |
nodel = d3.select(node) | |
nodel.style({ opacity: 0, 'pointer-events': 'none' }) | |
return tip | |
} | |
// Public: Proxy attr calls to the d3 tip container. Sets or gets attribute value. | |
// | |
// n - name of the attribute | |
// v - value of the attribute | |
// | |
// Returns tip or attribute value | |
tip.attr = function(n, v) { | |
if (arguments.length < 2 && typeof n === 'string') { | |
return d3.select(node).attr(n) | |
} else { | |
var args = Array.prototype.slice.call(arguments) | |
d3.selection.prototype.attr.apply(d3.select(node), args) | |
} | |
return tip | |
} | |
// Public: Proxy style calls to the d3 tip container. Sets or gets a style value. | |
// | |
// n - name of the property | |
// v - value of the property | |
// | |
// Returns tip or style property value | |
tip.style = function(n, v) { | |
if (arguments.length < 2 && typeof n === 'string') { | |
return d3.select(node).style(n) | |
} else { | |
var args = Array.prototype.slice.call(arguments) | |
d3.selection.prototype.style.apply(d3.select(node), args) | |
} | |
return tip | |
} | |
// Public: Set or get the direction of the tooltip | |
// | |
// v - One of n(north), s(south), e(east), or w(west), nw(northwest), | |
// sw(southwest), ne(northeast) or se(southeast) | |
// | |
// Returns tip or direction | |
tip.direction = function(v) { | |
if (!arguments.length) return direction | |
direction = v == null ? v : d3.functor(v) | |
return tip | |
} | |
// Public: Sets or gets the offset of the tip | |
// | |
// v - Array of [x, y] offset | |
// | |
// Returns offset or | |
tip.offset = function(v) { | |
if (!arguments.length) return offset | |
offset = v == null ? v : d3.functor(v) | |
return tip | |
} | |
// Public: sets or gets the html value of the tooltip | |
// | |
// v - String value of the tip | |
// | |
// Returns html value or tip | |
tip.html = function(v) { | |
if (!arguments.length) return html | |
html = v == null ? v : d3.functor(v) | |
return tip | |
} | |
function d3_tip_direction() { return 'n' } | |
function d3_tip_offset() { return [0, 0] } | |
function d3_tip_html() { return ' ' } | |
var direction_callbacks = d3.map({ | |
n: direction_n, | |
s: direction_s, | |
e: direction_e, | |
w: direction_w, | |
nw: direction_nw, | |
ne: direction_ne, | |
sw: direction_sw, | |
se: direction_se | |
}), | |
directions = direction_callbacks.keys() | |
function direction_n() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.n.y - node.offsetHeight, | |
left: bbox.n.x - node.offsetWidth / 2 | |
} | |
} | |
function direction_s() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.s.y, | |
left: bbox.s.x - node.offsetWidth / 2 | |
} | |
} | |
function direction_e() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.e.y - node.offsetHeight / 2, | |
left: bbox.e.x | |
} | |
} | |
function direction_w() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.w.y - node.offsetHeight / 2, | |
left: bbox.w.x - node.offsetWidth | |
} | |
} | |
function direction_nw() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.nw.y - node.offsetHeight, | |
left: bbox.nw.x - node.offsetWidth | |
} | |
} | |
function direction_ne() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.ne.y - node.offsetHeight, | |
left: bbox.ne.x | |
} | |
} | |
function direction_sw() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.sw.y, | |
left: bbox.sw.x - node.offsetWidth | |
} | |
} | |
function direction_se() { | |
var bbox = getScreenBBox() | |
return { | |
top: bbox.se.y, | |
left: bbox.e.x | |
} | |
} | |
function initNode() { | |
var node = d3.select(document.createElement('div')) | |
node.style({ | |
position: 'absolute', | |
top: 0, | |
opacity: 0, | |
'pointer-events': 'none', | |
'box-sizing': 'border-box' | |
}) | |
return node.node() | |
} | |
function getSVGNode(el) { | |
el = el.node() | |
if(el.tagName.toLowerCase() == 'svg') | |
return el | |
return el.ownerSVGElement | |
} | |
// Private - gets the screen coordinates of a shape | |
// | |
// Given a shape on the screen, will return an SVGPoint for the directions | |
// n(north), s(south), e(east), w(west), ne(northeast), se(southeast), nw(northwest), | |
// sw(southwest). | |
// | |
// +-+-+ | |
// | | | |
// + + | |
// | | | |
// +-+-+ | |
// | |
// Returns an Object {n, s, e, w, nw, sw, ne, se} | |
function getScreenBBox() { | |
var targetel = target || d3.event.target, | |
bbox = {}, | |
matrix = targetel.getScreenCTM(), | |
tbbox = targetel.getBBox(), | |
width = tbbox.width, | |
height = tbbox.height, | |
x = tbbox.x, | |
y = tbbox.y | |
point.x = x | |
point.y = y | |
bbox.nw = point.matrixTransform(matrix) | |
point.x += width | |
bbox.ne = point.matrixTransform(matrix) | |
point.y += height | |
bbox.se = point.matrixTransform(matrix) | |
point.x -= width | |
bbox.sw = point.matrixTransform(matrix) | |
point.y -= height / 2 | |
bbox.w = point.matrixTransform(matrix) | |
point.x += width | |
bbox.e = point.matrixTransform(matrix) | |
point.x -= width / 2 | |
point.y -= height / 2 | |
bbox.n = point.matrixTransform(matrix) | |
point.y += height | |
bbox.s = point.matrixTransform(matrix) | |
return bbox | |
} | |
return tip | |
}; | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<! based on http://bl.ocks.org/mbostock/3885705 by Mike Bostock > | |
<title>Bar chart</title> | |
<meta charset="utf-8"> | |
<link type="text/css" rel="stylesheet" href="tooltip-styles-barchart.css"> | |
<style> | |
body { | |
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | |
position: relative; | |
width: 960px; | |
} | |
.axis text { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.bar { | |
fill: orangered; | |
fill-opacity: .9; | |
} | |
.bar:hover { | |
fill: red; | |
} | |
.x.axis path { | |
display: none; | |
} | |
label { | |
position: absolute; | |
top: 10px; | |
right: 10px; | |
} | |
</style> | |
<body> | |
<p></p> | |
<label><input type="checkbox"> Sort values</label> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="d3-tip.js"></script> | |
<script> | |
var margin = {top: 50, right: 20, bottom: 30, left: 30}, | |
width = 1350 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var formatPercent = d3.format(".0%"); | |
var x = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1, 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") | |
.tickFormat(formatPercent); | |
var tip = d3.tip() | |
.attr('class', 'd3-tip') | |
.offset([-23, -8]) | |
.html(function(d) { | |
return "<strong>Frequency:</strong> " + d.frequency; | |
}) | |
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 + ")"); | |
svg.call(tip); | |
d3.tsv("barchart.tsv", function(error, data) { | |
data.forEach(function(d) { | |
d.frequency = +d.frequency; | |
}); | |
x.domain(data.map(function(d) { return d.letter; })); | |
y.domain([0, d3.max(data, function(d) { return d.frequency; })]); | |
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.letter); }) | |
.attr("width", x.rangeBand()) | |
.attr("y", function(d) { return y(d.frequency); }) | |
.attr("height", function(d) { return height - y(d.frequency); }) | |
.on('mouseover', tip.show) | |
.on('mouseout', tip.hide); | |
d3.select("input").on("change", change); | |
svg.selectAll(".bar") | |
.filter(function (d) {return d.letter == "N" ; }) | |
.style("fill", "black"); | |
function change() { | |
// Copy-on-write since tweens are evaluated after a delay. | |
var x0 = x.domain(data.sort(this.checked | |
? function(a, b) { return b.frequency - a.frequency; } | |
: function(a, b) { return d3.ascending(a.letter, b.letter); }) | |
.map(function(d) { return d.letter; })) | |
.copy(); | |
var transition = svg.transition().duration(750), | |
delay = function(d, i) { return i * 50; }; | |
transition.selectAll(".bar") | |
.delay(delay) | |
.attr("x", function(d) { return x0(d.letter); }); | |
transition.select(".x.axis") | |
.call(xAxis) | |
.selectAll("g") | |
.delay(delay); | |
} | |
}); | |
</script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*.d3-tip { | |
line-height: 1; | |
font-weight: bold; | |
padding: 12px; | |
background: rgba(0, 0, 0, 0.8); | |
color: #fff; | |
border-radius: 2px; | |
pointer-events: none; | |
}*/ | |
.d3-tip { | |
display: block; | |
position: absolute; | |
padding: .5em; | |
content: attr(title); | |
min-width: 120px; | |
text-align: center; | |
width: auto; | |
height: auto; | |
white-space: nowrap; | |
top: -32px; | |
background: rgba(0,0,0,.8); | |
-moz-border-radius:5px; | |
-webkit-border-radius:5px; | |
border-radius:5px; | |
color: #fff; | |
font-size: .86em; | |
pointer-events: none; | |
} | |
/* Eastward tooltips */ | |
.d3-tip.e:after { | |
content: "\25C0"; | |
margin: -4px 0 0 0; | |
top: 50%; | |
left: -8px; | |
} | |
/* Southward tooltips */ | |
.d3-tip.s:after { | |
content: "\25B2"; | |
margin: 0 0 1px 0; | |
top: -8px; | |
left: 0; | |
text-align: center; | |
} | |
/* Westward tooltips */ | |
.d3-tip.w:after { | |
content: "\25B6"; | |
margin: -4px 0 0 -1px; | |
top: 50%; | |
left: 100%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment