How to display d3 bubbles in different colors for a dataset with one branch and many children?

China☆狼群 提交于 2019-12-06 10:51:29

Simply pass a different datum to the color function, e.g. the index:

node.append("circle")
  .attr("r", function(d) { return d.r; })
  .style("fill", function(d, i) { return color(i); });

or, for your data, size:

node.append("circle")
  .attr("r", function(d) { return d.r; })
  .style("fill", function(d) { return color(d.size); });

To pic random colors user this

 node.append("circle").attr("r", function(d) {
    return d.r;
}).style("fill", function(d) {
    return color(Math.random());
});

Two concept we can use for fill color

1. var colour = d3.scale.category20(); // it will set random color from 20 color list.

2.Its easy to set custom color based on text value which comes from JSON Data .. i have used below concept

.style("fill", function (d) {
            if (d.item.text == "InProgress") {
                return "#2DD7EB"
            } else if (d.item.text == "Signed Off") {
                return "#3CEB2D"
            } else if (d.item.text == "Pending") {
                return "#F55431"
            }  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!