【发布时间】: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