I am trying to use D3 with a JSON file to create and display a tree map. Very simple, just trying out D3 for fun. However, after I have put together both my tree.js and ran the script, I get a blank page. Console says
XMLHttpRequest cannot load file:///Users/aczre/Desktop/d3TestApp/shreleases.json.
Cross origin requests are only supported for HTTP.
Uncaught TypeError: Cannot read property 'children' of null d3.v2.js:6081
d3_layout_hierarchyChildren d3.v2.js:6081
recurse d3.v2.js:5990
hierarchy d3.v2.js:6033
tree d3.v2.js:6384
object.nodes d3.v2.js:6074
(anonymous function) tree.js:17
d3.json d3.v2.js:514
ready d3.v2.js:504
d3.xhr.req.onreadystatechange d3.v2.js:497
d3.xhr d3.v2.js:500
d3.text d3.v2.js:510
d3.json d3.v2.js:513
(anonymous function) tree.js:16
Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101 d3.v2.js:500
d3.xhr d3.v2.js:500
d3.text d3.v2.js:510
d3.json d3.v2.js:513
(anonymous function) tree.js:16
Not sure what I did wrong... here's my tree.js
var width = 960,
height = 2000;
var tree = d3.layout.tree()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var vis = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40, 0)");
d3.json("shreleases.json", function(json) { // Line 16
var nodes = tree.nodes(json);
var link = vis.selectAll("path.link")
.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("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.attr("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.name; });
});
I tried to stick to the examples listed on the D3 website, https://github.com/mbostock/d3/wiki/Gallery but I'm not sure if I completely understand the syntax.
In chrome you can tell it to allow local file access, so you don't need a server, e.g.
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -allow-file-access-from-files
You probably need to make the request for the JSON file from a web server. The error message is complaining that it is not an http request, since your file is stored locally.
You could throw your JSON file onto your web host, if you have one. Or you could create a local server setup using something like WAMP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With