静力定向图链接长度不正确

问题描述 投票:0回答:1

我正在尝试创建静态有力有向图。一个不带任何动画的加载。这是我要模仿的内容:http://bl.ocks.org/mbostock/1667139

我有以下D3图:

var width = $("#theVizness").width(),
    height = $("#theVizness").height();

var color = d3.scale.ordinal().range(["#ff0000", "#fff000", "#ff4900"]);

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

var svg = d3.select("#theVizness").append("svg")
    .attr("width", width)
    .attr("height", height);

var loading = svg.append("text")
    .attr("class", "loading")
    .attr("x", width / 2)
    .attr("y", height / 2)
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .text("Loading...");

d3.json("https://dl.dropboxusercontent.com/u/5772230/ForceDirectData.json", function (error, json) {
    var nodes = json.nodes;
    force.nodes(nodes)
        .links(json.links)
        .linkDistance(function (d) {
            return d.value * 1.5;
        })
        .friction(0.4);

    var link = svg.selectAll(".link")
        .data(json.links)
        .enter().append("line")
        .attr("class", "link")              
        .style("stroke-width", 1);

        var files = svg.selectAll(".file")
        .data(json.nodes)
        .enter().append("circle")
        .attr("class", "file")
        .attr("r", 10)
        .attr("fill", function (d) {
            return color(d.colorGroup);
        });
    var totalNodes = files[0].length;

    files.append("title")
        .text(function (d) { return d.name; });

    force.start();
    for (var i = totalNodes * totalNodes; i > 0; --i) force.tick();
    force.stop();

    nodes[0].x = width / 2;
    nodes[0].y = height / 2;

    link.attr("x1", function (d) { return d.source.x; })
        .attr("y1", function (d) { return d.source.y; })
        .attr("x2", function (d) { return d.target.x; })
        .attr("y2", function (d) { return d.target.y; });

    files.attr("cx", function (d) { return d.x; })
        .attr("cy", function (d) { return d.y; })
        .attr("class", function(d){
            var classString = "file"

            if (d.index === 0) classString += " rootFile";

            return classString;
        })
        .attr("r", function(d){
            var radius = 10;

            if (d.index === 0) radius = radius * 2;

            return radius;
        });

    loading.remove();
});

这是我的数据:https://dl.dropboxusercontent.com/u/5772230/ForceDirectData.json

{
    "nodes":[
      {"name":"File1.exe","colorGroup":0},
      {"name":"File2.exe","colorGroup":0},
      {"name":"File3.exe","colorGroup":0},
      {"name":"File4.exe","colorGroup":0},
      {"name":"File5.exe","colorGroup":0},
      {"name":"File6.exe","colorGroup":0},
      {"name":"File7.exe","colorGroup":0},
      {"name":"File8.exe","colorGroup":0},
      {"name":"File8.exe","colorGroup":0},
      {"name":"File9.exe","colorGroup":0}
    ],
    "links":[
      {"source":1,"target":0,"value":10},
      {"source":2,"target":0,"value":35},
      {"source":3,"target":0,"value":50},
      {"source":4,"target":0,"value":50},
      {"source":5,"target":0,"value":65},
      {"source":6,"target":0,"value":65},
      {"source":7,"target":0,"value":81},
      {"source":8,"target":0,"value":98},
      {"source":9,"target":0,"value":100}
    ]
}

Fiddle

根据我对bl.ocks页面的了解,此图在tick方法上运行了一定次数。但是我的问题是节点之间的链接长度与JSON文件中的链接不成比例。

我选择了静态图形,因为我不想像standard graph中那样使图形成为动画。

为什么我到节点的链接也没有正确按比例匹配我的JSON文件?

javascript json d3.js force-layout
1个回答
1
投票

我不明白你的问题。

这是您的意思吗?

var width = $("#theVizness").width(),
    height = $("#theVizness").height();

var color = d3.scale.ordinal().range(["#ff0000", "#fff000", "#ff4900"]);

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

var svg = d3.select("#theVizness").append("svg")
    .attr("width", width)
    .attr("height", height);

var loading = svg.append("text")
    .attr("class", "loading")
    .attr("x", width / 2)
    .attr("y", height / 2)
    .attr("dy", ".35em")
    .style("text-anchor", "middle")
    .text("Loading...");

d3.json("https://dl.dropboxusercontent.com/u/5772230/ForceDirectData.json", function (error, json) {
    var nodes = json.nodes;
    force.nodes(nodes)
        .links(json.links)
        .linkDistance(function (d) {
            return d.value * 1.5;
        })
        .friction(0.4);

    var link = svg.selectAll(".link")
        .data(json.links)
        .enter().append("line")
        .attr("class", "link")              
        .style("stroke-width", 1);

        var files = svg.selectAll(".file")
        .data(json.nodes)
        .enter().append("circle")
        .attr("class", "file")
        .attr("r", 10)
        .attr("fill", function (d) {
            return color(d.colorGroup);
        });
    var totalNodes = files[0].length;

    files.append("title")
        .text(function (d) { return d.name; });

    force.start();
    for (var i = totalNodes * totalNodes; i > 0; --i) force.tick();


    nodes[0].x = width / 2;
    nodes[0].y = height / 2;

    link.attr("x1", function (d) { return d.source.x; })
        .attr("y1", function (d) { return d.source.y; })
        .attr("x2", function (d) { return d.target.x; })
        .attr("y2", function (d) { return d.target.y; });

    files.attr("cx", function (d) { return d.x; })
        .attr("cy", function (d) { return d.y; })
        .attr("class", function(d){
            var classString = "file"

            if (d.index === 0) classString += " rootFile";

            return classString;
        })
        .attr("r", function(d){
            var radius = 10;

            if (d.index === 0) radius = radius * 2;

            return radius;
        });
    force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    files.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });

    loading.remove();

  }); 

JSFiddle

© www.soinside.com 2019 - 2024. All rights reserved.