Spaces:
Running
Running
Add 2 files
Browse files- README.md +6 -4
- index.html +438 -19
README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: pink
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: knowledge-graph-s1
|
| 3 |
+
emoji: π³
|
| 4 |
+
colorFrom: purple
|
| 5 |
colorTo: pink
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
tags:
|
| 9 |
+
- deepsite
|
| 10 |
---
|
| 11 |
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
index.html
CHANGED
|
@@ -1,19 +1,438 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Interactive Knowledge Graph</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
| 9 |
+
<style>
|
| 10 |
+
.node {
|
| 11 |
+
transition: all 0.3s ease;
|
| 12 |
+
cursor: pointer;
|
| 13 |
+
}
|
| 14 |
+
.node:hover {
|
| 15 |
+
transform: scale(1.05);
|
| 16 |
+
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
| 17 |
+
}
|
| 18 |
+
.link {
|
| 19 |
+
stroke: #94a3b8;
|
| 20 |
+
stroke-width: 2px;
|
| 21 |
+
}
|
| 22 |
+
.graph-container {
|
| 23 |
+
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
|
| 24 |
+
border-radius: 1rem;
|
| 25 |
+
}
|
| 26 |
+
.tooltip {
|
| 27 |
+
position: absolute;
|
| 28 |
+
padding: 0.5rem 1rem;
|
| 29 |
+
background: white;
|
| 30 |
+
border-radius: 0.5rem;
|
| 31 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
| 32 |
+
pointer-events: none;
|
| 33 |
+
opacity: 0;
|
| 34 |
+
transition: opacity 0.2s;
|
| 35 |
+
z-index: 10;
|
| 36 |
+
}
|
| 37 |
+
@keyframes pulse {
|
| 38 |
+
0% { transform: scale(1); }
|
| 39 |
+
50% { transform: scale(1.05); }
|
| 40 |
+
100% { transform: scale(1); }
|
| 41 |
+
}
|
| 42 |
+
.pulse {
|
| 43 |
+
animation: pulse 2s infinite;
|
| 44 |
+
}
|
| 45 |
+
</style>
|
| 46 |
+
</head>
|
| 47 |
+
<body class="bg-gray-50 min-h-screen">
|
| 48 |
+
<div class="container mx-auto px-4 py-12">
|
| 49 |
+
<div class="text-center mb-12">
|
| 50 |
+
<h1 class="text-4xl font-bold text-gray-800 mb-4">Interactive Knowledge Graph</h1>
|
| 51 |
+
<p class="text-xl text-gray-600 max-w-3xl mx-auto">
|
| 52 |
+
Visualize complex relationships between concepts in an interactive network diagram.
|
| 53 |
+
</p>
|
| 54 |
+
<div class="mt-6 flex justify-center space-x-4">
|
| 55 |
+
<button id="physicsBtn" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
|
| 56 |
+
<i class="fas fa-atom mr-2"></i>Toggle Physics
|
| 57 |
+
</button>
|
| 58 |
+
<button id="addNodeBtn" class="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition">
|
| 59 |
+
<i class="fas fa-plus mr-2"></i>Add Node
|
| 60 |
+
</button>
|
| 61 |
+
<button id="resetBtn" class="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition">
|
| 62 |
+
<i class="fas fa-redo mr-2"></i>Reset Graph
|
| 63 |
+
</button>
|
| 64 |
+
</div>
|
| 65 |
+
</div>
|
| 66 |
+
|
| 67 |
+
<div class="graph-container relative p-6 shadow-xl">
|
| 68 |
+
<div id="graph" class="w-full h-[600px]"></div>
|
| 69 |
+
<div id="tooltip" class="tooltip"></div>
|
| 70 |
+
</div>
|
| 71 |
+
|
| 72 |
+
<div class="mt-12 grid grid-cols-1 md:grid-cols-3 gap-8">
|
| 73 |
+
<div class="bg-white p-6 rounded-xl shadow-md">
|
| 74 |
+
<div class="flex items-center mb-4">
|
| 75 |
+
<div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-4">
|
| 76 |
+
<i class="fas fa-info-circle text-blue-600"></i>
|
| 77 |
+
</div>
|
| 78 |
+
<h3 class="text-xl font-semibold text-gray-800">What is a Knowledge Graph?</h3>
|
| 79 |
+
</div>
|
| 80 |
+
<p class="text-gray-600">
|
| 81 |
+
A knowledge graph is a network of interconnected entities and their relationships.
|
| 82 |
+
It represents information in a way that's closer to how humans think about concepts.
|
| 83 |
+
</p>
|
| 84 |
+
</div>
|
| 85 |
+
<div class="bg-white p-6 rounded-xl shadow-md">
|
| 86 |
+
<div class="flex items-center mb-4">
|
| 87 |
+
<div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
|
| 88 |
+
<i class="fas fa-project-diagram text-purple-600"></i>
|
| 89 |
+
</div>
|
| 90 |
+
<h3 class="text-xl font-semibold text-gray-800">Graph Features</h3>
|
| 91 |
+
</div>
|
| 92 |
+
<ul class="text-gray-600 space-y-2">
|
| 93 |
+
<li class="flex items-start">
|
| 94 |
+
<i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
|
| 95 |
+
<span>Interactive nodes and connections</span>
|
| 96 |
+
</li>
|
| 97 |
+
<li class="flex items-start">
|
| 98 |
+
<i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
|
| 99 |
+
<span>Physics-based simulation</span>
|
| 100 |
+
</li>
|
| 101 |
+
<li class="flex items-start">
|
| 102 |
+
<i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
|
| 103 |
+
<span>Dynamic tooltips</span>
|
| 104 |
+
</li>
|
| 105 |
+
</ul>
|
| 106 |
+
</div>
|
| 107 |
+
<div class="bg-white p-6 rounded-xl shadow-md">
|
| 108 |
+
<div class="flex items-center mb-4">
|
| 109 |
+
<div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-4">
|
| 110 |
+
<i class="fas fa-lightbulb text-green-600"></i>
|
| 111 |
+
</div>
|
| 112 |
+
<h3 class="text-xl font-semibold text-gray-800">Try It Out</h3>
|
| 113 |
+
</div>
|
| 114 |
+
<p class="text-gray-600 mb-4">
|
| 115 |
+
Click on nodes to see details. Drag nodes to rearrange the graph. Use the buttons above to control the visualization.
|
| 116 |
+
</p>
|
| 117 |
+
<div class="flex space-x-2">
|
| 118 |
+
<span class="px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">Interactive</span>
|
| 119 |
+
<span class="px-3 py-1 bg-purple-100 text-purple-800 rounded-full text-sm">Dynamic</span>
|
| 120 |
+
<span class="px-3 py-1 bg-green-100 text-green-800 rounded-full text-sm">Responsive</span>
|
| 121 |
+
</div>
|
| 122 |
+
</div>
|
| 123 |
+
</div>
|
| 124 |
+
</div>
|
| 125 |
+
|
| 126 |
+
<script src="https://d3js.org/d3.v7.min.js"></script>
|
| 127 |
+
<script>
|
| 128 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 129 |
+
// Sample knowledge graph data
|
| 130 |
+
const graphData = {
|
| 131 |
+
nodes: [
|
| 132 |
+
{ id: 1, name: "Artificial Intelligence", type: "concept", description: "The simulation of human intelligence processes by machines.", color: "#3B82F6" },
|
| 133 |
+
{ id: 2, name: "Machine Learning", type: "subfield", description: "A subset of AI that enables systems to learn from data.", color: "#10B981" },
|
| 134 |
+
{ id: 3, name: "Neural Networks", type: "technique", description: "Computational models inspired by biological neural networks.", color: "#8B5CF6" },
|
| 135 |
+
{ id: 4, name: "Deep Learning", type: "method", description: "ML technique using multi-layered neural networks.", color: "#EC4899" },
|
| 136 |
+
{ id: 5, name: "Natural Language Processing", type: "application", description: "AI field focused on interactions between computers and human language.", color: "#F59E0B" },
|
| 137 |
+
{ id: 6, name: "Computer Vision", type: "application", description: "Field of AI that enables computers to interpret visual data.", color: "#6366F1" },
|
| 138 |
+
{ id: 7, name: "Reinforcement Learning", type: "method", description: "ML method where agents learn by taking actions in an environment.", color: "#EF4444" }
|
| 139 |
+
],
|
| 140 |
+
links: [
|
| 141 |
+
{ source: 1, target: 2, relation: "includes" },
|
| 142 |
+
{ source: 2, target: 3, relation: "uses" },
|
| 143 |
+
{ source: 3, target: 4, relation: "basis of" },
|
| 144 |
+
{ source: 2, target: 5, relation: "applies to" },
|
| 145 |
+
{ source: 2, target: 6, relation: "applies to" },
|
| 146 |
+
{ source: 2, target: 7, relation: "includes" },
|
| 147 |
+
{ source: 4, target: 5, relation: "enhances" },
|
| 148 |
+
{ source: 4, target: 6, relation: "enhances" }
|
| 149 |
+
]
|
| 150 |
+
};
|
| 151 |
+
|
| 152 |
+
// Set up the SVG container
|
| 153 |
+
const width = document.getElementById('graph').clientWidth;
|
| 154 |
+
const height = document.getElementById('graph').clientHeight;
|
| 155 |
+
|
| 156 |
+
const svg = d3.select("#graph")
|
| 157 |
+
.append("svg")
|
| 158 |
+
.attr("width", width)
|
| 159 |
+
.attr("height", height)
|
| 160 |
+
.attr("viewBox", [0, 0, width, height])
|
| 161 |
+
.attr("style", "max-width: 100%; height: auto;");
|
| 162 |
+
|
| 163 |
+
// Set up the simulation
|
| 164 |
+
const simulation = d3.forceSimulation(graphData.nodes)
|
| 165 |
+
.force("link", d3.forceLink(graphData.links).id(d => d.id).distance(150))
|
| 166 |
+
.force("charge", d3.forceManyBody().strength(-500))
|
| 167 |
+
.force("center", d3.forceCenter(width / 2, height / 2))
|
| 168 |
+
.force("collision", d3.forceCollide().radius(60));
|
| 169 |
+
|
| 170 |
+
// Create the links
|
| 171 |
+
const link = svg.append("g")
|
| 172 |
+
.selectAll("line")
|
| 173 |
+
.data(graphData.links)
|
| 174 |
+
.join("line")
|
| 175 |
+
.attr("class", "link")
|
| 176 |
+
.attr("stroke-width", 2);
|
| 177 |
+
|
| 178 |
+
// Add link labels
|
| 179 |
+
const linkText = svg.append("g")
|
| 180 |
+
.selectAll("text")
|
| 181 |
+
.data(graphData.links)
|
| 182 |
+
.join("text")
|
| 183 |
+
.attr("font-size", 12)
|
| 184 |
+
.attr("fill", "#64748b")
|
| 185 |
+
.text(d => d.relation);
|
| 186 |
+
|
| 187 |
+
// Create the nodes
|
| 188 |
+
const node = svg.append("g")
|
| 189 |
+
.selectAll("g")
|
| 190 |
+
.data(graphData.nodes)
|
| 191 |
+
.join("g")
|
| 192 |
+
.attr("class", "node")
|
| 193 |
+
.call(drag(simulation));
|
| 194 |
+
|
| 195 |
+
// Add circles to the nodes
|
| 196 |
+
node.append("circle")
|
| 197 |
+
.attr("r", 25)
|
| 198 |
+
.attr("fill", d => d.color)
|
| 199 |
+
.attr("stroke", "#fff")
|
| 200 |
+
.attr("stroke-width", 2);
|
| 201 |
+
|
| 202 |
+
// Add icons to the nodes based on type
|
| 203 |
+
node.append("text")
|
| 204 |
+
.attr("text-anchor", "middle")
|
| 205 |
+
.attr("dy", ".35em")
|
| 206 |
+
.attr("fill", "white")
|
| 207 |
+
.attr("font-size", "12px")
|
| 208 |
+
.html(d => {
|
| 209 |
+
switch(d.type) {
|
| 210 |
+
case "concept": return "π§ ";
|
| 211 |
+
case "subfield": return "π";
|
| 212 |
+
case "technique": return "βοΈ";
|
| 213 |
+
case "method": return "π";
|
| 214 |
+
case "application": return "π»";
|
| 215 |
+
default: return "π";
|
| 216 |
+
}
|
| 217 |
+
});
|
| 218 |
+
|
| 219 |
+
// Add node labels
|
| 220 |
+
const labels = node.append("text")
|
| 221 |
+
.attr("dy", 50)
|
| 222 |
+
.attr("text-anchor", "middle")
|
| 223 |
+
.attr("fill", "#1e293b")
|
| 224 |
+
.attr("font-weight", "600")
|
| 225 |
+
.text(d => d.name)
|
| 226 |
+
.clone(true).lower()
|
| 227 |
+
.attr("stroke", "white")
|
| 228 |
+
.attr("stroke-width", 3);
|
| 229 |
+
|
| 230 |
+
// Tooltip setup
|
| 231 |
+
const tooltip = d3.select("#tooltip");
|
| 232 |
+
|
| 233 |
+
node.on("mouseover", function(event, d) {
|
| 234 |
+
d3.select(this).select("circle").attr("r", 30);
|
| 235 |
+
|
| 236 |
+
tooltip.transition()
|
| 237 |
+
.duration(200)
|
| 238 |
+
.style("opacity", 1);
|
| 239 |
+
|
| 240 |
+
tooltip.html(`
|
| 241 |
+
<h3 class="font-bold text-lg mb-1" style="color: ${d.color}">${d.name}</h3>
|
| 242 |
+
<p class="text-sm text-gray-600 mb-1">Type: ${d.type}</p>
|
| 243 |
+
<p class="text-sm text-gray-700">${d.description}</p>
|
| 244 |
+
`)
|
| 245 |
+
.style("left", (event.pageX + 10) + "px")
|
| 246 |
+
.style("top", (event.pageY + 10) + "px");
|
| 247 |
+
})
|
| 248 |
+
.on("mouseout", function() {
|
| 249 |
+
d3.select(this).select("circle").attr("r", 25);
|
| 250 |
+
|
| 251 |
+
tooltip.transition()
|
| 252 |
+
.duration(500)
|
| 253 |
+
.style("opacity", 0);
|
| 254 |
+
});
|
| 255 |
+
|
| 256 |
+
// Update positions on each tick
|
| 257 |
+
simulation.on("tick", () => {
|
| 258 |
+
link
|
| 259 |
+
.attr("x1", d => d.source.x)
|
| 260 |
+
.attr("y1", d => d.source.y)
|
| 261 |
+
.attr("x2", d => d.target.x)
|
| 262 |
+
.attr("y2", d => d.target.y);
|
| 263 |
+
|
| 264 |
+
linkText
|
| 265 |
+
.attr("x", d => (d.source.x + d.target.x) / 2)
|
| 266 |
+
.attr("y", d => (d.source.y + d.target.y) / 2);
|
| 267 |
+
|
| 268 |
+
node
|
| 269 |
+
.attr("transform", d => `translate(${d.x},${d.y})`);
|
| 270 |
+
});
|
| 271 |
+
|
| 272 |
+
// Drag behavior
|
| 273 |
+
function drag(simulation) {
|
| 274 |
+
function dragstarted(event, d) {
|
| 275 |
+
if (!event.active) simulation.alphaTarget(0.3).restart();
|
| 276 |
+
d.fx = d.x;
|
| 277 |
+
d.fy = d.y;
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
function dragged(event, d) {
|
| 281 |
+
d.fx = event.x;
|
| 282 |
+
d.fy = event.y;
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
function dragended(event, d) {
|
| 286 |
+
if (!event.active) simulation.alphaTarget(0);
|
| 287 |
+
d.fx = null;
|
| 288 |
+
d.fy = null;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
return d3.drag()
|
| 292 |
+
.on("start", dragstarted)
|
| 293 |
+
.on("drag", dragged)
|
| 294 |
+
.on("end", dragended);
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// Button interactions
|
| 298 |
+
let physicsEnabled = true;
|
| 299 |
+
document.getElementById('physicsBtn').addEventListener('click', function() {
|
| 300 |
+
physicsEnabled = !physicsEnabled;
|
| 301 |
+
if (physicsEnabled) {
|
| 302 |
+
simulation.alphaTarget(0.3).restart();
|
| 303 |
+
this.innerHTML = '<i class="fas fa-atom mr-2"></i>Toggle Physics';
|
| 304 |
+
this.classList.remove('bg-gray-600');
|
| 305 |
+
this.classList.add('bg-blue-600');
|
| 306 |
+
} else {
|
| 307 |
+
simulation.alphaTarget(0);
|
| 308 |
+
this.innerHTML = '<i class="fas fa-pause mr-2"></i>Toggle Physics';
|
| 309 |
+
this.classList.remove('bg-blue-600');
|
| 310 |
+
this.classList.add('bg-gray-600');
|
| 311 |
+
}
|
| 312 |
+
});
|
| 313 |
+
|
| 314 |
+
document.getElementById('resetBtn').addEventListener('click', function() {
|
| 315 |
+
// Reset node positions
|
| 316 |
+
graphData.nodes.forEach(node => {
|
| 317 |
+
node.fx = null;
|
| 318 |
+
node.fy = null;
|
| 319 |
+
node.x = Math.random() * width;
|
| 320 |
+
node.y = Math.random() * height;
|
| 321 |
+
});
|
| 322 |
+
|
| 323 |
+
if (physicsEnabled) {
|
| 324 |
+
simulation.alpha(1).restart();
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
// Add pulse animation to all nodes temporarily
|
| 328 |
+
node.select("circle").classList.add("pulse");
|
| 329 |
+
setTimeout(() => {
|
| 330 |
+
node.select("circle").classList.remove("pulse");
|
| 331 |
+
}, 2000);
|
| 332 |
+
});
|
| 333 |
+
|
| 334 |
+
document.getElementById('addNodeBtn').addEventListener('click', function() {
|
| 335 |
+
const newNodeId = graphData.nodes.length + 1;
|
| 336 |
+
const colors = ["#3B82F6", "#10B981", "#8B5CF6", "#EC4899", "#F59E0B", "#6366F1", "#EF4444"];
|
| 337 |
+
const types = ["concept", "subfield", "technique", "method", "application"];
|
| 338 |
+
|
| 339 |
+
const newNode = {
|
| 340 |
+
id: newNodeId,
|
| 341 |
+
name: `New Node ${newNodeId}`,
|
| 342 |
+
type: types[Math.floor(Math.random() * types.length)],
|
| 343 |
+
description: "This is a newly added node to the knowledge graph.",
|
| 344 |
+
color: colors[Math.floor(Math.random() * colors.length)]
|
| 345 |
+
};
|
| 346 |
+
|
| 347 |
+
// Add the new node
|
| 348 |
+
graphData.nodes.push(newNode);
|
| 349 |
+
|
| 350 |
+
// Add a random link to an existing node
|
| 351 |
+
if (graphData.nodes.length > 1) {
|
| 352 |
+
const randomExistingNode = graphData.nodes[Math.floor(Math.random() * (graphData.nodes.length - 1))];
|
| 353 |
+
const relations = ["related to", "connected with", "part of", "similar to", "derived from"];
|
| 354 |
+
graphData.links.push({
|
| 355 |
+
source: newNodeId,
|
| 356 |
+
target: randomExistingNode.id,
|
| 357 |
+
relation: relations[Math.floor(Math.random() * relations.length)]
|
| 358 |
+
});
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
// Update the simulation with new data
|
| 362 |
+
updateGraph();
|
| 363 |
+
|
| 364 |
+
// Highlight the new node
|
| 365 |
+
const newNodeElement = node.filter(d => d.id === newNodeId);
|
| 366 |
+
newNodeElement.select("circle").classList.add("pulse");
|
| 367 |
+
setTimeout(() => {
|
| 368 |
+
newNodeElement.select("circle").classList.remove("pulse");
|
| 369 |
+
}, 3000);
|
| 370 |
+
});
|
| 371 |
+
|
| 372 |
+
function updateGraph() {
|
| 373 |
+
// Update the simulation
|
| 374 |
+
simulation.nodes(graphData.nodes);
|
| 375 |
+
simulation.force("link").links(graphData.links);
|
| 376 |
+
|
| 377 |
+
if (physicsEnabled) {
|
| 378 |
+
simulation.alpha(1).restart();
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
// Update the links
|
| 382 |
+
link.data(graphData.links).join("line").attr("class", "link");
|
| 383 |
+
linkText.data(graphData.links).join("text").text(d => d.relation);
|
| 384 |
+
|
| 385 |
+
// Update the nodes
|
| 386 |
+
const newNode = node.data(graphData.nodes).join("g")
|
| 387 |
+
.attr("class", "node")
|
| 388 |
+
.call(drag(simulation));
|
| 389 |
+
|
| 390 |
+
newNode.select("circle").attr("fill", d => d.color);
|
| 391 |
+
newNode.select("text").text(d => d.name);
|
| 392 |
+
|
| 393 |
+
// Add icons to new nodes
|
| 394 |
+
newNode.filter((d, i) => i >= graphData.nodes.length - 1)
|
| 395 |
+
.append("text")
|
| 396 |
+
.attr("text-anchor", "middle")
|
| 397 |
+
.attr("dy", ".35em")
|
| 398 |
+
.attr("fill", "white")
|
| 399 |
+
.attr("font-size", "12px")
|
| 400 |
+
.html(d => {
|
| 401 |
+
switch(d.type) {
|
| 402 |
+
case "concept": return "π§ ";
|
| 403 |
+
case "subfield": return "π";
|
| 404 |
+
case "technique": return "βοΈ";
|
| 405 |
+
case "method": return "π";
|
| 406 |
+
case "application": return "π»";
|
| 407 |
+
default: return "π";
|
| 408 |
+
}
|
| 409 |
+
});
|
| 410 |
+
|
| 411 |
+
// Add tooltip to new nodes
|
| 412 |
+
newNode.on("mouseover", function(event, d) {
|
| 413 |
+
d3.select(this).select("circle").attr("r", 30);
|
| 414 |
+
|
| 415 |
+
tooltip.transition()
|
| 416 |
+
.duration(200)
|
| 417 |
+
.style("opacity", 1);
|
| 418 |
+
|
| 419 |
+
tooltip.html(`
|
| 420 |
+
<h3 class="font-bold text-lg mb-1" style="color: ${d.color}">${d.name}</h3>
|
| 421 |
+
<p class="text-sm text-gray-600 mb-1">Type: ${d.type}</p>
|
| 422 |
+
<p class="text-sm text-gray-700">${d.description}</p>
|
| 423 |
+
`)
|
| 424 |
+
.style("left", (event.pageX + 10) + "px")
|
| 425 |
+
.style("top", (event.pageY + 10) + "px");
|
| 426 |
+
})
|
| 427 |
+
.on("mouseout", function() {
|
| 428 |
+
d3.select(this).select("circle").attr("r", 25);
|
| 429 |
+
|
| 430 |
+
tooltip.transition()
|
| 431 |
+
.duration(500)
|
| 432 |
+
.style("opacity", 0);
|
| 433 |
+
});
|
| 434 |
+
}
|
| 435 |
+
});
|
| 436 |
+
</script>
|
| 437 |
+
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 𧬠<a href="https://enzostvs-deepsite.hf.space?remix=Arg1990/knowledge-graph-s1" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
|
| 438 |
+
</html>
|