user2242044 user2242044 -3 years ago 60
Javascript Question

loading a local JSON file with d3

I know JSON can't load locally in

Chrome
as many other questions have discussed. I am trying to recreate this: https://romsson.github.io/dragit/example/nations.html

I have tried to implement the solution of hardcoding the json.

Working code:

d3.json("https://rawgit.com/romsson/dragit/master/data/nations.json", function(nations) {


....

My attempt to load
nations.json
locally:

var nations = [{"name":"Angola","region":"Sub-Saharan Africa","income":[[2006,12127071],[2007,12420476],[2008,12707546]],
"lifeExpectancy":[[2006,46.02],[2007,46.54],[2008,47.06]]},
{"name":"Benin","region":"Sub-Saharan Africa","income":[[2006,7862944],[2007,8078314],[2008,8294941]],
"lifeExpectancy":[[2006,60.6],[2007,61.03],[2008,61.47]]}]

d3.json(nations, function(nations) {

....


I get the error:

Failed to load resource: the server responded with a status of 404 (Not Found)

Answer Source

d3.json is used to load data from url not an object present in the page. So if you have an object already in the page just pass it to the required function.

Example

function draw(nationsObj){
    // A bisector since many nation's data is sparsely-defined.
       var bisect = d3.bisector(function(d) { return d[0]; });

        .....contents
} 

draw(nations); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download