【问题标题】:set specific colors from json for d3js sunburst charts从 json 为 d3js 旭日形图设置特定颜色
【发布时间】:2017-10-15 15:07:54
【问题描述】:

我正在尝试在 d3js 中创建一个旭日形图表,并希望每个项目/节点具有特定的颜色,该颜色可以从包含层次结构的 json 文件中读取。查看了 d3js 社区中的许多示例,但从未找到明确的答案。谢谢!

这是我的 json (flare2.json) 文件:

{
    "name": "Root","color": "#c0e2f1",
    "children": [
      {
        "name": "T1","color": "#a3a3a3",
        "children": [
            {"name": "S1", "size": 3938, "color": "#a9a9a9"},
            {"name": "D1","size": 3238, "color": "#ef69b4"}
        ]
      },
      {
        "name": "T2", "color": "#c0e2f1"
      }
    ]
}

这里是调用json的javascript sn-p:

d3.json("flare2.json", function(error, root) {
    var g = svg.selectAll("g")
        .data(partition.nodes(root))
        .enter().append("g");

    var color = d3.scale.ordinal();

    var path = g.append("path")
        .attr("d", arc)
        //.style("fill", function(d) { return colors((d.children ? d :     d.parent).name); })
        .style("fill", function(d) { return color(d.color); })
        .on("click", click);
}

【问题讨论】:

    标签: d3.js


    【解决方案1】:

    我认为你可能让这太难了。下面一行

    .style("fill", function(d) { return color(d.color); })
    

    似乎不需要使用color 函数,而只需返回对象的颜色属性,如下所示:

    .style("fill", function(d) { return d.color; })
    

    【讨论】:

    • 谢谢,@snapjs!是的,我通过 ".style("fill", function (d) { return d.color; })" 修复了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    相关资源
    最近更新 更多