-
Notifications
You must be signed in to change notification settings - Fork 117
/
tooltip.js
93 lines (80 loc) · 2.2 KB
/
tooltip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* global event */
import { select } from 'd3-selection'
import 'd3-transition'
import 'd3-dispatch'
import 'd3-ease'
function defaultLabel (d) {
return d.data.name
}
export function defaultFlamegraphTooltip () {
var rootElement = select('body')
var tooltip = null
// Function to get HTML content from data.
var html = defaultLabel
// Function to get text content from data.
var text = defaultLabel
// Whether to use d3's .html() to set content, otherwise use .text().
var contentIsHTML = false
function tip () {
tooltip = rootElement
.append('div')
.style('display', 'none')
.style('position', 'absolute')
.style('opacity', 0)
.style('pointer-events', 'none')
.attr('class', 'd3-flame-graph-tip')
}
tip.show = function (d) {
tooltip
.style('display', 'block')
.style('left', event.pageX + 5 + 'px')
.style('top', event.pageY + 5 + 'px')
.transition()
.duration(200)
.style('opacity', 1)
.style('pointer-events', 'all')
if (contentIsHTML) {
tooltip.html(html(d))
} else {
tooltip.text(text(d))
}
return tip
}
tip.hide = function () {
tooltip
.style('display', 'none')
.transition()
.duration(200)
.style('opacity', 0)
.style('pointer-events', 'none')
return tip
}
/**
* Gets/sets a function converting the d3 data into the tooltip's textContent.
*
* Cannot be combined with tip.html().
*/
tip.text = function (_) {
if (!arguments.length) return text
text = _
contentIsHTML = false
return tip
}
/**
* Gets/sets a function converting the d3 data into the tooltip's innerHTML.
*
* Cannot be combined with tip.text().
*
* @deprecated prefer tip.text().
*/
tip.html = function (_) {
if (!arguments.length) return html
html = _
contentIsHTML = true
return tip
}
tip.destroy = function () {
tooltip.remove()
}
return tip
}