'Coloring Connected Components D3 V3
I am working in D3 version 3, and I have a simple working program that can read a JSON file and convert it into an animated, linked graph.
I was wondering if there was a way for me to color separate connected components differently, for example, have the first component colored blue and another colored red, in a way that could be applied to a larger JSON file. I am pretty new to javascript, but wondering if I could use the group id to determine a node's color. I organized my example JSON file as follows-
{
"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}
]
}
Note that each node is part of a group (connected component).
My index.html is as follows.
<!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>
Solution 1:[1]
Here are a few approaches to color the nodes based on the group:
Using a pre-defined d3 colorScale (d3 oridnal color schemas)
var colorScale = d3.scale.category10().domain(json.nodes.map(function(d) { return d.group; }));
User defined ordinal color scale:
var colorScale = d3.scale.ordinal().domain([1, 2, 3]).range(["blue", "green", "red"]);
If the extent of the groups is huge, I'd recommend a linear scale using a gradient of colors. Similar to this: http://bl.ocks.org/jfreyre/b1882159636cc9e1283a
Using one of the above and applying to the nodes:
node.append("circle")
.attr("r","20")
.style('fill', function(d) {
return colorScale(d.group);
});
Here's a snippet:
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>
Hope this helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shashank |