score:1
Accepted answer
This happens because requesting the JSON via AJAX is an asynchronous operation so the console.log
is executed before what's inside the anonymous function.
You may want to look at promises in JavaScript or at least put your code inside that function so the result of the request is available to your code. At the simplest level this should work:
function draw(data) {
var g = new dagreD3.graphlib.Graph().setGraph({});
data.forEach(function (job) {
console.log(job); // I do see job values correctly.
g.setNode(job.child, { label: job.child });
g.setEdge(job.parent, job.child, { label: job.path });
});
// carry on doing what you need in here...
}
d3.json("expectedOutput.json", draw);
Source: stackoverflow.com
Related Query
- setting d3 graph from inside of an anonymous function
- D3 JavaScript return data value plus variable from anonymous function
- Error while calling d3-tip "tip.show" from inside a function
- Building a force-directed concentric circle graph with node ordering from inside out in D3.V4
- How to return a value from a function, which is generated inside a callback function within?
- Function create graph in C3js don't show inside a event Meteor.js
- Obtain access to parent node from inside attrTween callback function (D3)
- Updating links on a force directed graph from dynamic json data
- Render D3 graph from a string of JSON instead of a JSON file
- Can d3.js draw two scatterplots on the same graph using data from the same source?
- Third variable in D3 anonymous function
- D3 - forEach is not a function when upgrading from v3 to v4
- Remove line from line graph in d3.js
- Direct data input from Django for a D3 graph
- Typescript expects undefined to be returned from helper function in d3.nest().rollup()
- d3.event is null inside of debounced function
- Transition from one forced-directed graph to another graph in d3js
- Making a graph inside a leaflet popup using geoJson data
- Setting up D3 force directed graph
- Convert a d3 chart to load data from json inside a variable
- d3 setting multiple attributes from the same function?
- Avoid targeting the object scope of this inside a function in ES6
- d3.js: Pass anonymous function as parameter to centering force?
- Setting D3 svg.transition to go from slow to fast to slow
- How to get web element (id) from the element position in D3.js force graph
- D3 - Stop Force Graph from moving around, nodes should only stay where moved
- Why does event listener callback function only work inside function scope?
- Black area when exporting graph from svg (d3.js) to jpg
- Trying to retrieve the x-value from a x-coordinate on a d3.js SVG graph
- d3js pie graph from jquery ajax - correlating json keys and values on the chart
More Query from same tag
- Why is my scope lost in $http function on angularjs and d3js?
- D3 force layout nodes attributes not updated properly
- GeoJson Map in Angular2
- Show all data for a variable in box next to d3 chart on mouseover
- How to make D3 line hidden from start?
- d3.js: styling <text> does not work
- D3 Selecting SVG Text Elements
- Why isn't my SVG being created?
- C3 bar chart with 3 variables
- What is `MNaN`?
- D3 js with angular 2 popup on clicking the arc of pie
- Rotate Donut Chart to Middle of Selected Slice
- d3js: Concatenating "d.y" + year
- How to add some padding between a path and text on it?
- Sum of nested JSON array using D3 .rollup
- D3 change zoom and pan gestures
- Scientific notation on axes in plot from django-nvd3
- d3 interaction between xAxis and text
- D3 reusable chart tootip follows wrong chart when update
- Unable to display a JSON map using d3.js
- Constraining d3 force layout graphs based on node degree
- D3 Added Circles to a MultiLine Chart - Circles on top of Circles
- How to disable animation in a force-directed graph?
- D3.js HTML table from JSON file-- How to create empty values so there aren't missing <td> elements?
- d3.js force directed graph sphere
- How can I select a link using d3.select?
- d3-tip offset on svgs within Leaflet, Firefox only, not working
- Remove specific value from array
- How do I change nodes to be rectangles instead of circles in a d3 force layout?
- cannot clear d3.js div on dropdown change