gpt4 book ai didi

javascript - 着色连通分量 D3 V3

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:59 25 4
gpt4 key购买 nike

我在 D3 版本 3 中工作,我有一个简单的工作程序可以读取 JSON 文件并将其转换为动画链接图。

我想知道是否有一种方法可以对连接的组件进行不同的着色,例如,将第一个组件着色为蓝色,将另一个着色为红色,这种方式可以应用于更大的 JSON 文件。我对 javascript 很陌生,但想知道我是否可以使用组 ID 来确定节点的颜色。我按如下方式组织我的示例 JSON 文件-

{
"nodes":[
{"name":"node1","group":1},
{"name":"node2","group":1},
{"name":"node3","group":1},
{"name":"node4","group":3}
],
"links":[
{"source":2,"target":1,"weight":3},
{"source":0,"target":2,"weight":3}
]
}

请注意,每个节点都是一个组(连通分量)的一部分。

我的index.html如下。

<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>

svg {
background-color:red;
width: 100%;
}

.link {
stroke: #fff;
}

.node text {
stroke:#fff;
fill: aliceblue;
cursor: pointer;
font-family: fantasy;
padding: 10%;
}

.node circle{
stroke:#fff;
stroke-width:3px;
fill:#fff;
padding: 20px;
}

</style>
<body>

<div class="nodeContainer">

<script>

var width = 960,
height = 500

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var force = d3.layout.force()
.gravity(.05)
.distance(100)
.charge(-100)
.size([width, height]);

d3.json("graphFile.json", function(json) {
force
.nodes(json.nodes)
.links(json.links)
.start();

var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.weight); });

var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);

node.append("circle")
.attr("r","20");

node.append("text")
.attr("dx", 23)
.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>

</div>

最佳答案

以下是根据为节点着色的几种方法:

  1. 使用预定义的 d3 colorScale ( d3 oridnal color schemas )

    var colorScale = d3.scale.category10().domain(json.nodes.map(function(d) { return d.group; }));
  2. 用户定义的序数色标:

    var colorScale = d3.scale.ordinal().domain([1, 2, 3]).range(["blue", "green", "red"]);
  3. 如果组的范围很大,我建议使用颜色渐变的线性比例。类似于:http://bl.ocks.org/jfreyre/b1882159636cc9e1283a

使用上述之一并应用于节点:

node.append("circle")
.attr("r","20")
.style('fill', function(d) {
return colorScale(d.group);
});

这是一个片段:

var json = {
"nodes":[
{"name":"node1","group":1},
{"name":"node2","group":1},
{"name":"node3","group":1},
{"name":"node4","group":3}
],
"links":[
{"source":2,"target":1,"weight":3},
{"source":0,"target":2,"weight":3}
]
};

var width = 960,
height = 500

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var force = d3.layout.force()
.gravity(.05)
.distance(100)
.charge(-100)
.size([width, height]);

var colorScale = d3.scale.category10().domain(json.nodes.map(function(d) { return d.group; }));
//var colorScale = d3.scale.ordinal().domain([1, 2, 3]).range(["blue", "green", "red"]);

force
.nodes(json.nodes)
.links(json.links)
.start();

var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.weight); });

var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);

node.append("circle")
.attr("r","20")
.style('fill', function(d) {
return colorScale(d.group);
});

node.append("text")
.attr("dx", 23)
.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 + ")"; });
});
svg {
background-color:red;
width: 100%;
}

.link {
stroke: #fff;
}

.node text {
stroke:#fff;
fill: aliceblue;
cursor: pointer;
font-family: fantasy;
padding: 10%;
}

.node circle{
stroke:#fff;
stroke-width:3px;
fill:#fff;
padding: 20px;
}
<script src="https://d3js.org/d3.v3.min.js"></script>

<div class="nodeContainer">
</div>

希望这对您有所帮助。

关于javascript - 着色连通分量 D3 V3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com