代码之家  ›  专栏  ›  技术社区  ›  Jimmy

d3-对图表使用json中的颜色

  •  2
  • Jimmy  · 技术社区  · 7 年前

    我有一个json文件,如下所示:

    {  
       "name":"TOPICS",
       "children":[  
          {  
             "name":"Topic A",
             "children":[  
                {  
                   "name":"Anna",
                   "size":5,
                   "text":"A story",
                   "sentiment":0.8,
                   "source":"dictionary",
                   "color":"gray"
                },
    

    它被输入一个读取这个文件的d3图表。我要做的是如下现有的填充函数:

    .style("fill", function (d) { return color((d.children ? d : d.parent).data.name); });
    

    而是从json中提取颜色。在这种情况下,格雷。有人能帮我吗?

    编辑:根据下面的帮助,我有此代码,但仍然不起作用:

    g.selectAll('g')
        .data(root.descendants())
        .enter().append('g').attr("class", "node").append('path')
        .attr("display", function (d) { return d.depth ? null : "none"; })
        .attr("d", arc)
        .style('stroke', '#fff')
        .style("fill", d => (d.children? d: d.parent).data.color);
    
    // Populate the <text> elements with our data-driven titles.
    g.selectAll(".node")
        .append("text")
        .attr("transform", function(d) {
            return "translate(" + arc.centroid(d) + ")rotate(" + computeTextRotation(d) + ")"; })
        .attr("dx", "-20") // radius margin
        .attr("dy", ".5em") // rotation align
        .text(function(d) { return d.parent ? d.data.name : "" });
    
    }); 
    
    0 回复  |  直到 7 年前