当我在同一页上有两个图D3.js时,宽度异常

Abnormal width when I have two graph D3.js on the same page

本文关键字:js D3 两个 异常 一页      更新时间:2023-09-26

当同一页上有两个图时,我遇到了一个问题,第一个图的大小正常,但由固定数据填充的第二个图太大。这两张图叠加在一起。奇怪的是,如果我使用第二张图的代码,它在我的本地网站测试中有效。这是第二张图的代码:

// http://blog.thomsonreuters.com/index.php/mobile-patent-suits-graphic-of-the-day/
    var links = [
        {"source":"TEST111","target":"TEST222","level":"1","life":"1","test":"1","type":"licensing"},
        {"source":"TEST222","target":"TEST3333","level":"2","life":"2","test":"2","type":"licensing"},
        {"source":"TEST3333","target":"TEST4444","level":"3","life":"3","test":"3","type":"licensing"}
    ];
    var nodes = {};
    // Compute the distinct nodes from the links.
    links.forEach(function(link) {
        link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, level:link.level, life:link.life});
        link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, level:link.level, life:link.life});
    });
    var width = 960,
            height = 500;
    var force = d3.layout.force()
            .nodes(d3.values(nodes))
            .links(links)
            .size([width, height])
            .linkDistance(200)
            .charge(-400)
            .on("tick", tick)
            .start();
    var svg = d3.select("body").append("svg")
            .attr("width", width)
            .attr("height", height);
    // Per-type markers, as they don't inherit styles.
    svg.append("defs").selectAll("marker")
            .data(["suit", "licensing", "resolved"])
            .enter().append("marker")
            .attr("id", function(d) { return d; })
            .attr("viewBox", "0 -5 10 10")
            .attr("refX", 15)
            .attr("refY", -1.5)
            .attr("markerWidth", 20)
            .attr("markerHeight", 20)
            .attr("orient", "auto")
            .append("path")
            .attr("d", "M0,-5L10,0L0,5");
    var path = svg.append("g").selectAll("path")
            .data(force.links())
            .enter().append("path")
            .attr("class", function(d) { return "link " + d.type; })
            .attr("marker-end", function(d) { return "url(#" + d.type + ")"; });
    var circle = svg.append("g").selectAll("circle")
            .data(force.nodes())
            .enter().append("circle")
            .attr("r", 20)
            .call(force.drag)
            .style("fill","red")
            .on("click", click);
    function click(d){

    }
    var text = svg.append("g").selectAll("text")
            .data(force.nodes())
            .enter().append("text")
            .attr("x", 8)
            .attr("y", ".31em")
            .text(function(d) { return d.name; });
    // Use elliptical arc path segments to doubly-encode directionality.
    function tick() {
        path.attr("d", linkArc);
        circle.attr("transform", transform);
        text.attr("transform", transform)
    }
    function linkArc(d) {
        var dx = d.target.x - d.source.x,
                dy = d.target.y - d.source.y,
                dr = Math.sqrt(dx * dx + dy * dy);
        return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
    }
    function transform(d) {
        return "translate(" + d.x + "," + d.y + ")";
    }

此外,在Pluker上,两张图是双层的,但这不是问题所在(它在本地工作)。如果需要,可以删除数据加载(第34行)。这是一个在线Plunker来查看问题:https://plnkr.co/edit/ewoi6wao97tXAKr1QxIm?p=preview谢谢

基本上,您的问题是填充路径,而不仅仅是笔划。

因此,当您创建路径时,只需添加以下内容:

  .style("fill","none").style("stroke","red")

所以现在看起来是:

  var path = svg.append("g").selectAll("path")
            .data(force.links())
            .enter().append("path")
            .attr("class", function(d) { return "link " + d.type; })
            .style("fill","none").style("stroke","red")
            .attr("marker-end", function(d) { return "url(#" + d.type + ")"; });

更新plnkr:https://plnkr.co/edit/JgHwC4zyolj0hsg8ib4w?p=preview