暫無描述

d3_tip.go 8.3KB

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