当我在同一页面上有两个图表D3.js时出现异常宽度

时间:2016-05-03 07:34:18

标签: javascript d3.js graph force-layout bubble-chart

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

// 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 感谢。

1 个答案:

答案 0 :(得分:1)

基本上你的问题是你正在填补路径,而不仅仅是让它中风。

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

  .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