Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the gravity strength in d3v4 force simulation

Tags:

d3.js

I've read the api here that says x.strength and y.strength will set the strength of gravity: https://github.com/d3/d3-force/blob/master/README.md#x_strength

But I how to apply it?

simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function (d) {
        return d.id;
    }).distance(10))
    .force("charge", d3.forceManyBody().strength(-15))
    .force("center", d3.forceCenter(width * 0.5, height / 2))
like image 259
Frazer Kirkman Avatar asked Mar 14 '17 15:03

Frazer Kirkman


1 Answers

I did a quick search on GitHub:

https://github.com/search?l=JavaScript&q=forceX+strength&ref=searchresults&type=Code&utf8=%E2%9C%93

This looks like the correct syntax:

const forceX = d3.forceX(width / 2).strength(0.015)
const forceY = d3.forceY(height / 2).strength(0.015)

const simulation = d3.forceSimulation()
  .force('x', forceX)
  .force('y',  forceY)

(from https://github.com/ivarmlee11/d3quake/blob/5f332d1d82d25b21097af78b4d1740bb092b4bb9/public/js/app.js#L17)

like image 87
paradite Avatar answered Nov 06 '22 06:11

paradite