D3js树图NaN x值

时间:2016-01-04 22:56:04

标签: javascript json d3.js tree

我尝试使用Javascript对象创建一个小树图。我使用D3并试图说明元素(键,键)的嵌套结构而不是它们的值。

我的JSFiddle

var d1 = {
      "time_completed": 1420199657,
      "xid": "-dqAeAEjGxCDcjqrxHsraER3yWqew4xS",
      "title": "for 8h 46m",
      "time_created": 1420167382,
      "time_updated": 1420200033,
      "details": {
        "body": 0,
        "sound": 12598,
        "tz": "America/Chicago",
        "awakenings": 0,
        "light": 18970,
        "mind": 0,
        "asleep_time": 1420168079,
        "awake_time": 1420199400,
        "awake": 707,
        "rem": 0,
        "duration": 32275,
        "smart_alarm_fire": 1420199640,
        "quality": 100,
        "sunset": 0,
        "sunrise": 0
      },
      "date": 20150102,
      "shared": true,
      "snapshot_image": "/nudge/image/e/1420200033/-dqAeAEjGxCDcjqrxHsraER3yWqew4xS/MSh0lOvjHDY.png",
      "sub_type": 0
    }; 

var tree = d3.layout.tree()
    .size([360, diameter / 2 - 120])
    .separation(function(a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });

var diagonal = d3.svg.diagonal.radial()
    .projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });

var svg = d3.select("#sleep-schema").append("svg")
    .attr("width", diameter)
    .attr("height", diameter - 150)
    .append("g")
    .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");

var nodes = tree.nodes(d1),
    links = tree.links(nodes);

var link = svg.selectAll(".link")
    .data(links)
    .enter().append("path")
    .attr("class", "link")
    .attr("d", diagonal);

var node = svg.selectAll(".node")
    .data(nodes)
    .enter().append("g")
    .attr("class", "node")
    .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })

    node.append("circle")
    .attr("r", 4.5);

    node.append("text")
    .attr("dy", ".31em")
    .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
    .attr("transform", function(d) { return d.x < 180 ? "translate(8)" : "rotate(180)translate(-8)"; })
    .text(function(d) { return d.xid; });

}

出于某种原因,调用d3.layout.tree会在我的x坐标上给我一个NaN值

Error: Invalid value for <g> attribute transform="rotate(NaN)translate(0)"

有人能告诉我,我的问题是我的数据的形状还是我对d3.layout.tree()的使用?

更新:@Cyril,我已将数据对象从数组转换为JS对象,但d3.layout.tree()似乎无法遍历嵌套的JSON对象并计算x,y值。我倾向于同意你的观点,即我的数据存在问题。我一直试图关注this example

1 个答案:

答案 0 :(得分:1)

您的JSON数据是一个数组。 树布局要求Object不是数组。

您的数据:

d1 = [{
  "time_completed": 1420199657,
  "xid": "-dqAeAEjGxCDcjqrxHsraER3yWqew4xS",
  "title": "for 8h 46m",
  "time_created": 1420167382,
  "time_updated": 1420200033,
  "details": { ...
    "sunset": 0,
    "sunrise": 0
  },
  "date": 20150102,
  "shared": true,
  "snapshot_image": "/nudge/image/e/1420200033/-dqAeAEjGxCDcjqrxHsraER3yWqew4xS/MSh0lOvjHDY.png",
  "sub_type": 0
}];

应该是:

d1 = {
  "time_completed": 1420199657,
  "xid": "-dqAeAEjGxCDcjqrxHsraER3yWqew4xS",
  "title": "for 8h 46m",
  "time_created": 1420167382,
  "time_updated": 1420200033,
  "details": {
    "body": 0,
    "sound": 12598, ...
  },
  "date": 20150102,
  "shared": true,
  "snapshot_image": "/nudge/image/e/1420200033/-dqAeAEjGxCDcjqrxHsraER3yWqew4xS/MSh0lOvjHDY.png",
  "sub_type": 0
};

请参考这个数据结构应该如何的简单示例。 http://bl.ocks.org/d3noob/8375092

工作代码here 希望这有帮助!