forever_software forever_software - 25 days ago 6
Javascript Question

Uncaught TypeError: Cannot set property 'depth' of null in d3js

I'm new in d3js and trying to learn from the youtube totarial. I write the same code but I cant fix this error : "Uncaught TypeError: Cannot set property 'depth' of null(…) ------ d3.v3.min.js:4"
I give the code below:



<!DOCTYPE html>
<html>

<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="https://d3js.org/d3.v3.min.js"></script>
</head>
<script type="text/javascript">
var width = 960,
height = 600;

var canvas = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(50,50)");

var tree = d3.layout.tree()
.size([400, 400]);

d3.json("mydata.json" ,function(data) {

var nodes = tree.nodes(data);
console.log(nodes);
})
</script>

<body>
</body>

</html>




Answer

can you give more details about the elements your'e using ?

var width = 960,
    height = 600;

var canvas = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .append("g")
    .attr("transform", "translate(50,50)");

var tree = d3.layout.tree()
    .size([400, 400]);

d3.json("mydata.json" ,function(data) {
	
	var nodes = tree.nodes(data);
	console.log(nodes);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/2.1.3/d3.min.js"></script>