-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_pack_tooltip.html
More file actions
132 lines (117 loc) · 3.38 KB
/
circle_pack_tooltip.html
File metadata and controls
132 lines (117 loc) · 3.38 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html>
<head>
<title>D3 Circle Pack Visualization</title>
<style>
.node {
cursor: pointer;
}
.node:hover {
stroke: #000;
stroke-width: 1.5px;
}
.node text {
font-size: 12px;
pointer-events: none;
text-anchor: middle;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
// Read the CSV data
d3.csv("https://raw.githubusercontent.com/fantastic900/D3_CirclePack/master/New_Completed.csv").then(function(data) {
const root = {
name: "Root",
children: []
};
const parentNames = data.columns;
// Create parent objects
parentNames.forEach(parentName => {
root.children.push({
name: parentName,
children: []
});
});
// Populate children within each parent
data.forEach(row => {
parentNames.forEach(parentName => {
const value = row[parentName];
if (value) {
const parentIndex = parentNames.indexOf(parentName);
root.children[parentIndex].children.push({
name: value
});
}
});
});
// Preprocess the data
const pack = d3.pack()
.size([600, 600])
.padding(3);
const hierarchy = d3.hierarchy(root)
.sum(d => d.children ? 0 : 1)
.sort((a, b) => b.value - a.value);
pack(hierarchy);
// Create the SVG container
const svg = d3.select("body")
.append("svg")
.attr("width", 600)
.attr("height", 600);
// Create the circles
const node = svg.append("g").selectAll("circle")
.data(hierarchy.descendants().slice(1))
.join("circle")
.attr("class", d => d.children ? "node" : "leaf node")
.attr("fill", d => d.children ? "steelblue" : "lightgray")
.attr("r", d => {
return d.r
})
.attr("cx", d => d.x)
.attr("cy", d => d.y);
// Add labels to the circles
const label = svg.selectAll(".leaf.node")
.append("text")
.attr("dy", "-1em")
.text(d => d.parent.data.name)
.attr("text-anchor", "middle")
.attr("font-size", "10px");
// Add tooltip on hover
node.filter(d => !d.children) // Exclude outer circles
.append("title")
.text(d => d.data.name);
// Zoom behavior
svg.call(d3.zoom().on("zoom", function() {
svg.attr("transform", d3.event.transform);
}));
// Add labels for parent names
const groupLabel = svg.append("g")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.selectAll("text")
.data(hierarchy.descendants().filter(function(d) {
return d.height === 1;
}))
.join("text")
.text(function(d) {
return d.data.name;
});
groupLabel
.attr("transform", d => `translate(${(d.x)}, ${(d.y - (d.r))})`)
.attr("dy", "-0.25em")
.attr("class", "repo-name")
.attr("font-family", "helvetica")
.attr("fill", "#000")
.clone(true)
.lower()
.attr("aria-hidden", "true")
.attr("fill", "none")
.attr("stroke", "#6A1131")
.attr("stroke-width", 1)
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round");
});
</script>
</body>
</html>