javascriptd3.jsjsnetworkx

TypeError: undefined is not an object (evaluating 'M.layout.force')


I'm new to JSNetworkX. I created a graph using the examples from jsnetworkx.org but I can only log it, I can't display it.

So this is index.html file:

<html>
<head>
  <script src="https://d3js.org/d3.v4.js"></script>
  <script src="jsnetworkx.js"></script>
</head>

<body>
  <script src = "script.js"></script>
</body>

</html>

And this the code written in script.js:

var G = jsnx.completeGraph(6);
console.log(G.nodes());
jsnx.draw(G, {
    element: 'body',
    weighted: true,
    edgeStyle: {
        'stroke-width': 10
    }
});

This is the output: [0, 1, 2, 3, 4, 5]

And this is the error i get:

JSNetworkX error - TypeError: undefined is not an object (evaluating 'M.layout.force')


Solution

  • The problem was including the latest version of D3.js.

    Changing

    <script src="https://d3js.org/d3.v4.js"></script>

    to

    <script src="https://d3js.org/d3.v3.js"></script>

    will fix it.