d3.js 关于力引导图的link.js的简单阅读

初始化函数
function initialize() {
    if (!nodes) return;
    var i,
        n = nodes.length,
        m = links.length,
        nodeById = map(nodes, id),
        link;
    for (i = 0, count = new Array(n); i < m; ++i) {
      link = links[i], link.index = i;
      if (typeof link.source !== "object") link.source = find(nodeById, link.source);
      if (typeof link.target !== "object") link.target = find(nodeById, link.target);
      count[link.source.index] = (count[link.source.index] || 0) + 1;
      count[link.target.index] = (count[link.target.index] || 0) + 1;
    }

    for (i = 0, bias = new Array(m); i < m; ++i) {
      link = links[i], bias[i] = count[link.source.index] / (count[link.source.index] + count[link.target.index]);
    }

    strengths = new Array(m), initializeStrength();
    distances = new Array(m), initializeDistance();
  }
复制代码

计算出来两个值strengths和distances 有一个过渡值count distances的值始终固定。

force函数
function force(alpha) {
    for (var k = 0, n = links.length; k < iterations; ++k) {
      for (var i = 0, link, source, target, x, y, l, b; i < n; ++i) {
        link = links[i], source = link.source, target = link.target;
        x = target.x + target.vx - source.x - source.vx || jiggle();
        y = target.y + target.vy - source.y - source.vy || jiggle();
        l = Math.sqrt(x * x + y * y);
        l = (l - distances[i]) / l * alpha * strengths[i];
        x *= l, y *= l;
        target.vx -= x * (b = bias[i]);
        target.vy -= y * b;
        source.vx += x * (b = 1 - b);
        source.vy += y * b;
      }
    }
  }
复制代码

上面的代码遍历了所有边,对于每一条边,都计算这条边的两个结点的受力,关键代码是下面一行: l = (l - distances[i]) / l * alpha * strengths[i]; 其中l为两结点间的距离,distances为配置的边的长度,alpha与strengths也基本为常量,所以当距离与配置的边长相等时受力为零,否则都会出现引力或斥力,这就是所谓的弹簧力,遵守胡克定律。一句说就是你配置了一个弹簧的长度,把连接两个结点的边想象成弹簧,当拉得太长时会有一个力把它们拉拢,当太短时弹簧被压缩会将它们推开。其它的力基本也同理,就是运用一些物理上的受力公式进行计算。

测试数据
{
    "nodes": [
      {"id": "name0", "group": 1},
      {"id": "name1", "group": 1},
      {"id": "name2", "group": 1},
      {"id": "name3", "group": 1},
      {"id": "name4", "group": 1},
      {"id": "name5", "group": 1},
      {"id": "name6", "group": 1},
      {"id": "name7", "group": 1}
    ],
    "links": [
      {"source": "name0", "target": "name1", "value": 1},
      {"source": "name0", "target": "name2", "value": 1},
      {"source": "name0", "target": "name3", "value": 1},
      {"source": "name0", "target": "name4", "value": 1},
      {"source": "name1", "target": "name5", "value": 1},
      {"source": "name1", "target": "name6", "value": 1},
      {"source": "name5", "target": "name7", "value": 1}
    ]
  }
复制代码
打印的

count 计算方法在初始化里。

strengths 的计算方法

function defaultStrength(link) {
    return 1 / Math.min(count[link.source.index], count[link.target.index]);
  }
复制代码

distances

distance = constant(30)
export default function(x) {
  return function() {
    return x;
  };
}

复制代码

结果展示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下绘制导向的方法。d3.js是一个非常流行的JavaScript库,它提供了丰富的绘功能,包括绘制导向。 首先,您需要定义一个包含节点和链接信息的数据集。节点可以是一个对象,包含其名称和其他属性,链接可以是一个具有源节点和目标节点的对象。例如: ```javascript var nodes = [ { name: "A", group: 1 }, { name: "B", group: 2 }, { name: "C", group: 2 }, { name: "D", group: 3 } ]; var links = [ { source: "A", target: "B", value: 1 }, { source: "B", target: "C", value: 2 }, { source: "C", target: "D", value: 3 } ]; ``` 然后,您需要定义一个SVG容器来容纳您的形: ```javascript var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); ``` 接下来,您可以使用d3.forceSimulation()方法来创建一个导向的仿真器,并使用d3.forceLink()和d3.forceManyBody()方法来定义链接和节点之间的。 ```javascript var simulation = d3.forceSimulation() .force("link", d3.forceLink().id(function(d) { return d.id; })) .force("charge", d3.forceManyBody()) .force("center", d3.forceCenter(width / 2, height / 2)); ``` 最后,您可以使用d3.select()和d3.selectAll()方法来选择和绘制节点和链接。 ```javascript var link = svg.append("g") .attr("class", "links") .selectAll("line") .data(links) .enter().append("line") .attr("stroke-width", function(d) { return Math.sqrt(d.value); }); var node = svg.append("g") .attr("class", "nodes") .selectAll("circle") .data(nodes) .enter().append("circle") .attr("r", 5) .attr("fill", function(d) { return color(d.group); }) .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)); ``` 以上是一个简单导向的绘制过程,如果您需要更多高级的控制,您可以查看d3.js的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值