简体   繁体   English

如何为Force Network d3.js中的节点组分配一致的颜色

[英]How to assign consistent colours to node groups in Force Network d3.js

I am trying to assign a specific colour to a specific group in my force network graph. 我正在尝试将特定颜色分配给力网络图中的特定组。 This is so that the groups are assigned the same colours consistently across all my graphs. 这样可以在所有图形中为组分配相同的颜色。 The group names are "a", "b", "c", "d" and they are assigned a colour in accordance to their order currently. 组名称是“a”,“b”,“c”,“d”,并且根据它们当前的顺序为它们分配颜色。

What I want, for example, is "a" to consistently be assigned to Blue and "b" always to Red. 例如,我想要的是“a”一直被分配给Blue而“b”总是分配给Red。

The JavaScript I have for the node colouring is below: 我用于节点着色的JavaScript如下:

var color = d3.scale.category10();

var node = vis.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.style("fill", function(d) { return color(d.group); })
.style("opacity", 0.9)
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.call(force.drag);

Any help would be much appreciated, 任何帮助将非常感激,

Thank you 谢谢

d3.scale.category10() (or d3.schemeCategory10 in v4) works on a first-come, first-served basis. d3.scale.category10() (或v4中的d3.schemeCategory10 )以先到先得的方式工作。 It means that, if you don't set a domain, 这意味着,如果您没有设置域名,

the domain will be inferred implicitly from usage by assigning each unique value passed to the scale a new value from the range. 通过将传递给比例的每个唯一值分配给该范围中的新值,将从使用中隐式推断该域。 ( source ) 来源

We can easily show this. 我们可以轻松地展示这一点 Have a look at this demo, where I'm using two color scales: 看看这个演示,我使用两个色标:

 var data = ["a", "b", "c", "d"]; var color = d3.scale.category10(); var color2 = d3.scale.category10(); var body = d3.select("body") var paragraphs1 = body.selectAll(null) .data(data) .enter() .append("p") .style("background-color", function(d) { return color(d) }) .html(String); body.append("br"); var paragraphs1 = body.selectAll(null) .data(data.reverse()) .enter() .append("p") .style("background-color", function(d) { return color2(d) }) .html(String); 
 p { margin: 0px; } 
 <script src="https://d3js.org/d3.v3.min.js"></script> 

As you can see, the data itself doesn't matter, just the order. 如您所见,数据本身并不重要,只是顺序。

To have a color consistently assigned to a datum value, as you asked, we just need to set the domain: 要按照您的要求将颜色一致地分配给基准值,我们只需要设置域:

var color = d3.scale.category10()
    .domain(["a", "b", "c", "d"]);

Here is the demo, now the colors are the same for the same datum: 这是演示,现在相同数据的颜色相同:

 var data = ["a", "b", "c", "d"]; var color = d3.scale.category10() .domain(data); var color2 = d3.scale.category10() .domain(data); var body = d3.select("body") var paragraphs1 = body.selectAll(null) .data(data) .enter() .append("p") .style("background-color", function(d) { return color(d) }) .html(String); body.append("br"); var paragraphs1 = body.selectAll(null) .data(data.reverse()) .enter() .append("p") .style("background-color", function(d) { return color2(d) }) .html(String); 
 p { margin: 0px; } 
 <script src="https://d3js.org/d3.v3.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM