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