如何显示一个分支和许多孩子的数据集的不同颜色的d3气泡? [英] How to display d3 bubbles in different colors for a dataset with one branch and many children?

查看:160
本文介绍了如何显示一个分支和许多孩子的数据集的不同颜色的d3气泡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 http://bl.ocks.org/发布的示例制作d3气泡图表mbostock / 4063269

我的数据集有一个分支,但有很多子代(见下面的示例)。所有我的气泡都以相同的颜色显示。是否可以修改逻辑以为同一分支中的每个孩子生成不同的颜色气泡?如果是这样,任何援助将不胜感激。

My dataset is has one branch but many children (see sample below). All my bubbles are displayed in the same color. Is it possible to modify the logic to generate different color bubbles for each children in the same branch? If so, any assistance would be much appreciated.

{
 "name": "mydata",
 "children": [
  {"name": "test1", "size": 5},
  {"name": "test2", "size": 10},
  {"name": "test3", "size": 15},
  {"name": "test4", "size": 20}
 ]
}


推荐答案

只需传递一个不同的数据到颜色函数,例如索引:

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); });

或您的数据大小:

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

这篇关于如何显示一个分支和许多孩子的数据集的不同颜色的d3气泡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆