Skip to content

Instantly share code, notes, and snippets.

@mrfr0g
mrfr0g / d3.selection.tooltip.js
Created July 10, 2013 16:58
Simple modification of Bootstrap's tooltip method to allow tooltips on arbitrary d3 selections. Usage example, d3.selectAll('g.circle') .tooltip(); // Looks at the `tooltip` property of the datum. d3.selectAll('g.circle') .tooltip('Uses this value as the tooltip for the selection'); d3.selectAll('g.circle') .tooltip(function (d) {return 'any val…
(function () {
// Bootstrap provided getPosition uses offsetWidth and offsetHeight to calculate
// the positioning of the tooltip. SVG Elements do not have this property because
// SVG does not layout elements, it assumes elements are always positioned.
// This replaces their implementation for SVG elements, and utilizes getBoundingClientRect.
var getPosition = $.fn.tooltip.Constructor.prototype.getPosition;
$.fn.tooltip.Constructor.prototype.getPosition = function (inside) {
var svgParent = this.$element.parents('svg');
// Only apply to SVG children
// Test for iOS 3/BlackBerry