JSNetworkX icon indicating copy to clipboard operation
JSNetworkX copied to clipboard

jsnetworkx.js:5 Uncaught TypeError: Cannot read property 'force' of undefined

Open ScientiaEtVeritas opened this issue 8 years ago • 11 comments

Hi, I'm trying to draw the graph, but always get this error: jsnetworkx.js:5 Uncaught TypeError: Cannot read property 'force' of undefined

Even if I try sth. like this: ` G = new jsnx.Graph();

    G.addNode(1);
    G.addNode(2);

    G.addEdge(1,2);


    jsnx.draw(G, {
      element: '#canvas',
      withLabels: false
    });`

Does someone have an idea what's the reason for this?

D3 is included.

Thanks in advance.

ScientiaEtVeritas avatar Jul 26 '16 23:07 ScientiaEtVeritas

Hey, I was experiencing the same issue and downgrading d3 version was the solution for me. Try changing you d3 script to this one:

juhojo avatar Dec 05 '16 17:12 juhojo

Downgrading d3 resulted in a different error for me:

Uncaught TypeError: Cannot read property 'getComputedStyle' of null

I tried implementing ScientiaEtVeritas's example and also some examples from the jsnetworkx.org page.

jeffzemla avatar Mar 04 '17 19:03 jeffzemla

Are you certain that you did pass a correct element to the jsnx.draw()- function? getComputedStyle method is used to

give the values of all the CSS properties of an element after applying the active stylesheets and resolving any basic computation those values may contain. MDN - Mozilla Foundation

so it appears to me that the canvas element you have created in your HTML DOM does not have an id canvas, therefore resulting in the compile error.

I cannot provide more suggestions, because I have not yet received a similar error.

juhojo avatar Mar 05 '17 00:03 juhojo

i m facing same problem...

revosys avatar May 17 '17 11:05 revosys

Downgrading d3 also solved this issue for me

bigbasti avatar May 17 '17 11:05 bigbasti

My code is simple.... but still this is the issue...

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

var G = new jsnx.Graph();
G.addNodesFrom([
    [1, {color: 'red'}],
    [2, {color: 'green'}],
    [3, {color: 'white'}]
]);
 
G.addEdgesFrom([[1,2], [1,3]]);
 
// `jsnx.draw` accept a graph and configuration object
jsnx.draw(G, {
  element: '#canvas',
  withLabels: true,
  nodeStyle: {
      fill: function(d) {
          return d.data.color;
      }
  }
});

</script>

<div id="canvas" class="span9"></div>

Error is Uncaught TypeError: Cannot read property 'getComputedStyle' of null at Array.d3_selectionPrototype.style (d3.v3.js:740) at Object.i [as draw] (jsnetworkx.js:5)

revosys avatar May 17 '17 11:05 revosys

just a wild guess here: shouldn't you declare the div you want to draw in BEFORE drawing on it? (move the div before the script)

bigbasti avatar May 17 '17 11:05 bigbasti

thanks for your reply.... ITS WORKING....:) Thanks

revosys avatar May 17 '17 12:05 revosys

i just tried your code - got the same error - moved the div above the script tag and everything ran fine

bigbasti avatar May 17 '17 12:05 bigbasti

Yes.... Some time over load work can make you behave like idiot...:)

revosys avatar May 17 '17 12:05 revosys

I have the same problem. I can downgrade to d3 v3, but then I get the error: jsnetworkx.js:5 Uncaught Error: D3 requried for draw()

im using <script src="http://d3js.org/d3.v3.min.js"></script>

snipercup avatar Nov 21 '19 10:11 snipercup