我的数据需要在网络图中可视化。我正在尝试使用D3,我设法得到静态图。
有人可以建议我如何创建图表,当点击节点A时,它将扩展到与节点A相关的其他节点。
这是我当前的代码(从D3库中的示例编辑):
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
.node circle {
fill: #ccc;
stroke: #fff;
stroke-width: 1.5px;
}
text {
font: 10px sans-serif;
pointer-events: none;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
// http://blog.thomsonreuters.com/index.php/mobile-patent-suits-graphic-of-the-day/
var links = [
{source: "Plant B", target: "Round shape"},
{source: "Library", target: "Plant C"},
{source: "Library", target: "Plant A"},
{source: "Plant C", target: "Sandy"},
{source: "Round shape", target: "Plant A"},
{source: "Plant C", target: "Long shape"},
{source: "Plant A", target: "Round shape"},
{source: "Plant D", target: "Lake"},
{source: "Plant D", target: "Round shape"},
{source: "Plant A", target: "Library"},
{source: "Plant B", target: "Library"},
{source: "Plant E", target: "Lake"},
{source: "Lake", target: "Sandy"},
{source: "Plant E", target: "Long shape"},
{source: "Library", target: "Wet soil"},
{source: "Library", target: "Plant B"}
];
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});
var width = 960,
height = 500;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(80)
.charge(-300)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var link = svg.selectAll(".link")
.data(force.links())
.enter().append("line")
.attr("class", "link");
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.call(force.drag);
node.append("circle")
.attr("r", 8);
node.append("text")
.attr("x", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
function tick() {
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 + ")"; });
}
function mouseover() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 16);
}
function mouseout() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 8);
}
</script>
我的数据应该是来自查询,所以有什么方法可以将该节点链接到我的查询页面吗?