-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
85 lines (80 loc) · 2.4 KB
/
test.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
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
<!DOCTYPE html>
<html>
<head>
<title>Arbre Généalogique avec d3.js</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<br><br><br><br><br>
<svg width="1600" height="800"></svg>
<script>
// Données de l'arbre généalogique
const treeData = {
name: "Vous",
children: [
{
name: "Père",
children: [
{
name: "Grand-père Paternel",
},
{
name: "Grand-mère Paternelle",
},
],
},
{
name: "Mère",
children: [
{
name: "Grand-père Maternel",
},
{
name: "Grand-mère Maternelle",
},
],
},
],
};
// Configuration de l'arbre
const width = 1200;
const height = 400;
const svg = d3.select("svg");
// Création de la hiérarchie des données
const root = d3.hierarchy(treeData);
const treeLayout = d3.tree().size([width, height]);
treeLayout(root);
// Dessin des liens
const links = root.links();
svg.selectAll("path.link")
.data(links)
.enter()
.append("path")
.attr("class", "link")
.attr("d", d => {
return `M${d.source.x},${d.source.y} L${d.target.x},${d.target.y}`;
});
// Dessin des nœuds
const nodes = root.descendants();
svg.selectAll("circle.node")
.data(nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 5);
// Ajout de textes aux nœuds
svg.selectAll("text.node-label")
.data(nodes)
.enter()
.append("text")
.attr("class", "node-label")
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("dy", "-15")
.attr("text-anchor", "middle")
.text(d => d.data.name);
</script>
</body>
</html>