【发布时间】:2018-05-17 09:24:12
【问题描述】:
最新代码尝试:
var node = svg.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", radius - .75)
.style("fill", function(d) { return fill(d.group); })
.style("stroke", function(d) { return d3.rgb(fill(d.group)).darker(); })
.call(force.drag);
var label = svg.selectAll("text")
.data(graph.nodes)
.enter()
.append("text")
.text(function(d){return d.id; });
.style("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "red");
})
force
.nodes(graph.nodes)
.links(graph.links)
.on("tick", tick)
.start();
function tick() {
node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
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; });
label.attr("x", function(d){ return d.x; })
.attr("y", function (d) {return d.y });
大家好,
我是 D3 的新手,到目前为止已经花了将近两天的时间来解决这个问题!
我正在尝试将节点标签添加到 https://bl.ocks.org/mbostock/1129492 的版本中
我已经从我能找到的每个论坛回复中复制/粘贴了 sn-ps 代码,它要么破坏了我的图表(执行时出现空白屏幕),要么没有效果(很多漂亮的彩色圆圈,但没有标签)。我已经能够成功地将标签添加到另一个力图,但是那个没有边界,边界是我想要的一个关键特性。在这个阶段,我没有对 Bostock 提供的 graph.json 文件进行任何更改。我只是想让它显示节点文本标签。
谁能告诉我我需要写什么代码来显示标签,以及在哪里以及如何插入它?我决心不放弃这个!
这是我的代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
circle {
stroke-width: 1.5px;
}
line {
stroke: #999;
}
text {
font: 12px "open sans";
fill: #fff;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var width = 960,
height = 500,
radius = 6;
var fill = d3.scaleOrdinal()
.range(["#a02a65","#2aa02a","#2aa0a0","#a0652a","#a02a2a","#2a2aa0","#65a02a","#a0a02a"])
var simulation = d3.forceSimulation()
.velocityDecay(0.1)
.force("x", d3.forceX(width / 2).strength(.05))
.force("y", d3.forceY(height / 2).strength(.05))
.force("charge", d3.forceManyBody().strength(-240))
.force("link", d3.forceLink().distance(50).strength(1));
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("https://[URL HERE]/graph.json", function(error, graph) {
if (error) throw error;
var link = svg.selectAll("line")
.data(graph.links)
.enter().append("line");
var node = svg.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
.attr("r", radius - .75)
.style("fill", function(d) { return fill(d.group); })
.style("stroke", function(d) { return d3.rgb(fill(d.group)).darker(); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var labels = node.append("text")
.text(function(d) {
return d.id;
})
.attr('x', 6)
.attr('y', 3);
node.append("title")
.text(function(d) { return d.id; });simulation
.nodes(graph.nodes)
.on("tick", tick);
simulation.force('link')
.links(graph.links);
function tick() {
node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
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; });
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>
【问题讨论】:
-
变量节点似乎是选择带有标题的圆形元素,但通常不会呈现标题 (developer.mozilla.org/en-US/docs/Web/SVG/Element/title)。您可能想要重复循环数据连接,但只针对文本元素,或者为每个节点创建组元素,然后同时附加一个圆和一个标签。
-
嘿约翰!非常感谢您的回复。这一切听起来都很棒,但我不知道如何实现你在代码方面的建议。你有没有机会用代码 sn-p/s 为我指明正确的方向?再次感谢!
-
谢谢!因此,我研究了链接并进行了另一次尝试,但只是没有得到它 - 我正在学习 javascript 和 D3,但我的(缺乏)经验的程度正在显现。我有机会获得一些代码帮助吗?这是我的最新消息:
-
[在原始问题的顶部]——再次感谢(老实说!)
标签: javascript d3.js data-visualization graph-visualization