簡體   English   中英

D3js沖突未更新

[英]D3js collision not updating

我正在嘗試使用d3js創建動畫氣泡圖。 除了一件小事情,它似乎正在起作用:當我為節點的大小設置動畫時,它們開始重疊。

我和我的同事整天都在工作,但我們似乎缺少或忽略了一些東西。

我們的html / js:

.node {
  stroke: #fff;
  stroke-width: 1.5px;
}

.link {
  stroke: #999;
  stroke-opacity: .6;
}

</style>
<body>

<div class="buttons">
  <button id="twelve">2012</button>
</div>

<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<script>

var width = 960,
    height = 500;

var force = d3.layout.force()
    .charge(0.5)
    .gravity(0.2)
    .distance(500)
    .size([width, height]);

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

d3.json("miserables.json", function(error, graph) {
  force
      .nodes(graph.nodes)
      .distance( function(d){ return d.radius})
      .start();

var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", function(d) { return d.radius; })
      .style("fill", function(d) { return d.color; })
      .call(force.drag);

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

  force.on("tick", function(e) {
    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; })

        var nodes = graph.nodes
        var q = d3.geom.quadtree(nodes),
      i = 0,
      n = nodes.length;

  while (++i < n) {
    q.visit(collide(nodes[i]));
  }

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


  $(document).ready(function(){

        $('.buttons button').on('click', function(){
            var showYear = $(this).attr('id');
            node.transition()
            .duration(350)
            .attr("r", function(d){ return d[showYear]["radius"]})
            .style("fill", function(d){ return d[showYear]["color"]})
        });
    })



});


function collide(node) {

  var r = node.radius + 16,
      nx1 = node.x - r,
      nx2 = node.x + r,
      ny1 = node.y - r,
      ny2 = node.y + r;
  return function(quad, x1, y1, x2, y2) {
    if (quad.point && (quad.point !== node)) {
      var x = node.x - quad.point.x,
          y = node.y - quad.point.y,
          l = Math.sqrt(x * x + y * y),
          r = node.radius + quad.point.radius;
      if (l < r) {
        l = (l - r) / l * .5;
        node.x -= x *= l;
        node.y -= y *= l;
        quad.point.x += x;
        quad.point.y += y;
      }
    }
    return x1 > nx2
        || x2 < nx1
        || y1 > ny2
        || y2 < ny1;
  };

}



</script>

</body>

這是我們的json文件:

    {
      "nodes":[
        {
            "name":     "Country One",
            "radius":   40,
            "color":    "#ff0000",
            "twelve" : {
                "radius" : 60,
                "color" : "#0ff000"
                }
            },
            {
            "name":     "Country Two",
            "radius":   40,
            "color":    "#ffff00",
            "twelve" : {
                "radius" : 60,
                "color" : "#0ff000"
                }
            }

      ]
    }

任何幫助將不勝感激。

碰撞檢測是基於節點的radius屬性完成的,但是我想您只對SVG圓的半徑設置了動畫,而不對它們后面的數據的radius屬性設置了動畫。

建議的解決方案:

node
    .each( function( d ) {
        d.radius = d[showYear]["radius"];
    })
    .transition()
    .duration(350)
    .attr("r", function(d){ return d.radius})
    .style("fill", function(d){ return d[showYear]["color"]})

我認為還有些奇怪,但可能沒問題:在創建時有一個d.radius

.attr("r", function(d) { return d.radius; })

但后來變成d[showYear]["radius"]; 請記住, d對象僅保存一個節點的數據,而不是全部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM