【问题标题】:Hierarchy's links() function returns empty arrayHierarchy 的 links() 函数返回空数组
【发布时间】:2020-06-29 00:21:52
【问题描述】:

我正在尝试使用 .json 数据以及 d3 js 和 vue js 创建树形图。我将数据加载到应用程序中,然后创建我的树形图对象。但是,当我将数据记录到控制台时,它会返回一个空数组。我怎样才能解决这个问题? console logs 我已经尝试删除 links() 函数,它工作得很好,但为了制作树形图,我相信我需要它

代码

createTreemap() {
      

    d3.json("https://raw.githubusercontent.com/BrennanAdams/treemap/master/warehouses.json").then(function(data) {
       //reformating the data to fit hierarchy layout
      var root = d3.hierarchy(data)
      root.sum(d => d.value)
      root.sort((a, b) => b.value - a.value)

      var treemapLayout = d3.treemap()
        .paddingTop(20)
        .paddingInner(2)
        .round(true)
        .tile(d3.treemapSquarify.ratio(2)); // Squarify, Slice, SliceDice, Binary

      //finalizing treemap
      const links = treemapLayout(root).links()
       //need to load the data
       console.log(links)

      //creating the treemap and its properties
        
      var svg = d3.select("body")
        .data(root)
        .enter()
        .append("rect")
        .attr('x', function (d) { return d.x0; })
        .attr('y', function (d) { return d.y0; })
        .attr('width', function (d) { return d.x1 - d.x0; })
        .attr('height', function (d) { return d.y1 - d.y0; })
        .style("stroke", "black")
        .style("fill", "slateblue")
    })
  },
}

数据集

{
    "warehouses":[
        {
            "id":4,
            "name":"Goimek",
            "value":354,
            "idParent":0,
            "nameParent":"",
            "locationInfo":[
            {
                "id":19,
                "name":"Puerta 4",
                "value":354
            }],
            "warehouseChildrenInfo":[]
        },
        {
            "id":5,
            "name":"Karpa",
            "value":167,
            "idParent":0,
            "nameParent":"",
            "locationInfo":[
            {
                "id":24,
                "name":"Karpa",
                "value":167
            }],
            "warehouseChildrenInfo":[]
        },
        {
            "id":6,
            "name":"Wec",
            "value":145,
            "idParent":0,
            "nameParent":"",
            "locationInfo":[
            {
                "id":25,
                "name":"WEC",
                "value":115
            }],
            "warehouseChildrenInfo":[
            {
                "id":1009,
                "name":"BIGUMETRIK",
                "value":30,
                "idParent":0,
                "nameParent":"",
                "locationInfo":[
                {
                    "id":1015,
                    "name":"BIGUMETRIK",
                    "value":30
                }],
                "warehouseChildrenInfo":[]
            }]
        },
        
        {
            "id":1037,
            "name":"PROVEEDOR",
            "value":10,
            "idParent":0,
            "nameParent":"",
            "locationInfo":[
            {
                "id":1553,
                "name":"PROVEEDOR Lehenetsitako kokapena",
                "value":10
            },
            {
                "id":1554,
                "name":"PIKUMEK S.L",
                "value":10
            },
            {
                "id":1555,
                "name":"TENKOR S.L",
                "value":10
            },
            {
                "id":1556,
                "name":"ZUMELTXU S.L",
                "value":10
            }],
            "warehouseChildrenInfo":[]
        }
    ]
}

【问题讨论】:

  • 您是否尝试将links() 移至根目录?

标签: javascript json d3.js treemap


【解决方案1】:

您需要指定children accessor。例如:

var root = d3.hierarchy(data, d => d.warehouses);

这是工作代码:

const data = {
  "warehouses": [{
      "id": 4,
      "name": "Goimek",
      "value": 354,
      "idParent": 0,
      "nameParent": "",
      "locationInfo": [{
        "id": 19,
        "name": "Puerta 4",
        "value": 354
      }],
      "warehouseChildrenInfo": []
    },
    {
      "id": 5,
      "name": "Karpa",
      "value": 167,
      "idParent": 0,
      "nameParent": "",
      "locationInfo": [{
        "id": 24,
        "name": "Karpa",
        "value": 167
      }],
      "warehouseChildrenInfo": []
    },
    {
      "id": 6,
      "name": "Wec",
      "value": 145,
      "idParent": 0,
      "nameParent": "",
      "locationInfo": [{
        "id": 25,
        "name": "WEC",
        "value": 115
      }],
      "warehouseChildrenInfo": [{
        "id": 1009,
        "name": "BIGUMETRIK",
        "value": 30,
        "idParent": 0,
        "nameParent": "",
        "locationInfo": [{
          "id": 1015,
          "name": "BIGUMETRIK",
          "value": 30
        }],
        "warehouseChildrenInfo": []
      }]
    },

    {
      "id": 1037,
      "name": "PROVEEDOR",
      "value": 10,
      "idParent": 0,
      "nameParent": "",
      "locationInfo": [{
          "id": 1553,
          "name": "PROVEEDOR Lehenetsitako kokapena",
          "value": 10
        },
        {
          "id": 1554,
          "name": "PIKUMEK S.L",
          "value": 10
        },
        {
          "id": 1555,
          "name": "TENKOR S.L",
          "value": 10
        },
        {
          "id": 1556,
          "name": "ZUMELTXU S.L",
          "value": 10
        }
      ],
      "warehouseChildrenInfo": []
    }
  ]
};

var root = d3.hierarchy(data, d=>d.warehouses);

var treemapLayout = d3.treemap()
  .paddingTop(20)
  .paddingInner(2)
  .round(true)
  .tile(d3.treemapSquarify.ratio(2)); 
  
const links = treemapLayout(root).links()

console.log(links)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

顺便说一句,属性名称(此处为warehouses)必须与层次结构中的其他级别相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    相关资源
    最近更新 更多