我想可视化包含节点和json文件中的链接的网络数据。我正在寻找如何利用D3。我没有尝试过D3,但可视化网络数据似乎很不错。
很多使用D3进行数据可视化的示例,但我观察到json文件示例中的链接始终基于源和目标,或者基于索引的节点。...
如果我的json文件没有源密钥和目标密钥,而是该密钥使用了源密钥和目标密钥或json节点,并且仅链接了内容的值...我仍然可以使用D3 ..?
我希望我可以编辑D3来跟踪我的数据,或者反之亦然。..我不确定如何将json数据放入D3结构中。我已经阅读了D3文档和一些示例,但是却迷失了方向……赞赏的人可以给我进一步的建议。谢谢
请找到我拥有的数据json的示例
SAMPLE1(仅节点和链接的值):
{"nodes":["ser1","ser2","ser3","ser4","ser5"],"links":[["ser1","ser3",10],
["ser1","ser5",30],["ser2","ser3",11],["ser3","ser4",10],
["ser3","ser5",20],["ser2","ser5",30]]}
{
"nodes": [
"ser1",
"ser2",
"ser3",
"ser4",
"ser5"
],
"links": [
[
"ser1",
"ser3",
10
],
[
"ser1",
"ser5",
30
],
[
"ser2",
"ser3",
11
],
[
"ser3",
"ser4",
10
],
[
"ser3",
"ser5",
20
],
[
"ser2",
"ser5",
30
]
]
}
SAMPLE2(节点和链接的键和值:
{"nodes":[{"name":"ser1"},{"name":"ser2"},{"name":"ser3"},{"name":"ser4"},
{"name":"ser5"}],"links":[{"source":"ser1","dest":"ser3","value":"10"},
{"source":"ser1","dest":"ser5","value":"10"},
{"source":"ser2","dest":"ser4","value":"30"},
{"source":"ser3","dest":"ser4","value":"10"},
{"source":"ser3","dest":"ser5","value":"10"}]}
{
"nodes": [
{
"name": "ser1"
},
{
"name": "ser2"
},
{
"name": "ser3"
},
{
"name": "ser4"
},
{
"name": "ser5"
}
],
"links": [
{
"source": "ser1",
"dest": "ser3",
"value": "10"
},
{
"source": "ser1",
"dest": "ser5",
"value": "10"
},
{
"source": "ser2",
"dest": "ser4",
"value": "30"
},
{
"source": "ser3",
"dest": "ser4",
"value": "10"
},
{
"source": "ser3",
"dest": "ser5",
"value": "10"
}
]
}
我的index.html
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
font-family: sans-serif;
font-size: 10px;
}
</style>
<svg width="960" height="600"></svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.name; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
var graph = getData();
graph.links = graph.links.map(function(ele) {return {
source: ele.source, target: ele.dest, value: +ele.value
}
});
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(graph.nodes)
.enter().append("g")
var circles = node.append("circle")
.attr("r", 5)
.attr("fill", function(d) { return color(d.name); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var lables = node.append("text")
.text(function(d) {
return d.name;
})
.attr('x', 6)
.attr('y', 3);
node.append("title")
.text(function(d) { return d.name; });
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
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 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;
}
function getData() {
let json = {"nodes":[{"name":"ser1"},{"name":"ser2"},{"name":"ser3"},{"name":"ser4"},
{"name":"ser5"}],"links":[{"source":"ser1","dest":"ser3","value":"10"},
{"source":"ser1","dest":"ser5","value":"10"},
{"source":"ser2","dest":"ser4","value":"30"},
{"source":"ser3","dest":"ser4","value":"10"},
{"source":"ser3","dest":"ser5","value":"10"}]};
return json;
}
</script>
我在下面的发现 (1)仅sample2结构(json数据)适合并且可以适合D3(强制有向图)(2)要使用D3模板,请更改(i)id = node键(ii)添加graph.link = graph.links.map (function(ele){return {source:ele.source,target:ele.dest,value:+ ele.value}}); ((3)Json数据可以是内部或外部数据源(4)D3可以根据我们拥有的数据提供更多值/信息
答案 0 :(得分:1)
如果要引用节点的属性而不是索引 (这是默认设置),您可以使用功能link.id()。
在您的情况下,可以使用sample2并通过以下方式将链接与节点连接:
d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.name; }))
,您只需将“目标”重命名为“目标”:
graph.links = graph.links.map(function(ele) {
return {
source: ele.source, target: ele.dest, value: +ele.value
}
});
我稍微调整了一个现有块来加载您的数据,see this example block。 另外,我在这里添加了一个代码段:
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.name; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
var graph = getData();
graph.links = graph.links.map(function(ele) {return {
source: ele.source, target: ele.dest, value: +ele.value
}
});
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(graph.nodes)
.enter().append("g")
var circles = node.append("circle")
.attr("r", 5)
.attr("fill", function(d) { return color(d.name); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var lables = node.append("text")
.text(function(d) {
return d.name;
})
.attr('x', 6)
.attr('y', 3);
node.append("title")
.text(function(d) { return d.name; });
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
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 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;
}
function getData() {
let json = {"nodes":[{"name":"ser1"},{"name":"ser2"},{"name":"ser3"},{"name":"ser4"},
{"name":"ser5"}],"links":[{"source":"ser1","dest":"ser3","value":"10"},
{"source":"ser1","dest":"ser5","value":"10"},
{"source":"ser2","dest":"ser4","value":"30"},
{"source":"ser3","dest":"ser4","value":"10"},
{"source":"ser3","dest":"ser5","value":"10"}]};
return json;
}
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
font-family: sans-serif;
font-size: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js"></script>
<svg width="300" height="300"></svg>