简体   繁体   English

D3.js返回颜色()似乎是随机的?

[英]D3.js return color() seems random?

So I've been using D3.js to make display information from a Facebook network, and the nodes in the network represent the different people in the group. 所以我一直在使用D3.js从Facebook网络制作显示信息,网络中的节点代表组中的不同人。 I recently started dealing with centrality and wanted some nodes to be different in color so that they can stand out. 我最近开始处理中心性,并希望一些节点的颜色不同,以便它们能够脱颖而出。

In my JSON file the nodes are represented as so: 在我的JSON文件中,节点表示为:

 {"nodes":[{"id":"10066","group":1},{"id":"10072","group":2},{"id":"10075","group":1},{"id":"10077","group":1},{"id":"10093","group":1},{"id":"10114","group":2}],

The "group 1" nodes are supposed to be blue, while the "group 2" nodes are supposed to be orange. “组1”节点应该是蓝色,而“组2”节点应该是橙色。 For most of the graphs it's fine, but for some graphs the colors are switched. 对于大多数图表来说没问题,但对于某些图表,颜色会被切换。 Is there any way to keep the colors consistant with the group? 有没有办法让颜色与群体保持一致?

Here is the code for when I build the nodes: 以下是构建节点时的代码:

//sets up the nodes
           var node = svg.append("g")
               .attr("class", "nodes")
               .selectAll("circle")
               .data(graph.nodes)
               .enter().append("circle")
                   .attr("r", 10)
                   .attr("fill", function (d) { return color(d.group); })
                   .call(d3.drag()
                       .on("start", dragstarted)
                       .on("drag", dragged)
                       .on("end", dragended));

Oh and the color scale: 哦和色标:

var color = d3.scaleOrdinal(d3.schemeCategory10);

This is what ALL of the networks should look like: 这就是所有网络应该是这样的:

这就是所有网络应该是什么样子

But some of them end up like this: 但其中一些最终会像这样:

不应该是什么样子

If you need the entire code, or more examples go to: https://github.com/AnimNations/Biomed-Research-Undergrad 如果您需要整个代码或更多示例,请访问: https//github.com/AnimNations/Biomed-Research-Undergrad

The problem here is that you didn't define the domain of your color scale. 这里的问题是你没有定义色标的域。

Your ordinal color scale, as any ordinal scale, will work without defining domain (that's clear, since your code didn't break). 您的序数色标,如任何序数标度,将在不定义域的情况下工作(这很清楚,因为您的代码没有中断)。 As explained in the API , if you don't set the domain... API中所述,如果您未设置域...

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

However, as you can see, the scale assigns the colors in a first-come, first-served basis. 但是,正如您所看到的,缩放比例以先到先得的方式分配颜色。 And that's why you are seeing the colors inverted sometimes. 这就是为什么你有时会看到颜色倒置的原因。

Let's show that. 让我们来表明。

Here is a simple demo. 这是一个简单的演示。 The first 3 circles are from group "a", while the last 3 are from group "b": 前3个圆圈来自组“a”,而后3个圆圈来自组“b”:

 var svg = d3.select("svg"); var colors = d3.scaleOrdinal(d3.schemeCategory10) var data = [{ group: "a" }, { group: "a" }, { group: "a" }, { group: "b" }, { group: "b" }, { group: "b" }, ] svg.selectAll(null) .data(data) .enter() .append("circle") .attr("cy", 50) .attr("cx", function(d, i) { return 10 + i * 30 }) .attr("r", 10) .attr("fill", function(d) { return colors(d.group) }); 
 <script src="https://d3js.org/d3.v4.min.js"></script> <svg></svg> 

As you can see, we have the "a" circles in blue and the "b" circles in orange. 如您所见,我们将“a”圆圈设为蓝色,将“b”圆圈设为橙色。

But what happens if the first circle is from "b" group? 但如果第一个圆圈来自“b”组,会发生什么?

 var svg = d3.select("svg"); var colors = d3.scaleOrdinal(d3.schemeCategory10) var data = [{ group: "b" }, { group: "a" }, { group: "a" }, { group: "b" }, { group: "b" }, { group: "b" }, ] svg.selectAll(null) .data(data) .enter() .append("circle") .attr("cy", 50) .attr("cx", function(d, i) { return 10 + i * 30 }) .attr("r", 10) .attr("fill", function(d) { return colors(d.group) }); 
 <script src="https://d3js.org/d3.v4.min.js"></script> <svg></svg> 

As you can see, blue is now assigned to "b" and orange to "a". 如您所见,蓝色现在分配为“b”,橙色分配给“a”。 The colors are inverted. 颜色是倒置的。

Solution : set the domain: 解决方案 :设置域名:

var colors = d3.scaleOrdinal(d3.schemeCategory10)
    .domain(["a", "b"])

Now the order of the elements in the data doesn't matter anymore: "a" will be blue and "b" will be orange, for any data set. 现在,数据中元素的顺序不再重要:对于任何数据集,“a”将为蓝色,“b”将为橙色。

 var svg = d3.select("svg"); var colors = d3.scaleOrdinal(d3.schemeCategory10) .domain(["a", "b"]) var data = [{ group: "b" }, { group: "a" }, { group: "a" }, { group: "b" }, { group: "b" }, { group: "b" }, ] svg.selectAll(null) .data(data) .enter() .append("circle") .attr("cy", 50) .attr("cx", function(d, i) { return 10 + i * 30 }) .attr("r", 10) .attr("fill", function(d) { return colors(d.group) }); 
 <script src="https://d3js.org/d3.v4.min.js"></script> <svg></svg> 

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

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