JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<style>
#my_dataviz {
width: 100%;
height: 100vh;
}
body {
height: 100vh;
margin: 0;
overflow: hidden;
}
.node {
width: 95%;
background: white;
height: 95%;
display: flex;
align-items: center;
justify-content: center;
border: 1px solid;
}
</style>
<title>Graph Visualization with D3.js</title>
<script src="https://d3js.org/d3.v6.min.js"></script>
</head>
<body>
<script>
const data = {
nodes: [
{
id: 1,
depth: 0,
name: "A",
content: '<div class="node">ROOT</div>',
},
{
id: 2,
depth: 1,
name: "B",
content: '<div class="node">NODE</div>',
},
{
id: 3,
depth: 1,
name: "E",
content: '<div class="node">NODE</div>',
},
],
links: [
{
source: 1,
target: 2,
},
{
source: 1,
target: 3,
},
],
};
// set the dimensions and margins of the graph
const margin = { top: 10, right: 30, bottom: 30, left: 40 },
width = document.body.offsetWidth - margin.left - margin.right,
height = document.body.offsetHeight - margin.top - margin.bottom;
const colors = d3.scaleOrdinal(d3.schemeCategory10);
var zoom = d3.zoom().scaleExtent([0.1, 10]).on("zoom", zoomed);
function zoomed(d) {
svg.attr("transform", d.transform);
}
// append the svg object to the body of the page
const svg = d3
.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.call(zoom)
.append("g");
const gLink...