在d3.js強制佈局中,給出重力值會使佈局循環。如何讓d3.js強制佈局重力矩形?
不過,我想使力佈局的矩形,而節點有一個負電荷和甚至距離。 (如上所述)
有什麼辦法可以讓力佈局成矩形嗎?
我可以通過修改滴答功能來實現嗎?
這裏是我的代碼:
var background = d3.select('.background');
var width = background.node().getBoundingClientRect().width,
\t height = background.node().getBoundingClientRect().height;
var nodes = d3.range(50).map(function(d, i) {
\t \t return {
\t \t text: "Hello"
\t \t };
\t });
var messages = background.selectAll('.message')
\t .data(nodes)
\t .enter()
\t \t .append('div')
\t \t .attr('class', 'message')
\t \t .text(d => d.text)
\t \t .each(function(d, i) {
\t \t \t d.width = this.getBoundingClientRect().width;
\t \t \t d.height = this.getBoundingClientRect().height;
\t \t });
var force = d3.layout.force()
\t .gravity(1/88)
\t .charge(-50)
\t .nodes(nodes)
\t .size([width, height]);
messages.call(force.drag);
force.on('tick', function(e) {
\t messages.each(d => {
\t \t d.x = Math.max(0, Math.min(width - d.width, d.x));
\t \t d.y = Math.max(0, Math.min(height - d.height, d.y));
\t });
\t
\t messages.style('left', d => `${d.x}px`)
\t \t .style('top', d => `${d.y}px`);
});
force.start();
body {
padding: 0;
margin: 0;
}
.background {
width: 100%;
height: 100vh;
border: 1px solid #007aff;
}
.message {
display: inline-block;
font-family: sans-serif;
border-radius: 100vh;
color: white;
padding: 10px;
background-color: #007aff;
position: absolute;
}
.boundary {
display: inline-block;
width: 10px;
height: 10px;
background-color: red;
position: absolute;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<body>
<div class="background">
</div>
</body>
是的,有。是的,它涉及修改滴答功能。然而,這裏沒有人會寫代碼來展示如何去做(我的意思是我不會),在這種情況下沒有代碼的答案是沒有意義的。因此,發佈**您的代碼**創建的力量,與滴答功能,所以我們只需要修改它。 –
@GerardoFurtado哦,我明白了。我贊同你。我用我的代碼更新了我的帖子!謝謝。 – Stleamist