d3-tip-0.8.0-alpha.1.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * d3.tip
  3. * Copyright (c) 2013 Justin Palmer
  4. *
  5. * Tooltips for d3.js SVG visualizations
  6. */
  7. // eslint-disable-next-line no-extra-semi
  8. ;(function(root, factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD. Register as an anonymous module with d3 as a dependency.
  11. define([
  12. 'd3-collection',
  13. 'd3-selection'
  14. ], factory)
  15. } else if (typeof module === 'object' && module.exports) {
  16. /* eslint-disable global-require */
  17. // CommonJS
  18. var d3Collection = require('d3-collection'),
  19. d3Selection = require('d3-selection')
  20. module.exports = factory(d3Collection, d3Selection)
  21. /* eslint-enable global-require */
  22. } else {
  23. // Browser global.
  24. var d3 = root.d3
  25. // eslint-disable-next-line no-param-reassign
  26. root.d3.tip = factory(d3, d3)
  27. }
  28. }(this, function(d3Collection, d3Selection) {
  29. // Public - contructs a new tooltip
  30. //
  31. // Returns a tip
  32. return function() {
  33. var direction = d3TipDirection,
  34. offset = d3TipOffset,
  35. html = d3TipHTML,
  36. rootElement = document.body,
  37. node = initNode(),
  38. svg = null,
  39. point = null,
  40. target = null
  41. function tip(vis) {
  42. svg = getSVGNode(vis)
  43. if (!svg) return
  44. point = svg.createSVGPoint()
  45. rootElement.appendChild(node)
  46. }
  47. // Public - show the tooltip on the screen
  48. //
  49. // Returns a tip
  50. tip.show = function() {
  51. var args = Array.prototype.slice.call(arguments)
  52. if (args[args.length - 1] instanceof SVGElement) target = args.pop()
  53. var content = html.apply(this, args),
  54. poffset = offset.apply(this, args),
  55. dir = direction.apply(this, args),
  56. nodel = getNodeEl(),
  57. i = directions.length,
  58. coords,
  59. scrollTop = document.documentElement.scrollTop ||
  60. rootElement.scrollTop,
  61. scrollLeft = document.documentElement.scrollLeft ||
  62. rootElement.scrollLeft
  63. nodel.html(content)
  64. .style('opacity', 1).style('pointer-events', 'all')
  65. while (i--) nodel.classed(directions[i], false)
  66. coords = directionCallbacks.get(dir).apply(this)
  67. nodel.classed(dir, true)
  68. .style('top', (coords.top + poffset[0]) + scrollTop + 'px')
  69. .style('left', (coords.left + poffset[1]) + scrollLeft + 'px')
  70. return tip
  71. }
  72. // Public - hide the tooltip
  73. //
  74. // Returns a tip
  75. tip.hide = function() {
  76. var nodel = getNodeEl()
  77. nodel.style('opacity', 0).style('pointer-events', 'none')
  78. return tip
  79. }
  80. // Public: Proxy attr calls to the d3 tip container.
  81. // Sets or gets attribute value.
  82. //
  83. // n - name of the attribute
  84. // v - value of the attribute
  85. //
  86. // Returns tip or attribute value
  87. // eslint-disable-next-line no-unused-vars
  88. tip.attr = function(n, v) {
  89. if (arguments.length < 2 && typeof n === 'string') {
  90. return getNodeEl().attr(n)
  91. }
  92. var args = Array.prototype.slice.call(arguments)
  93. d3Selection.selection.prototype.attr.apply(getNodeEl(), args)
  94. return tip
  95. }
  96. // Public: Proxy style calls to the d3 tip container.
  97. // Sets or gets a style value.
  98. //
  99. // n - name of the property
  100. // v - value of the property
  101. //
  102. // Returns tip or style property value
  103. // eslint-disable-next-line no-unused-vars
  104. tip.style = function(n, v) {
  105. if (arguments.length < 2 && typeof n === 'string') {
  106. return getNodeEl().style(n)
  107. }
  108. var args = Array.prototype.slice.call(arguments)
  109. d3Selection.selection.prototype.style.apply(getNodeEl(), args)
  110. return tip
  111. }
  112. // Public: Set or get the direction of the tooltip
  113. //
  114. // v - One of n(north), s(south), e(east), or w(west), nw(northwest),
  115. // sw(southwest), ne(northeast) or se(southeast)
  116. //
  117. // Returns tip or direction
  118. tip.direction = function(v) {
  119. if (!arguments.length) return direction
  120. direction = v == null ? v : functor(v)
  121. return tip
  122. }
  123. // Public: Sets or gets the offset of the tip
  124. //
  125. // v - Array of [x, y] offset
  126. //
  127. // Returns offset or
  128. tip.offset = function(v) {
  129. if (!arguments.length) return offset
  130. offset = v == null ? v : functor(v)
  131. return tip
  132. }
  133. // Public: sets or gets the html value of the tooltip
  134. //
  135. // v - String value of the tip
  136. //
  137. // Returns html value or tip
  138. tip.html = function(v) {
  139. if (!arguments.length) return html
  140. html = v == null ? v : functor(v)
  141. return tip
  142. }
  143. // Public: sets or gets the root element anchor of the tooltip
  144. //
  145. // v - root element of the tooltip
  146. //
  147. // Returns root node of tip
  148. tip.rootElement = function(v) {
  149. if (!arguments.length) return rootElement
  150. rootElement = v == null ? v : functor(v)
  151. return tip
  152. }
  153. // Public: destroys the tooltip and removes it from the DOM
  154. //
  155. // Returns a tip
  156. tip.destroy = function() {
  157. if (node) {
  158. getNodeEl().remove()
  159. node = null
  160. }
  161. return tip
  162. }
  163. function d3TipDirection() { return 'n' }
  164. function d3TipOffset() { return [0, 0] }
  165. function d3TipHTML() { return ' ' }
  166. var directionCallbacks = d3Collection.map({
  167. n: directionNorth,
  168. s: directionSouth,
  169. e: directionEast,
  170. w: directionWest,
  171. nw: directionNorthWest,
  172. ne: directionNorthEast,
  173. sw: directionSouthWest,
  174. se: directionSouthEast
  175. }),
  176. directions = directionCallbacks.keys()
  177. function directionNorth() {
  178. var bbox = getScreenBBox()
  179. return {
  180. top: bbox.n.y - node.offsetHeight,
  181. left: bbox.n.x - node.offsetWidth / 2
  182. }
  183. }
  184. function directionSouth() {
  185. var bbox = getScreenBBox()
  186. return {
  187. top: bbox.s.y,
  188. left: bbox.s.x - node.offsetWidth / 2
  189. }
  190. }
  191. function directionEast() {
  192. var bbox = getScreenBBox()
  193. return {
  194. top: bbox.e.y - node.offsetHeight / 2,
  195. left: bbox.e.x
  196. }
  197. }
  198. function directionWest() {
  199. var bbox = getScreenBBox()
  200. return {
  201. top: bbox.w.y - node.offsetHeight / 2,
  202. left: bbox.w.x - node.offsetWidth
  203. }
  204. }
  205. function directionNorthWest() {
  206. var bbox = getScreenBBox()
  207. return {
  208. top: bbox.nw.y - node.offsetHeight,
  209. left: bbox.nw.x - node.offsetWidth
  210. }
  211. }
  212. function directionNorthEast() {
  213. var bbox = getScreenBBox()
  214. return {
  215. top: bbox.ne.y - node.offsetHeight,
  216. left: bbox.ne.x
  217. }
  218. }
  219. function directionSouthWest() {
  220. var bbox = getScreenBBox()
  221. return {
  222. top: bbox.sw.y,
  223. left: bbox.sw.x - node.offsetWidth
  224. }
  225. }
  226. function directionSouthEast() {
  227. var bbox = getScreenBBox()
  228. return {
  229. top: bbox.se.y,
  230. left: bbox.se.x
  231. }
  232. }
  233. function initNode() {
  234. var div = d3Selection.select(document.createElement('div'))
  235. div
  236. .style('position', 'absolute')
  237. .style('top', 0)
  238. .style('opacity', 0)
  239. .style('pointer-events', 'none')
  240. .style('box-sizing', 'border-box')
  241. return div.node()
  242. }
  243. function getSVGNode(element) {
  244. var svgNode = element.node()
  245. if (!svgNode) return null
  246. if (svgNode.tagName.toLowerCase() === 'svg') return svgNode
  247. return svgNode.ownerSVGElement
  248. }
  249. function getNodeEl() {
  250. if (node == null) {
  251. node = initNode()
  252. // re-add node to DOM
  253. rootElement.appendChild(node)
  254. }
  255. return d3Selection.select(node)
  256. }
  257. // Private - gets the screen coordinates of a shape
  258. //
  259. // Given a shape on the screen, will return an SVGPoint for the directions
  260. // n(north), s(south), e(east), w(west), ne(northeast), se(southeast),
  261. // nw(northwest), sw(southwest).
  262. //
  263. // +-+-+
  264. // | |
  265. // + +
  266. // | |
  267. // +-+-+
  268. //
  269. // Returns an Object {n, s, e, w, nw, sw, ne, se}
  270. function getScreenBBox() {
  271. var targetel = target || d3Selection.event.target
  272. while (targetel.getScreenCTM == null && targetel.parentNode == null) {
  273. targetel = targetel.parentNode
  274. }
  275. var bbox = {},
  276. matrix = targetel.getScreenCTM(),
  277. tbbox = targetel.getBBox(),
  278. width = tbbox.width,
  279. height = tbbox.height,
  280. x = tbbox.x,
  281. y = tbbox.y
  282. point.x = x
  283. point.y = y
  284. bbox.nw = point.matrixTransform(matrix)
  285. point.x += width
  286. bbox.ne = point.matrixTransform(matrix)
  287. point.y += height
  288. bbox.se = point.matrixTransform(matrix)
  289. point.x -= width
  290. bbox.sw = point.matrixTransform(matrix)
  291. point.y -= height / 2
  292. bbox.w = point.matrixTransform(matrix)
  293. point.x += width
  294. bbox.e = point.matrixTransform(matrix)
  295. point.x -= width / 2
  296. point.y -= height / 2
  297. bbox.n = point.matrixTransform(matrix)
  298. point.y += height
  299. bbox.s = point.matrixTransform(matrix)
  300. return bbox
  301. }
  302. // Private - replace D3JS 3.X d3.functor() function
  303. function functor(v) {
  304. return typeof v === 'function' ? v : function() {
  305. return v
  306. }
  307. }
  308. return tip
  309. }
  310. // eslint-disable-next-line semi
  311. }));