簡體   English   中英

調整D3力布局的大小

[英]Resizing D3 force layout

是否可以在d3.js中調整力布局的大小? 例如,我有一個定義的布局

var force = d3.layout.force()
.nodes(documents.nodes)
.linkDistance(0)
.friction(.2)
.size([width, height]);

force.start();

我希望稍后在項目中重新調整大小。 這可能嗎?

不久,在調整大小事件時,您應該更改svg對象的屬性(它也會改變重心)並恢復力計算:

window.onresize = function() {
  width = window.innerWidth, height = window.innerHeight;
  svg.attr('width', width).attr('height', height);
  force.size([width, height]).resume();
};

這里提供d3強制調整大小的示例。

根據文檔 ,力布局的.size()參數僅影響重心和節點的初始分布,而不影響可用空間。 您必須自己強制執行任何約束,例如在tick函數中:

force.on("tick", function() {
    node.attr("cx", function(d) { return Math.min(maxX, Math.max(minX, d.x)); })
        .attr("cy", function(d) { return Math.min(maxY, Math.max(minY, d.y)); });
}

暫無
暫無

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

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