123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- // Tooltips for d3.js visualizations
- // https://github.com/Caged/d3-tip
- // Version 0.7.1
- // See LICENSE file for license details
-
- package d3tip
-
- // JSSource returns the d3-tip.js file
- const JSSource = `
- (function (root, factory) {
- if (typeof define === 'function' && define.amd) {
- // AMD. Register as an anonymous module with d3 as a dependency.
- define(['d3'], factory)
- } else if (typeof module === 'object' && module.exports) {
- // CommonJS
- var d3 = require('d3')
- module.exports = factory(d3)
- } 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 = getNodeEl(),
- i = directions.length,
- coords,
- scrollTop = document.documentElement.scrollTop || document.body.scrollTop,
- scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft
-
- nodel.html(content)
- .style('opacity', 1).style('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')
- .style('left', (coords.left + poffset[1]) + scrollLeft + 'px')
-
- return tip;
- };
-
- // Public - hide the tooltip
- //
- // Returns a tip
- tip.hide = function() {
- var nodel = getNodeEl()
- nodel.style('opacity', 0).style('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 getNodeEl().attr(n)
- } else {
- var args = Array.prototype.slice.call(arguments)
- d3.selection.prototype.attr.apply(getNodeEl(), 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 getNodeEl().style(n)
- } else {
- var args = Array.prototype.slice.call(arguments)
- d3.selection.prototype.style.apply(getNodeEl(), 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 : 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 : 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 : functor(v)
-
- return tip
- }
-
- // Public: destroys the tooltip and removes it from the DOM
- //
- // Returns a tip
- tip.destroy = function() {
- if(node) {
- getNodeEl().remove();
- node = null;
- }
- 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').style('top', 0).style('opacity', 0)
- .style('pointer-events', 'none').style('box-sizing', 'border-box')
-
- return node.node()
- }
-
- function getSVGNode(el) {
- el = el.node()
- if(el.tagName.toLowerCase() === 'svg')
- return el
-
- return el.ownerSVGElement
- }
-
- function getNodeEl() {
- if(node === null) {
- node = initNode();
- // re-add node to DOM
- document.body.appendChild(node);
- };
- return d3.select(node);
- }
-
- // 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;
-
- while ('undefined' === typeof targetel.getScreenCTM && 'undefined' === targetel.parentNode) {
- targetel = targetel.parentNode;
- }
-
- var 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
- }
-
- // Private - replace D3JS 3.X d3.functor() function
- function functor(v) {
- return typeof v === "function" ? v : function() {
- return v
- }
- }
-
- return tip
- };
-
- }));
- `
|