如何在d3v4力模拟中设定重力强度

时间:2017-03-14 15:31:51

标签: d3.js

我在这里读过api,说x.strength和y.strength会设置引力的强度: https://github.com/d3/d3-force/blob/master/README.md#x_strength

但我该怎么申请?

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))

1 个答案:

答案 0 :(得分:9)

我在GitHub上快速搜索了一下:

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

这看起来像正确的语法:

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)

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