D3 sample code is a struggle

I’m trying to get the sample code for a force directed graph working.

The difference is I have added the JSON directly to the code. That seems to be enough to break it.

I think the problem starts here:

 var link = svg.append("g")
    .attr("class", "links")
    .selectAll("line")
    .data(graph.links)
    .enter().append("line")
    .attr("stroke-width", function(d) { return Math.sqrt(d.value); });

The example shows:

var svg = d3.select("svg")

But you have:

const svg = document.querySelector("svg")
1 Like