infinite-agents-public/sdg_viz/sdg_viz_1.html

431 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SDG Network Visualization - Force-Directed Graph</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
overflow: hidden;
}
#container {
width: 100vw;
height: 100vh;
position: relative;
}
svg {
width: 100%;
height: 100%;
cursor: grab;
}
svg:active {
cursor: grabbing;
}
.node {
cursor: move;
transition: r 0.3s ease, stroke-width 0.3s ease;
}
.node:hover {
stroke: #fff;
stroke-width: 4px;
}
.link {
stroke: rgba(255, 255, 255, 0.3);
stroke-width: 1.5px;
transition: stroke 0.3s ease, stroke-width 0.3s ease;
}
.link:hover {
stroke: rgba(255, 255, 255, 0.8);
stroke-width: 3px;
}
.node-label {
fill: white;
font-size: 10px;
font-weight: 600;
text-anchor: middle;
pointer-events: none;
text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
#tooltip {
position: absolute;
background: rgba(0, 0, 0, 0.9);
color: white;
padding: 12px 16px;
border-radius: 8px;
font-size: 14px;
pointer-events: none;
opacity: 0;
transition: opacity 0.3s ease;
max-width: 300px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
z-index: 1000;
}
#tooltip.visible {
opacity: 1;
}
.tooltip-title {
font-weight: 700;
font-size: 16px;
margin-bottom: 6px;
color: #4ade80;
}
.tooltip-description {
font-size: 13px;
line-height: 1.5;
color: #e5e7eb;
}
.tooltip-targets {
font-size: 12px;
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid rgba(255, 255, 255, 0.2);
color: #9ca3af;
}
#loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.8);
color: white;
padding: 24px 32px;
border-radius: 12px;
font-size: 18px;
font-weight: 600;
text-align: center;
}
#footer {
position: absolute;
bottom: 16px;
left: 16px;
right: 16px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 16px;
border-radius: 8px;
font-size: 12px;
line-height: 1.6;
backdrop-filter: blur(10px);
}
.footer-title {
font-weight: 700;
font-size: 14px;
margin-bottom: 8px;
color: #4ade80;
}
.footer-section {
margin: 4px 0;
}
.footer-label {
font-weight: 600;
color: #60a5fa;
}
#legend {
position: absolute;
top: 16px;
right: 16px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 16px;
border-radius: 8px;
font-size: 12px;
backdrop-filter: blur(10px);
}
.legend-title {
font-weight: 700;
font-size: 14px;
margin-bottom: 12px;
color: #4ade80;
}
.legend-item {
display: flex;
align-items: center;
margin: 6px 0;
}
.legend-color {
width: 20px;
height: 20px;
border-radius: 50%;
margin-right: 8px;
border: 2px solid rgba(255, 255, 255, 0.5);
}
</style>
</head>
<body>
<div id="container">
<div id="loading">Loading UN SDG Data...</div>
<div id="tooltip"></div>
<div id="legend"></div>
<svg id="graph"></svg>
<div id="footer">
<div class="footer-title">SDG Network Visualization - Iteration 1</div>
<div class="footer-section">
<span class="footer-label">Data Source:</span> UN SDG API - https://unstats.un.org/SDGAPI/v1/sdg/Goal/List?includechildren=true
</div>
<div class="footer-section">
<span class="footer-label">Web Learning Source:</span> https://observablehq.com/@d3/force-directed-graph
</div>
<div class="footer-section">
<span class="footer-label">Techniques Applied:</span> d3.forceSimulation() with forceLink(), forceManyBody(), forceCenter(), forceCollide() | SVG node/link rendering | Drag interactions with dragstarted/dragged/dragended | Tick-based position updates | Zoom/pan behavior
</div>
</div>
</div>
<script>
// Configuration
const width = window.innerWidth;
const height = window.innerHeight;
// Create SVG
const svg = d3.select("#graph")
.attr("viewBox", [0, 0, width, height]);
// Create groups for links and nodes (order matters for z-index)
const linkGroup = svg.append("g").attr("class", "links");
const nodeGroup = svg.append("g").attr("class", "nodes");
const labelGroup = svg.append("g").attr("class", "labels");
// Tooltip
const tooltip = d3.select("#tooltip");
// Color scale for SDG goals
const colorScale = d3.scaleOrdinal(d3.schemeSet3);
// Fetch and process UN SDG data
async function fetchSDGData() {
try {
const response = await fetch('https://unstats.un.org/SDGAPI/v1/sdg/Goal/List?includechildren=true');
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching SDG data:', error);
return null;
}
}
// Transform SDG data into network graph format
function transformToGraph(sdgData) {
if (!sdgData || !Array.isArray(sdgData)) {
return { nodes: [], links: [] };
}
const nodes = sdgData.map(goal => ({
id: goal.code,
title: goal.title,
description: goal.description || goal.title,
targets: goal.targets ? goal.targets.length : 0,
color: goal.colorInfo?.hex || '#333333',
uri: goal.uri
}));
// Create links based on thematic relationships between SDGs
// SDGs are interconnected - we'll create a network based on common themes
const links = [];
const themes = {
poverty: [1, 2, 8, 10],
health: [2, 3, 6],
education: [4, 5, 8, 10],
gender: [5, 8, 10],
water: [6, 14, 15],
energy: [7, 9, 11, 12, 13],
economy: [8, 9, 10, 12],
infrastructure: [9, 11],
inequality: [10, 16],
cities: [11, 12, 13],
climate: [13, 14, 15],
oceans: [14],
land: [15],
peace: [16, 17],
partnerships: [17]
};
// Create links within each theme
Object.values(themes).forEach(themeGoals => {
for (let i = 0; i < themeGoals.length - 1; i++) {
for (let j = i + 1; j < themeGoals.length; j++) {
links.push({
source: themeGoals[i].toString(),
target: themeGoals[j].toString()
});
}
}
});
return { nodes, links };
}
// Initialize and run the visualization
async function init() {
const sdgData = await fetchSDGData();
d3.select("#loading").style("opacity", "0").style("pointer-events", "none");
if (!sdgData) {
alert("Failed to load SDG data. Please check your internet connection.");
return;
}
const graph = transformToGraph(sdgData);
// Create legend
const legend = d3.select("#legend");
legend.html('<div class="legend-title">UN Sustainable Development Goals</div>');
graph.nodes.slice(0, 6).forEach(node => {
const item = legend.append("div").attr("class", "legend-item");
item.append("div")
.attr("class", "legend-color")
.style("background-color", node.color);
item.append("span").text(`Goal ${node.id}: ${node.title.substring(0, 25)}...`);
});
// Apply D3 Force Simulation (learned from Observable)
const simulation = d3.forceSimulation(graph.nodes)
.force("link", d3.forceLink(graph.links)
.id(d => d.id)
.distance(100))
.force("charge", d3.forceManyBody()
.strength(-300))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collide", d3.forceCollide()
.radius(d => Math.max(30, d.targets * 3 + 10)))
.alphaDecay(0.02);
// Create links
const link = linkGroup.selectAll("line")
.data(graph.links)
.join("line")
.attr("class", "link");
// Create nodes
const node = nodeGroup.selectAll("circle")
.data(graph.nodes)
.join("circle")
.attr("class", "node")
.attr("r", d => Math.max(15, d.targets * 2 + 8))
.attr("fill", d => d.color)
.attr("stroke", "#fff")
.attr("stroke-width", 2)
.call(drag(simulation))
.on("mouseover", function(event, d) {
tooltip.classed("visible", true)
.html(`
<div class="tooltip-title">Goal ${d.id}: ${d.title}</div>
<div class="tooltip-description">${d.description}</div>
<div class="tooltip-targets">${d.targets} targets</div>
`)
.style("left", (event.pageX + 15) + "px")
.style("top", (event.pageY - 15) + "px");
})
.on("mouseout", function() {
tooltip.classed("visible", false);
});
// Create labels
const label = labelGroup.selectAll("text")
.data(graph.nodes)
.join("text")
.attr("class", "node-label")
.text(d => d.id);
// Implement drag behavior (learned from Observable)
function drag(simulation) {
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
// Update positions on each tick (learned from Observable)
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
label
.attr("x", d => d.x)
.attr("y", d => d.y + 4);
});
// Add zoom and pan behavior
const zoom = d3.zoom()
.scaleExtent([0.5, 5])
.on("zoom", (event) => {
linkGroup.attr("transform", event.transform);
nodeGroup.attr("transform", event.transform);
labelGroup.attr("transform", event.transform);
});
svg.call(zoom);
// Initial animation
node.attr("r", 0)
.transition()
.duration(800)
.delay((d, i) => i * 50)
.attr("r", d => Math.max(15, d.targets * 2 + 8));
}
// Start the visualization
init();
// Handle window resize
window.addEventListener('resize', () => {
location.reload();
});
</script>
</body>
</html>