-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
118 lines (100 loc) · 3.3 KB
/
script.js
File metadata and controls
118 lines (100 loc) · 3.3 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
(async function () {
try {
const res = await fetch("data.json");
const rootData = await res.json();
const width = 1000;
const height = 800;
const trunkHeight = 150;
const branchSpread = 300;
const levelGap = 180;
const svg = d3.select("#memoryTree")
.append("svg")
.attr("width", width)
.attr("height", height)
.style("background", "white");
const g = svg.append("g")
.attr("transform", `translate(${width / 2}, ${height - 100})`);
const root = d3.hierarchy(rootData);
// Step 1: Position nodes manually for a strong central trunk
root.x = 0;
root.y = 0;
const firstLevel = root.children || [];
const secondLevel = [];
firstLevel.forEach((child, i) => {
const angle = (-Math.PI / 2) + ((i - (firstLevel.length - 1) / 2) * 0.6);
child.x = Math.sin(angle) * branchSpread * 0.5;
child.y = -trunkHeight - Math.cos(angle) * levelGap;
if (child.children) {
child.children.forEach((grand, j) => {
grand.x = child.x + (j - (child.children.length - 1) / 2) * 100;
grand.y = child.y - levelGap * 0.8;
secondLevel.push(grand);
});
}
});
// Step 2: Combine all nodes
const allNodes = [root, ...firstLevel, ...secondLevel];
// Step 3: Create branches (trunk + splits)
const branches = [];
firstLevel.forEach(child => {
branches.push({
source: { x: root.x, y: root.y },
target: { x: child.x, y: child.y }
});
if (child.children) {
child.children.forEach(grand => {
branches.push({
source: { x: child.x, y: child.y },
target: { x: grand.x, y: grand.y }
});
});
}
});
// Step 4: Draw curved golden branches
const curve = d3.line()
.curve(d3.curveBasis)
.x(d => d.x)
.y(d => d.y);
svg.selectAll(".branch")
.data(branches)
.enter()
.append("path")
.attr("class", "branch")
.attr("fill", "none")
.attr("stroke", "#D4AF37")
.attr("stroke-width", 4)
.attr("d", d => curve([
d.source,
{ x: (d.source.x + d.target.x) / 2, y: (d.source.y + d.target.y) / 2 - 40 },
d.target
]));
// Step 5: Draw nodes with consistent colors
const node = svg.selectAll(".node")
.data(allNodes)
.enter()
.append("g")
.attr("transform", d => `translate(${width / 2 + d.x}, ${height - 100 + d.y})`)
.style("cursor", d => d.data.url ? "pointer" : "default")
.on("click", (e, d) => {
if (d.data.url) window.open(d.data.url, "_blank");
});
node.append("circle")
.attr("r", d => d.depth === 0 ? 16 : d.children ? 10 : 8)
.attr("fill", d => {
if (d.depth === 0) return "#D4AF37"; // root (gold trunk)
if (d.children || d.data.children) return "#FF69B4"; // people (pink)
return "#4CAF50"; // objects (green)
})
.attr("stroke", "#fff")
.attr("stroke-width", 1.5);
node.append("text")
.attr("y", d => d.depth === 0 ? 30 : -14)
.attr("text-anchor", "middle")
.style("font-family", "sans-serif")
.style("font-size", "13px")
.style("fill", "#333")
.text(d => d.data.name);
} catch (err) {
console.error("Tree rendering error:", err);
}
})();