-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbubble.js
69 lines (58 loc) · 2.1 KB
/
bubble.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
const file = 'data.json';
const width = window.innerWidth;
const height = window.innerHeight;
const colors = {
html: '#F16529',
css: '#1C88C7',
js: '#FCC700'
};
const generateChart = data => {
const bubble = data => d3.pack()
.size([width, height])
.padding(2)(d3.hierarchy({ children: data }).sum(d => d.score));
const svg = d3.select('#bubble-chart')
.style('width', width)
.style('height', height);
const root = bubble(data);
const tooltip = d3.select('.tooltip');
const node = svg.selectAll()
.data(root.children)
.enter().append('g')
.attr('transform', `translate(${width / 2}, ${height / 2})`);
const circle = node.append('circle')
.style('fill', d => colors[d.data.category])
.on('mouseover', function (e, d) {
tooltip.select('img').attr('src', d.data.img);
tooltip.select('a').attr('href', d.data.link).text(d.data.name);
tooltip.select('span').attr('class', d.data.category).text(d.data.category);
tooltip.style('visibility', 'visible');
d3.select(this).style('stroke', '#222');
})
.on('mousemove', e => tooltip.style('top', `${e.pageY}px`)
.style('left', `${e.pageX + 10}px`))
.on('mouseout', function () {
d3.select(this).style('stroke', 'none');
return tooltip.style('visibility', 'hidden');
})
.on('click', (e, d) => window.open(d.data.link));
const label = node.append('text')
.attr('dy', 2)
.text(d => d.data.name.substring(0, d.r / 3));
node.transition()
.ease(d3.easeExpInOut)
.duration(1000)
.attr('transform', d => `translate(${d.x}, ${d.y})`);
circle.transition()
.ease(d3.easeExpInOut)
.duration(1000)
.attr('r', d => d.r);
label.transition()
.delay(700)
.ease(d3.easeExpInOut)
.duration(1000)
.style('opacity', 1)
};
(async () => {
data = await d3.json(file).then(data => data);
generateChart(data);
})();