Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: d3.forceSimulation().size is not a function (d3 v4)

Tags:

d3.js

I'm just starting with d3.js and have copied the example for d3.js v3 and trying to adapt it for v4. I have the following part of code:

var width = 960,
  height = 500,
  fill = d3.schemeCategory20;

// another code

var force = d3.forceSimulation()
  .size([width, height]) // the error occurred at this row 
  .nodes([{}])
  .linkDistance(50)
  .charge(-200)
  .on("tick", tick);

d3.forceSimulation() is an operator of v4, so my question is: "How should I use all of the following operators with new d3.forceSimulation() syntax of version 4 of d3.js"?

Thanks in advance

like image 588
Commercial Suicide Avatar asked Jul 18 '26 06:07

Commercial Suicide


2 Answers

Simply because size() is not a simulation object method see the docs for more info

like image 175
user10089632 Avatar answered Jul 23 '26 05:07

user10089632


As per this issue, you can try to use the forceX/Y forces as below:

var force = d3.forceSimulation()
  .size([width, height]) // the error occurred at this row 
  .nodes([{}])
  .linkDistance(50)
  .charge(-200)
  .force("x", d3.forceX([width/2]).strength(0.01))
  .force("y", d3.forceY([height/2]).strength(0.01))
  .on("tick", tick);

You can play with the strength parameter to control how much spread out your graph is.

like image 21
yml33t Avatar answered Jul 23 '26 05:07

yml33t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!