如何删除d3.js中图表之间的重叠

时间:2016-01-13 02:32:33

标签: d3.js

我正在尝试使用d3.js库绘制图表并且效果很好。 使用输入值(force.csv),我绘制了2个图。

问题:

  1. 我可以在屏幕上绘制2个图表而不重叠吗? 默认情况下,这些是重叠的。我不确定我应该修改哪些代码。
  2. 如何删除动画?每当我刷新HTML页面时,它都在移动。
  3. 我对d3很新,所以请理解我的基本问题。

    force.csv

    source,target,type
    first_1,second_1,licensing
    second_1,third_1,licensing
    first_2,second_2,resolved
    

    的test.html

    <!DOCTYPE html>
    <meta charset="utf-8">
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <style>
    
    .link {
      fill: none;
      stroke: #666;
      stroke-width: 2px;
    }
    
    #licensing {
      fill: green;
    }
    
    .link.licensing {
      stroke: green;
    }
    
    .link.resolved {
      stroke-dasharray: 0,2 1;
    }
    
    circle {
      fill: #ccc;
      stroke: #333;
      stroke-width: 3px;
    }
    
    text {
      font: 15px sans-serif;
      pointer-events: none;
      text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
    }
    
    </style>
    <body>
    <script>
    
    // get the data
    d3.csv("force.csv", function(error, links) {
    
    var nodes = {};
    
    // Compute the distinct nodes from the links.
    links.forEach(function(link) {
      link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
      link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
    });
    
    var width = 960,
        height = 500;
    
    var force = d3.layout.force()
        .nodes(d3.values(nodes))
        .links(links)
        .size([width, height])
        .linkDistance(300)
        .charge(-300)
        .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", 6)
        .attr("markerHeight", 6)
        .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", 6)
        .call(force.drag);
    
    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 + ")";
    }
    });
    
    </script>
    </body>
    </html>
    

0 个答案:

没有答案