Here's a beautiful social network visualization that meets your requirements:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Music Network Visualization</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://unpkg.com/force-graph"></script>
<style>
body, html {
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden;
height: 100%;
width: 100%;
background-color: #121212;
color: #f5f5f5;
}
.container {
display: flex;
height: 100vh;
width: 100vw;
}
#graph {
flex: 1;
position: relative;
}
#details {
width: 300px;
padding: 20px;
background-color: #1e1e1e;
overflow-y: auto;
box-shadow: -5px 0 15px rgba(0, 0, 0, 0.2);
transition: transform 0.3s ease;
z-index: 10;
}
#details.hidden {
transform: translateX(100%);
}
#toggle-details {
position: absolute;
top: 10px;
right: 310px;
z-index: 11;
background: #333;
border: none;
color: white;
padding: 8px;
border-radius: 4px;
cursor: pointer;
}
#toggle-details.hidden {
right: 10px;
}
.node-details {
margin-bottom: 20px;
}
.node-details h2 {
margin-top: 0;
border-bottom: 1px solid #444;
padding-bottom: 10px;
color: #fff;
}
.node-details img {
max-width: 100%;
border-radius: 8px;
margin-bottom: 15px;
}
.metadata {
background-color: #2a2a2a;
padding: 15px;
border-radius: 8px;
margin-bottom: 15px;
}
.metadata h3 {
margin-top: 0;
color: #ccc;
font-size: 16px;
}
.metadata p {
margin: 5px 0;
font-size: 14px;
}
.connections {
background-color: #2a2a2a;
padding: 15px;
border-radius: 8px;
}
.connections h3 {
margin-top: 0;
color: #ccc;
font-size: 16px;
}
.connection-item {
display: flex;
align-items: center;
padding: 8px 0;
border-bottom: 1px solid #444;
cursor: pointer;
transition: background-color 0.2s;
}
.connection-item:hover {
background-color: #333;
}
.connection-item:last-child {
border-bottom: none;
}
.connection-icon {
width: 30px;
height: 30px;
border-radius: 50%;
margin-right: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
}
.search-container {
position: absolute;
top: 10px;
left: 10px;
z-index: 11;
display: flex;
gap: 10px;
}
#search-input {
padding: 8px 12px;
border-radius: 20px;
border: none;
background-color: rgba(255, 255, 255, 0.1);
color: white;
width: 200px;
}
#search-input::placeholder {
color: rgba(255, 255, 255, 0.5);
}
.controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
z-index: 11;
background-color: rgba(30, 30, 30, 0.7);
padding: 10px;
border-radius: 20px;
}
.control-btn {
background-color: #333;
border: none;
color: white;
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background-color 0.2s;
}
.control-btn:hover {
background-color: #555;
}
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
z-index: 100;
}
.loading h2 {
color: #fff;
margin-bottom: 20px;
}
.spinner {
border: 5px solid rgba(255, 255, 255, 0.1);
border-top: 5px solid #fff;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 1s linear infinite;
margin: 0 auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.legend {
position: absolute;
bottom: 20px;
left: 20px;
background-color: rgba(30, 30, 30, 0.7);
padding: 15px;
border-radius: 8px;
z-index: 11;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 8px;
}
.legend-color {
width: 20px;
height: 20px;
border-radius: 50%;
margin-right: 10px;
}
.tooltip {
position: absolute;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 8px 12px;
border-radius: 4px;
font-size: 12px;
pointer-events: none;
z-index: 100;
display: none;
}
</style>
</head>
<body>
<div class="container">
<div id="graph"></div>
<div id="details" class="hidden">
<div class="node-details">
<h2>Select a node to view details</h2>
<div class="metadata">
<p>Click on any node in the network to see its details and connections.</p>
</div>
</div>
</div>
</div>
<button id="toggle-details" class="hidden">›</button>
<div class="search-container">
<input type="text" id="search-input" placeholder="Search nodes...">
</div>
<div class="controls">
<button class="control-btn" id="zoom-in" title="Zoom In">+</button>
<button class="control-btn" id="zoom-out" title="Zoom Out">-</button>
<button class="control-btn" id="zoom-reset" title="Reset View">⟲</button>
</div>
<div class="legend">
<div class="legend-item">
<div class="legend-color" style="background-color: #4a90e2;"></div>
<span>SoundCloud Accounts</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #90caf9;"></div>
<span>Artists</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #ef9a9a;"></div>
<span>Groups</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #a5d6a7;"></div>
<span>Labels</span>
</div>
</div>
<div class="loading">
<h2>Loading Network Data</h2>
<div class="spinner"></div>
</div>
<div class="tooltip" id="tooltip"></div>
<script>
// Sample data - in a real application, this would come from your backend
const sampleData = {
nodes: [
// SoundCloud accounts
{ id: 'sc1', name: 'Bass Horizon', type: 'soundcloud', avatar: 'https://i.imgur.com/JR8lAcw.jpg', followers: 12500, tracks: 48, url: 'soundcloud.com/basshorizon' },
{ id: 'sc2', name: 'Melody Craft', type: 'soundcloud', avatar: 'https://i.imgur.com/NLs6IJs.jpg', followers: 8700, tracks: 32, url: 'soundcloud.com/melodycraft' },
{ id: 'sc3', name: 'Future Beats', type: 'soundcloud', avatar: 'https://i.imgur.com/7jGmAQj.jpg', followers: 22000, tracks: 65, url: 'soundcloud.com/futurebeats' },
{ id: 'sc4', name: 'Deep Dive', type: 'soundcloud', avatar: 'https://i.imgur.com/uVt8b5E.jpg', followers: 5300, tracks: 27, url: 'soundcloud.com/deepdive' },
{ id: 'sc5', name: 'Synth Wave', type: 'soundcloud', avatar: 'https://i.imgur.com/QZwGLKv.jpg', followers: 15800, tracks: 53, url: 'soundcloud.com/synthwave' },
{ id: 'sc6', name: 'Ambient Flow', type: 'soundcloud', avatar: 'https://i.imgur.com/8XO3Vy5.jpg', followers: 9200, tracks: 41, url: 'soundcloud.com/ambientflow' },
{ id: 'sc7', name: 'Rhythm Section', type: 'soundcloud', avatar: 'https://i.imgur.com/JdYOzLW.jpg', followers: 18600, tracks: 59, url: 'soundcloud.com/rhythmsection' },
{ id: 'sc8', name: 'Echo Chamber', type: 'soundcloud', avatar: 'https://i.imgur.com/pQZxDKT.jpg', followers: 7400, tracks: 36, url: 'soundcloud.com/echochamber' },
// Artists
{ id: 'a1', name: 'DJ Pulse', type: 'artist', genres: ['House', 'Techno'], location: 'Berlin', founded: 2015 },
{ id: 'a2', name: 'Melody Maker', type: 'artist', genres: ['Ambient', 'Downtempo'], location: 'London', founded: 2012 },
{ id: 'a3', name: 'Bass Hunter', type: 'artist', genres: ['Drum & Bass', 'Dubstep'], location: 'Los Angeles', founded: 2017 },
{ id: 'a4', name: 'Synth Master', type: 'artist', genres: ['Synthwave', 'Retrowave'], location: 'Tokyo', founded: 2014 },
{ id: 'a5', name: 'Vocal Vibe', type: 'artist', genres: ['Pop', 'R&B'], location: 'New York', founded: 2016 },
{ id: 'a6', name: 'Beat Crafter', type: 'artist', genres: ['Hip Hop', 'Trap'], location: 'Atlanta', founded: 2013 },
{ id: 'a7', name: 'Groove Theory', type: 'artist', genres: ['Funk', 'Soul'], location: 'Detroit', founded: 2011 },
{ id: 'a8', name: 'Digital Dreams', type: 'artist', genres: ['IDM', 'Glitch'], location: 'San Francisco', founded: 2018 },
{ id: 'a9', name: 'Sound Sculptor', type: 'artist', genres: ['Experimental', 'Noise'], location: 'Montreal', founded: 2019 },
{ id: 'a10', name: 'Rhythm Rebel', type: 'artist', genres: ['Breakbeat', 'Jungle'], location: 'Bristol', founded: 2010 },
// Groups
{ id: 'g1', name: 'Bass Collective', type: 'group', members: 8, location: 'London', founded: 2016, focus: 'Bass Music' },
{ id: 'g2', name: 'Synth Squad', type: 'group', members: 5, location: 'Los Angeles', founded: 2018, focus: 'Synthwave' },
{ id: 'g3', name: 'Beat Alliance', type: 'group', members: 12, location: 'New York', founded: 2015, focus: 'Hip Hop Production' },
{ id: 'g4', name: 'Future Sound', type: 'group', members: 7, location: 'Berlin', founded: 2017, focus: 'Electronic Fusion' },
{ id: 'g5', name: 'Harmony Hub', type: 'group', members: 9, location: 'Tokyo', founded: 2019, focus: 'Collaborative Production' },
// Labels
{ id: 'l1', name: 'Deep Records', type: 'label', artists: 24, founded: 2010, location: 'Berlin', genres: ['Techno', 'House', 'Ambient'] },
{ id: 'l2', name: 'Future Sound', type: 'label', artists: 18, founded: 2015, location: 'Los Angeles', genres: ['Future Bass', 'Trap', 'Electronic'] },
{ id: 'l3', name: 'Rhythm Nation', type: 'label', artists: 32, founded: 2008, location: 'London', genres: ['Drum & Bass', 'Jungle', 'Breakbeat'] },
{ id: 'l4', name: 'Synth City', type: 'label', artists: 15, founded: 2016, location: 'Tokyo', genres: ['Synthwave', 'Retrowave', 'Electro'] },
{ id: 'l5', name: 'Urban Beats', type: 'label', artists: 27, founded: 2012, location: 'New York', genres: ['Hip Hop', 'R&B', 'Soul'] }
],
links: [
// SoundCloud to Artist connections
{ source: 'sc1', target: 'a1', strength: 0.8 },
{ source: 'sc1', target: 'a3', strength: 0.6 },
{ source: 'sc2', target: 'a2', strength: 0.9 },
{ source: 'sc3', target: 'a4', strength: 0.7 },
{ source: 'sc3', target: 'a6', strength: 0.5 },
{ source: 'sc4', target: 'a5', strength: 0.8 },
{ source: 'sc5', target: 'a4', strength: 0.9 },
{ source: 'sc6', target: 'a2', strength: 0.7 },
{ source: 'sc6', target: 'a9', strength: 0.6 },
{ source: 'sc7', target: 'a7', strength: 0.8 },
{ source: 'sc7', target: 'a10', strength: 0.7 },
{ source: 'sc8', target: 'a8', strength: 0.9 },
// Artist to Group connections
{ source: 'a1', target: 'g4', strength: 0.7 },
{ source: 'a2', target: 'g5', strength: 0.6 },
{ source: 'a3', target: 'g1', strength: 0.8 },
{ source: 'a4', target: 'g2', strength: 0.9 },
{ source: 'a5', target: 'g5', strength: 0.7 },
{ source: 'a6', target: 'g3', strength: 0.8 },
{ source: 'a7', target: 'g3', strength: 0.6 },
{ source: 'a8', target: 'g4', strength: 0.7 },
{ source: 'a9', target: 'g5', strength: 0.5 },
{ source: 'a10', target: 'g1', strength: 0.8 },
// Artist to Label connections
{ source: 'a1', target: 'l1', strength: 0.9 },
{ source: 'a2', target: 'l1', strength: 0.7 },
{ source: 'a3', target: 'l2', strength: 0.8 },
{ source: 'a4', target: 'l4', strength: 0.9 },
{ source: 'a5', target: 'l5', strength: 0.8 },
{ source: 'a6', target: 'l5', strength: 0.7 },
{ source: 'a7', target: 'l5', strength: 0.6 },
{ source: 'a8', target: 'l2', strength: 0.7 },
{ source: 'a9', target: 'l1', strength: 0.5 },
{ source: 'a10', target: 'l3', strength: 0.9 },
// Additional connections for clustering
{ source: 'sc1', target: 'sc3', strength: 0.4 },
{ source: 'sc2', target: 'sc6', strength: 0.5 },
{ source: 'sc3', target: 'sc5', strength: 0.6 },
{ source: 'sc7', target: 'sc8', strength: 0.4 },
{ source: 'a1', target: 'a8', strength: 0.3 },
{ source: 'a2', target: 'a9', strength: 0.4 },
{ source: 'a3', target: 'a10', strength: 0.5 },
{ source: 'a4', target: 'a5', strength: 0.3 },
{ source: 'a6', target: 'a7', strength: 0.4 },
{ source: 'g1', target: 'g4', strength: 0.3 },
{ source: 'g2', target: 'g5', strength: 0.4 },
{ source: 'g3', target: 'g5', strength: 0.3 },
{ source: 'l1', target: 'l4', strength: 0.2 },
{ source: 'l2', target: 'l5', strength: 0.3 },
{ source: 'l3', target: 'l5', strength: 0.2 }
]
};
// Wait for DOM to be fully loaded
document.addEventListener('DOMContentLoaded', () => {
// Initialize the graph
initGraph(sampleData);
// Set up UI controls
setupUIControls();
});
function initGraph(data) {
// Process the data to add visual properties
processGraphData(data);
// Create the graph
const Graph = ForceGraph()
(document.getElementById('graph'))
.graphData(data)
.nodeId('id')
.nodeVal(node => getNodeSize(node))
.nodeLabel(node => node.name)
.nodeColor(node => getNodeColor(node))
.nodeCanvasObject((node, ctx, globalScale) => drawNode(node, ctx, globalScale))
.linkWidth(link => link.strength * 3)
.linkColor(link => 'rgba(255, 255, 255, 0.2)')
.onNodeClick(node => handleNodeClick(node, Graph))
.onNodeHover(node => handleNodeHover(node))
.d3Force('charge', d3.forceManyBody().strength(-150))
.d3Force('link', d3.forceLink().id(d => d.id).distance(link => 100 / (link.strength || 0.5)))
.d3Force('center', d3.forceCenter())
.d3Force('cluster', alpha => applyClusteringForce(alpha))
.cooldownTicks(100)
.onEngineStop(() => {
document.querySelector('.loading').style.display = 'none';
});
// Make graph responsive
window.addEventListener('resize', () => {
Graph.width(document.getElementById('graph').clientWidth)
.height(document.getElementById('graph').clientHeight);
});
// Setup search functionality
setupSearch(Graph);
// Setup zoom controls
setupZoomControls(Graph);
// Store graph instance for later use
window.graphInstance = Graph;
}
function processGraphData(data) {
// Add visual properties to nodes
data.nodes.forEach(node => {
// Add cluster property for force-directed layout
switch(node.type) {
case 'soundcloud':
node.cluster = 1;
break;
case 'artist':
node.cluster = 2;
break;
case 'group':
node.cluster = 3;
break;
case 'label':
node.cluster = 4;
break;
}
// Add importance score based on connections
node.importance = data.links.filter(link =>
link.source === node.id || link.target === node.id
).length;
});
// Process links to ensure they have proper references
data.links.forEach(link => {
// Add weight property for force-directed layout
link.weight = link.strength;
});
}
function getNodeSize(node) {
// Base size on importance (number of connections)
const baseSize = 5;
const importanceFactor = Math.sqrt(node.importance || 1);
switch(node.type) {
case 'soundcloud':
return baseSize * 1.2 * importanceFactor;
case 'artist':
return baseSize * 1.0 * importanceFactor;
case 'group':
return baseSize * 1.5 * importanceFactor;
case 'label':
return baseSize * 1.8 * importanceFactor;
default:
return baseSize;
}
}
function getNodeColor(node) {
switch(node.type) {
case 'soundcloud':
return '#4a90e2';
case 'artist':
return '#90caf9';
case 'group':
return '#ef9a9a';
case 'label':
return '#a5d6a7';
default:
return '#cccccc';
}
}
function drawNode(node, ctx, globalScale) {
const size = getNodeSize(node);
const x = node.x || 0;
const y = node.y || 0;
const fontSize = 12 / globalScale;
ctx.beginPath();
// Draw different node types
switch(node.type) {
case 'soundcloud':
if (node.avatar) {
// Create circular clipping path
ctx.save();
ctx.beginPath();
ctx.arc(x, y, size, 0, 2 * Math.PI, false);
ctx.clip();
// Create image if it doesn't exist
if (!node.img) {
node.img = new Image();
node.img.src = node.avatar;
}
// Draw the image if loaded
if (node.img.complete) {
ctx.drawImage(node.img, x - size, y - size, size * 2, size * 2);
} else {
// Draw placeholder circle
ctx.fillStyle = '#4a90e2';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
}
ctx.restore();
} else {
// Draw placeholder circle
ctx.fillStyle = '#4a90e2';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
}
break;
case 'artist':
// Draw artist icon (user icon with light blue background)
ctx.fillStyle = '#90caf9';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
// Draw simple user icon
ctx.fillStyle = '#ffffff';
ctx.beginPath();
ctx.arc(x, y - size * 0.2, size * 0.4, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.moveTo(x - size * 0.5, y + size * 0.6);
ctx.quadraticCurveTo(x, y + size * 0.9, x + size * 0.5, y + size * 0.6);
ctx.quadraticCurveTo(x + size * 0.3, y + size * 0.3, x, y + size * 0.3);
ctx.quadraticCurveTo(x - size * 0.3, y + size * 0.3, x - size * 0.5, y + size * 0.6);
ctx.fill();
break;
case 'group':
// Draw group icon (user group with light red background)
ctx.fillStyle = '#ef9a9a';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
// Draw simple group icon
ctx.fillStyle = '#ffffff';
// First person
ctx.beginPath();
ctx.arc(x - size * 0.3, y - size * 0.2, size * 0.3, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.moveTo(x - size * 0.6, y + size * 0.5);
ctx.quadraticCurveTo(x - size * 0.3, y + size * 0.7, x, y + size * 0.5);
ctx.quadraticCurveTo(x - size * 0.2, y + size * 0.2, x - size * 0.3, y + size * 0.2);
ctx.quadraticCurveTo(x - size * 0.4, y + size * 0.2, x - size * 0.6, y + size * 0.5);
ctx.fill();
// Second person
ctx.beginPath();
ctx.arc(x + size * 0.3, y - size * 0.2, size * 0.3, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.moveTo(x, y + size * 0.5);
ctx.quadraticCurveTo(x + size * 0.3, y + size * 0.7, x + size * 0.6, y + size * 0.5);
ctx.quadraticCurveTo(x + size * 0.4, y + size * 0.2, x + size * 0.3, y + size * 0.2);
ctx.quadraticCurveTo(x + size * 0.2, y + size * 0.2, x, y + size * 0.5);
ctx.fill();
break;
case 'label':
// Draw label icon (building with light green background)
ctx.fillStyle = '#a5d6a7';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
// Draw simple building icon
ctx.fillStyle = '#ffffff';
ctx.beginPath();
ctx.moveTo(x - size * 0.6, y + size * 0.6);
ctx.lineTo(x - size * 0.6, y - size * 0.2);
ctx.lineTo(x, y - size * 0.6);
ctx.lineTo(x + size * 0.6, y - size * 0.2);
ctx.lineTo(x + size * 0.6, y + size * 0.6);
ctx.closePath();
ctx.fill();
// Draw windows
ctx.fillStyle = '#a5d6a7';
ctx.fillRect(x - size * 0.4, y - size * 0.1, size * 0.2, size * 0.2);
ctx.fillRect(x + size * 0.2, y - size * 0.1, size * 0.2, size * 0.2);
ctx.fillRect(x - size * 0.4, y + size * 0.2, size * 0.2, size * 0.2);
ctx.fillRect(x + size * 0.2, y + size * 0.2, size * 0.2, size * 0.2);
// Draw door
ctx.fillRect(x - size * 0.1, y + size * 0.2, size * 0.2, size * 0.4);
break;
default:
ctx.fillStyle = '#cccccc';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
}
// Add highlight for selected node
if (node.selected) {
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 2 / globalScale;
ctx.arc(x, y, size + 2 / globalScale, 0, 2 * Math.PI);
ctx.stroke();
}
// Add highlight for hovered node
if (node.hovered) {
ctx.strokeStyle = '#ffeb3b';
ctx.lineWidth = 2 / globalScale;
ctx.arc(x, y, size + 2 / globalScale, 0, 2 * Math.PI);
ctx.stroke();
}
// Add faded effect for non-connected nodes when a node is selected
if (window.selectedNode && !node.selected && !node.isConnected) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
}
// Draw node label if zoomed in enough
if (globalScale > 0.8 || node.selected || node.hovered) {
ctx.font = `${fontSize}px Sans-Serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = 'white';
// Add background for text
const textWidth = ctx.measureText(node.name).width;
ctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
ctx.fillRect(
x - textWidth / 2 - 2,
y + size + 2,
textWidth + 4,
fontSize + 4
);
ctx.fillStyle = 'white';
ctx.fillText(node.name, x, y + size + fontSize / 2 + 4);
}
}
function handleNodeClick(node, Graph) {
// Clear previous selection
Graph.graphData().nodes.forEach(n => {
n.selected = false;
n.isConnected = false;
});
// Mark the selected node
node.selected = true;
window.selectedNode = node;
// Find connected nodes
const links = Graph.graphData().links;
const connectedNodes = links
.filter(link => link.source.id === node.id || link.target.id === node.id)
.map(link => link.source.id === node.id ? link.target : link.source);
// Mark connected nodes
connectedNodes.forEach(n => {
n.isConnected = true;
});
// Update details panel
updateDetailsPanel(node, connectedNodes);
// Show details panel if hidden
const detailsPanel = document.getElementById('details');
const toggleButton = document.getElementById('toggle-details');
if (detailsPanel.classList.contains('hidden')) {
detailsPanel.classList.remove('hidden');
toggleButton.textContent = '›';
toggleButton.classList.remove('hidden');
}
// Center view on the selected node with animation
Graph.centerAt(node.x, node.y, 1000);
Graph.zoom(1.5, 1000);
}
function handleNodeHover(node) {
// Clear previous hover state
window.graphInstance.graphData().nodes.forEach(n => {
n.hovered = false;
});
// Update tooltip
const tooltip = document.getElementById('tooltip');
if (node) {
// Mark the hovered node
node.hovered = true;
// Update and show tooltip
tooltip.innerHTML = `<strong>${node.name}</strong><br>${capitalizeFirstLetter(node.type)}`;
tooltip.style.display = 'block';
tooltip.style.left = (event.pageX + 10) + 'px';
tooltip.style.top = (event.pageY + 10) + 'px';
} else {
// Hide tooltip
tooltip.style.display = 'none';
}
}
function updateDetailsPanel(node, connectedNodes) {
const detailsPanel = document.getElementById('details');
let html = `<div class="node-details">
<h2>${node.name}</h2>`;
// Add avatar for SoundCloud accounts
if (node.type === 'soundcloud' && node.avatar) {
html += `<img src="${node.avatar}" alt="${node.name}">`;
}
// Add metadata based on node type
html += `<div class="metadata">
<h3>${capitalizeFirstLetter(node.type)} Information</h3>`;
switch(node.type) {
case 'soundcloud':
html += `<p><strong>Followers:</strong> ${node.followers.toLocaleString()}</p>
<p><strong>Tracks:</strong> ${node.tracks}</p>
<p><strong>URL:</strong> ${node.url}</p>`;
break;
case 'artist':
html += `<p><strong>Genres:</strong> ${node.genres.join(', ')}</p>
<p><strong>Location:</strong> ${node.location}</p>
<p><strong>Founded:</strong> ${node.founded}</p>`;
break;
case 'group':
html += `<p><strong>Members:</strong> ${node.members}</p>
<p><strong>Location:</strong> ${node.location}</p>
<p><strong>Founded:</strong> ${node.founded}</p>
<p><strong>Focus:</strong> ${node.focus}</p>`;
break;
case 'label':
html += `<p><strong>Artists:</strong> ${node.artists}</p>
<p><strong>Founded:</strong> ${node.founded}</p>
<p><strong>Location:</strong> ${node.location}</p>
<p><strong>Genres:</strong> ${node.genres.join(', ')}</p>`;
break;
}
html += `</div>`;
// Add connections section
html += `<div class="connections">
<h3>Connections (${connectedNodes.length})</h3>`;
if (connectedNodes.length > 0) {
connectedNodes.forEach(connectedNode => {
let iconBg = '';
switch(connectedNode.type) {
case 'soundcloud': iconBg = '#4a90e2'; break;
case 'artist': iconBg = '#90caf9'; break;
case 'group': iconBg = '#ef9a9a'; break;
case 'label': iconBg = '#a5d6a7'; break;
}
html += `<div class="connection-item" onclick="focusNode('${connectedNode.id}')">
<div class="connection-icon" style="background-color: ${iconBg}"></div>
<div>
<strong>${connectedNode.name}</strong><br>
<small>${capitalizeFirstLetter(connectedNode.type)}</small>
</div>
</div>`;
});
} else {
html += `<p>No connections found.</p>`;
}
html += `</div></div>`;
detailsPanel.innerHTML = html;
}
function setupUIControls() {
// Toggle details panel
const detailsPanel = document.getElementById('details');
const toggleButton = document.getElementById('toggle-details');
toggleButton.addEventListener('click', () => {
detailsPanel.classList.toggle('hidden');
if (detailsPanel.classList.contains('hidden')) {
toggleButton.textContent = '‹';
toggleButton.classList.add('hidden');
} else {
toggleButton.textContent = '›';
toggleButton.classList.remove('hidden');
}
});
// Track mouse movement for tooltip
document.addEventListener('mousemove', (event) => {
const tooltip = document.getElementById('tooltip');
if (tooltip.style.display === 'block') {
tooltip.style.left = (event.pageX + 10) + 'px';
tooltip.style.top = (event.pageY + 10) + 'px';
}
});
}
function setupSearch(Graph) {
const searchInput = document.getElementById('search-input');
searchInput.addEventListener('input', () => {
const searchTerm = searchInput.value.toLowerCase();
if (searchTerm.length < 2) return;
const nodes = Graph.graphData().nodes;
const matchedNode = nodes.find(node =>
node.name.toLowerCase().includes(searchTerm)
);
if (matchedNode) {
// Highlight the node temporarily
matchedNode.hovered = true;
// Center on the node
Graph.centerAt(matchedNode.x, matchedNode.y, 1000);
Graph.zoom(1.5, 1000);
// Remove highlight after a delay
setTimeout(() => {
matchedNode.hovered = false;
}, 2000);
}
});
searchInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
const searchTerm = searchInput.value.toLowerCase();
if (searchTerm.length < 2) return;
const nodes = Graph.graphData().nodes;
const matchedNode = nodes.find(node =>
node.name.toLowerCase().includes(searchTerm)
);
if (matchedNode) {
// Simulate a click on the node
handleNodeClick(matchedNode, Graph);
}
}
});
}
function setupZoomControls(Graph) {
document.getElementById('zoom-in').addEventListener('click', () => {
const currentZoom = Graph.zoom();
Graph.zoom(currentZoom * 1.5, 800);
});
document.getElementById('zoom-out').addEventListener('click', () => {
const currentZoom = Graph.zoom();
Graph.zoom(currentZoom / 1.5, 800);
});
document.getElementById('zoom-reset').addEventListener('click', () => {
Graph.centerAt(0, 0, 800);
Graph.zoom(1, 800);
});
}
function applyClusteringForce(alpha) {
const nodes = window.graphInstance.graphData().nodes;
const clusterCenters = {};
// Calculate cluster centers
nodes.forEach(node => {
if (!clusterCenters[node.cluster]) {
clusterCenters[node.cluster] = { x: 0, y: 0, count: 0 };
}
clusterCenters[node.cluster].x += node.x || 0;
clusterCenters[node.cluster].y += node.y || 0;
clusterCenters[node.cluster].count++;
});
// Calculate average positions
Object.keys(clusterCenters).forEach(cluster => {
const center = clusterCenters[cluster];
center.x /= center.count;
center.y /= center.count;
});
// Apply force towards cluster center
const clusteringStrength = 0.1;
nodes.forEach(node => {
if (!node.cluster) return;
const center = clusterCenters[node.cluster];
node.vx += (center.x - node.x) * alpha * clusteringStrength;
node.vy += (center.y - node.y) * alpha * clusteringStrength;
});
}
// Helper function to focus on a node by ID
function focusNode(nodeId) {
const Graph = window.graphInstance;
const node = Graph.graphData().nodes.find(n => n.id === nodeId);
if (node) {
handleNodeClick(node, Graph);
}
}
// Helper function to capitalize first letter
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
</script>
</body>
</html>
Would you like me to explain how this visualization works?