javascript - 当任何列被称为 "value"时是否可以创建树形图

标签 javascript d3.js treemap

我是 d3 的新手,我尝试创建一个树形图。 我注意到,当键名为“value”时,我的代码可以工作,而如果我将键命名为“size”,则代码不会工作。

我的问题从第一行开始:

d3.json("dataZC.json", function(err, data)
{
    var treemap =d3.layout.treemap()
          .size([500,500])
          .nodes(data);
console.log(treemap);
}

我的数据看起来像这样

{
 "name": "flare",
 "children": [
  {"name": "analytics",
   "children": [
     {"name": "cluster",
     "children": [
      {"name": "AgglomerativeCluster", "size": 3938},
      {"name": "CommunityStructure", "size": 3812},
      {"name": "HierarchicalCluster", "size": 6714},
      {"name": "MergeEdge", "size": 743}
     ]},
    {
     "name": "graph",
     "children": [
      {"name": "BetweennessCentrality", "size": 3534},
      {"name": "LinkDistance", "size": 5731},
      {"name": "MaxFlowMinCut", "size": 7840},
      {"name": "ShortestPaths", "size": 5914},
      {"name": "SpanningTree", "size": 3416}
     ]}

     ,........ 

   ]}
]}

如果有人有一个使用任何键使用树状图的解决方案,我将非常感激。 谢谢。

最佳答案

来自fine manual :

treemap.value([value])
If value is specified, sets the value accessor to the specified function. If value is not specified, returns the current value accessor, which assumes that the input data is an object with a numeric value attribute:

function value(d) {
    return d.value;
}

使用

treemap = d3.layout.treemap()
    .value(function(d) {
        return d.size;
    })

关于javascript - 当任何列被称为 "value"时是否可以创建树形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36427411/

相关文章:

javascript - 使链接在新页面上的 iframe 中加载内容

javascript - 使用 d3 同时用 .tween 过渡所有 div 元素

d3.js - 在 D3.js 中嵌套数据

java - Java 中的集合和/或数组是否有正确的 upperBound 和 lowerBound?

javascript - 从 HTML 表单获取输入的类型

javascript - 有什么比$(document).ready()更靠谱的吗?

javascript - 将多个 colorAxis 对象应用于 dimple.js 中的线图与气泡图时,行为不一致

java - 在大文件Java中查找重复的行

java - 使用TreeMap Key作为两种不同类型的集合

javascript - 从 URL 重定向 anchor 以指定 Onload