【发布时间】:2018-03-15 14:44:47
【问题描述】:
所以我正在使用 d3 网络图,但不知道如何在我的节点和链接之间获得更大的间距。 margin 和 padding 无效,它们是 css 中主要的间距功能。 jsfiddle 是here。代码如下:
<script>
var network = {
"nodes":[
{"name":"Sensor Operater","group":1},
{"name":"Linguist","group":2},
{"name":"Report Writer","group":3},
{"name":"Mission Manager","group":4},
{"name":"Shift Supervisor","group":5},
{"name":"Geo-location","group":6},
{"name":"COMINT (Internal)","group":7},
{"name":"COMINT (External)","group":8},
{"name":"ELINT","group":9}
],
"links":[
{"source":6,"target":0,"weight":1},
{"source":6,"target":1,"weight":1},
{"source":6,"target":2,"weight":1},
{"source":6,"target":3,"weight":1},
{"source":6,"target":4,"weight":1},
{"source":7,"target":2,"weight":1},
{"source":7,"target":3,"weight":1},
{"source":5,"target":0,"weight":1},
{"source":5,"target":2,"weight":1},
{"source":8,"target":2,"weight":1}
]
}
var width = 960,
height = 500
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(0.05)
.distance(100)
.charge(-100)
.size([width, height]);
force
.nodes(network.nodes)
.links(network.links)
.start();
var link = svg.selectAll(".link")
.data(network.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.weight); });
var node = svg.selectAll(".node")
.data(network.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r","5");
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
</script>
<style>
.link {
stroke: #ccc;
margin:100px;
}
.node text {
stroke:#333;
cursor:pointer;
margin:100px;
}
.node circle{
stroke: steelblue;
stroke-width:3px;
fill:#555;
margin:100px;
}
</style>
【问题讨论】: