Colin
Colin

Reputation: 940

D3js Tree Diagram NaN x value

I'm trying to create a small tree diagram with a Javascript object. I'm using D3 and trying to illustrate the nested structure of the elements (keys, ids) rather than their values.

My 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; });

}

For some reason, calling d3.layout.tree is giving me a NaN value on my x coordinate

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

Can anyone tell me if my problem is the shape of my data or my use of d3.layout.tree()?

Updated: @Cyril, I've converted my data object from an array to a JS object but d3.layout.tree() seems unable to traverse the nested JSON object and calculate x,y values. I would tend to agree with you that it's a problem with my data. I've been trying to follow this example.

Upvotes: 0

Views: 723

Answers (1)

Cyril Cherian
Cyril Cherian

Reputation: 32327

Your JSON data is an array. The tree layout expects an Object not an array.

Your data:

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
}];

It should have been:

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
};

Please refer this simple example of how the data structure should be. http://bl.ocks.org/d3noob/8375092

Working code here Hope this helps!

Upvotes: 1

Related Questions