【问题标题】:d3.js v4 nest to hierarchy - Unable to read valued3.js v4 嵌套到层次结构 - 无法读取值
【发布时间】:2021-06-16 14:20:22
【问题描述】:

下面的函数有点问题。

我将 d3 嵌套传递给此函数并将其转换为 d3 层次结构。

想根据 scaledFTE 调整我的圈子(在一个包中)的大小。但是,我无法参考 scaledFTE 值。

我附上显示 D3 层次结构和错误消息的控制台屏幕截图。

感谢您的帮助

function layoutAndRenderHierarchyInNestFormat (nestFormatHierarchy, rootName){
    //Lays out and renders hierarchy in "nest" ("key-values" format).
    
        //Move the 'nest' array into a root node:
        var datasetAsJsonD3Hierarchy = {"key":rootName, "values": nestFormatHierarchy}
        
        console.log(datasetAsJsonD3Hierarchy)

        //Now create hierarchy structure 
        //Note that we need to add the "children" accessor "d=>d.values" in order
        //to tell d3.hierarchy to use nest's 'values' as children
        hierarchyGraph = d3
            .hierarchy(datasetAsJsonD3Hierarchy, d=>d.values) 
            //NB below is required for d3.pack etc that use a size attribute (e.g. for the different sizes of circles in d3.pack)
            .sum(d=>d.value.scaledFTE) //accessor for size data (e.g. circle size in a d3.pack)
               
            console.log(hierarchyGraph)
        
        //Can now calculate position data and render
        calculatepositionsAndRender(hierarchyGraph);
    }


[Screenshot of raw data structure][1]
[Console screenshot showing nest/hierarchy data structure][2]


  [1]: https://i.stack.imgur.com/5yXst.jpg
  [2]: https://i.stack.imgur.com/WZ60A.png

【问题讨论】:

  • 试试 .sum(d => d.scaledFTE)
  • 谢谢。我已经尝试过了,但由于某种原因,它最终给了我一个 Nan for r 值。
  • 你是对的 - 我误读了你的照片。你能发布你的数据吗?否则真的很难提供帮助。似乎并非每个节点都包含属性值。根是否包含属性值?
  • 谢谢。原始数据相当大。所有条目都有一个 scaledFTE 值。我首先让它们成为一个巢,然后将巢传递给 d3.hierarchy。 ` nest_pack = d3.nest() .key(e=>e['Main panel']) .key(e=>e.UoAString) .rollup(function(leaves) { return {"scaledFTE": d3.sum(叶, d=>d.context.scaledFTE)}}) //.rollup(e=>e.context.scaledFTE) .entries(dataModel.refEntries) pc1 = pack("#packDiv") .loadAndRenderNestDataset(nest_pack, " REF2014") `
  • 每个数据条目看起来像主问题中发布的屏幕截图

标签: javascript arrays d3.js


【解决方案1】:

您的嵌套数据的某些节点不包含 d.value(例如,在您的屏幕截图中,带有键的节点:"REF2014")。

d3.hierarchy(...).sum(accessor function) // iterates over every node and calls the accessor function. So nodes with no value property lead to an error when calling .sum(d=>d.value.scaledFTE).

另见https://github.com/d3/d3-hierarchy/blob/master/README.md#node_sum

所以你可以这样做:

function layoutAndRenderHierarchyInNestFormat (nestFormatHierarchy, rootName){
//Lays out and renders hierarchy in "nest" ("key-values" format).

    //Move the 'nest' array into a root node:
    var datasetAsJsonD3Hierarchy = {"key":rootName, "values": nestFormatHierarchy}
    
    console.log(datasetAsJsonD3Hierarchy)

    //Now create hierarchy structure 
    //Note that we need to add the "children" accessor "d=>d.values" in order
    //to tell d3.hierarchy to use nest's 'values' as children
    hierarchyGraph = d3
        .hierarchy(datasetAsJsonD3Hierarchy, d=>d.values) 
        //NB below is required for d3.pack etc that use a size attribute (e.g. for the different sizes of circles in d3.pack)
        .sum(d=> d.value ? d.value.scaledFTE : 0) //accessor for size data (e.g. circle size in a d3.pack)
           
        console.log(hierarchyGraph)
    
    //Can now calculate position data and render
    calculatepositionsAndRender(hierarchyGraph);
}

【讨论】:

  • 太棒了。这就是问题所在。我做了一个嵌套汇总,只有最低级别的节点才有“价值”。非常感谢您指出这一点
  • 我很高兴它有帮助!那你可以接受我的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多