Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property error using d3.js

I've been trying to visualize a Parse Tree generated by a Python Script via HTML and d3.js. The Python Script generates an HMTL document that look like this:

<!DOCTYPE html>
<meta charset="utf-8">
<head><title> Tree Visualization </title></head>

<script src="https://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript">js</script>

<body onLoad="drawTree({divID: \'viz\', width: 600, height: 400, padding: 50, treeData: treeData})">
<div id="viz"></div>
</body>
</html>

Where js is the Javascript code doing the d3 stuff

function drawTree(o) {  
    d3.select("#"+o.divID).select("svg").remove() 

    var viz = d3.select("#"+o.divID)
                .append("svg")
                .attr("width", o.width)
                .attr("height", o.height) 

    var vis = viz
              .append("g")
              .attr("id","treeg")
              .attr("transform", "translate("+ o.padding +","+ o.padding +")"); 

    var tree = d3.layout.tree()
               .size([o.width - (2 * o.padding), o.height - (2 * o.padding)]);

    var diagonal = d3.svg.diagonal()
                   .projection(function(d) { return [d.x, d.y]; });

    var nodes = tree.nodes(o.treeData);

    var link = vis.selectAll("pathlink")
               .data(tree.links(nodes))
               .enter()
               .append("path")
               .attr("class", "link")
               .attr("d", diagonal)

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

    node.append("circle")
        .attr("r", 10)
        .style("fill", function(d) { return (d.children) ? "#E14B3B" : "#1C8B98" });

    node.append("svg:text")
        .attr("dx", function(d) { return d.children ? 0 : 0; })
        .attr("dy", function(d) { return d.children ? 5 : 5; })
        .attr("text-anchor", function(d) { return d.children ? "middle" : "middle"; })
        .style("fill", "white")
        .text(function(d) { return d.name; });
}

and treeData is a JSON like representation of the tree.

What I now get is the error Cannot read property 'tree' of undefinedin the js part, in the var tree = ... line. Most likely I'm just blind and you'll instantly see what I did wrong, but I've been staring at this for a while now...

Thanks in advance for any help!

like image 429
Domenic Quirl Avatar asked Jul 01 '16 21:07

Domenic Quirl


2 Answers

d3js v4 doesn't use d3.layout.tree any more. Instead use like following

var tree = d3.layout.tree();  //v3

var tree = d3.tree();  //v4

Check the changelog for the differences:

  • d3.layout.tree ↦ d3.tree
like image 67
M.Kumaran Avatar answered Oct 10 '22 05:10

M.Kumaran


It may be the version of d3 you are using, I see you are loading:

<script src="https://d3js.org/d3.v4.min.js"></script>

It may be the case that in v4 there is no d3.layout.tree() object. Try switching to different version, may be this one:

<script src="http://d3js.org/d3.v3.min.js"></script>

As the error says, it can be that d3.layout is undefined or d3.layout.tree() is undefined.

like image 1
Ajit Avatar answered Oct 10 '22 06:10

Ajit