Skip to content

Commit

Permalink
fix: Resolve D3 tree visualization initialization error
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Nov 30, 2024
1 parent 78c8568 commit a32e2f8
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions hugo-site/static/html/eventual_convergence_viz.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ <h1 class="title is-4">Freenet Eventual Convergence Simulation</h1>
return { nodes, links };
}

let randomTreeData = generateRandomTree();
let randomTreeData;
let svg;
let root;

function calculateOptimalParameters(containerWidth, containerHeight) {
const availableArea = containerWidth * containerHeight;
Expand Down Expand Up @@ -159,19 +160,11 @@ <h1 class="title is-4">Freenet Eventual Convergence Simulation</h1>
.value(1) // Each segment has equal weight
.sort(null); // Do not sort, keep original order

let root = d3
.stratify()
.id((d) => d.id)
.parentId((d) => {
const link = randomTreeData.links.find((link) => link.target.id === d.id);
return link ? link.source.id : null;
})(randomTreeData.nodes);

// Initialize tree layout
const treeLayout = d3.tree().nodeSize([
window.innerWidth < 768 ? 100 : 60, // Wider spacing on mobile
window.innerWidth < 768 ? 140 : 100 // More vertical space on mobile
]);
treeLayout(root);

function handleResize() {
({ width, height } = getContainerDimensions());
Expand Down Expand Up @@ -216,6 +209,11 @@ <h1 class="title is-4">Freenet Eventual Convergence Simulation</h1>
// Remove existing SVG if it exists
d3.select("svg").remove();

// Generate new tree data with current parameters
const { width, height } = getContainerDimensions();
const params = calculateOptimalParameters(width, height);
randomTreeData = generateRandomTree(params.nodeCount);

// Create hierarchy from randomTreeData
root = d3
.stratify()
Expand Down

0 comments on commit a32e2f8

Please sign in to comment.