forked from bumbeishvili/d3-v6-tip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
53 lines (39 loc) · 1.56 KB
/
index.html
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
<html>
<html>
<body>
<div style="margin:150px;font-family: Arial, Helvetica, sans-serif;">Hover over bars to see tooltip!</div>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="../build/d3-v6-tip.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/bumbeishvili/d3-tip-for-v6@4/d3-tip.min.css">
<svg width=800 height=500 style="margin:150px;margin-top:0px"></svg>
<script >
const width = 400, height = 100;
const data = [
{ "letter": "A", "frequency": 0.08167 },
{ "letter": "B", "frequency": 0.01492 },
{ "letter": "C", "frequency": 0.02782 },
{ "letter": "D", "frequency": 0.04253 },
{ "letter": "E", "frequency": 0.12702 }
]
const svg = d3.select("svg")
.style('overflow', 'visible')
const y = d3.scaleLinear().domain([0.127, 0,]).range([0, height])
const rects = svg.append("g")
.attr("fill", "aqua")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => i * 50)
.attr("y", d => y(d.frequency) - 50)
.attr("height", d => y(0) - y(d.frequency))
.attr("width", 10)
// -------- TIP CODE ---------
const tip = d3.tip().attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (event, d) { return JSON.stringify(d); });
svg.call(tip);
rects.on('mouseenter.tip', tip.show)
.on('mouseleave.tip', tip.hide)
</script>
</body>
</html>