score:1
Accepted answer
look at the objects in your links
array:
[{"source": "0", "target": "1", "value": 1},...
//this is a string ---------^
you don't have numbers here, just strings. for the force to work, you have to set the number of the source and the target.
a simple solution is coercing:
links.foreach(function(d) {
d.source = +d.source;
d.target = +d.target;
})
here is the resulting code:
var nodes = [{
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxxn"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}, {
"name": "xxx"
}];
var links = [{
"source": "0",
"target": "1",
"value": 1
}, {
"source": "2",
"target": "1",
"value": 1
}, {
"source": "3",
"target": "1",
"value": 1
}, {
"source": "4",
"target": "1",
"value": 1
}, {
"source": "5",
"target": "1",
"value": 1
}, {
"source": "6",
"target": "1",
"value": 1
}, {
"source": "7",
"target": "1",
"value": 1
}, {
"source": "8",
"target": "1",
"value": 1
}, {
"source": "9",
"target": "1",
"value": 1
}, {
"source": "10",
"target": "1",
"value": 1
}, {
"source": "11",
"target": "1",
"value": 1
}, {
"source": "12",
"target": "1",
"value": 1
}, {
"source": "13",
"target": "1",
"value": 1
}, {
"source": "14",
"target": "1",
"value": 1
}, {
"source": "1",
"target": "15",
"value": 1
}, {
"source": "1",
"target": "16",
"value": 1
}, {
"source": "1",
"target": "17",
"value": 1
}, {
"source": "1",
"target": "18",
"value": 1
}, {
"source": "1",
"target": "19",
"value": 1
}, {
"source": "1",
"target": "20",
"value": 1
}, {
"source": "1",
"target": "21",
"value": 1
}, {
"source": "1",
"target": "22",
"value": 1
}, {
"source": "1",
"target": "23",
"value": 1
}, {
"source": "1",
"target": "24",
"value": 1
}, {
"source": "1",
"target": "25",
"value": 1
}, {
"source": "1",
"target": "26",
"value": 1
}, {
"source": "1",
"target": "27",
"value": 1
}, {
"source": "1",
"target": "28",
"value": 1
}, {
"source": "1",
"target": "29",
"value": 1
}, {
"source": "1",
"target": "30",
"value": 1
}, {
"source": "1",
"target": "31",
"value": 1
}, {
"source": "1",
"target": "32",
"value": 1
}, {
"source": "1",
"target": "33",
"value": 1
}, {
"source": "1",
"target": "34",
"value": 1
}, {
"source": "1",
"target": "35",
"value": 1
}, {
"source": "1",
"target": "36",
"value": 1
}];
links.foreach(function(d) {
d.source = +d.source;
d.target = +d.target;
})
var width = 400,
height = 400;
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var force = d3.layout.force()
.size([width, height])
.nodes(nodes)
.links(links);
force.linkdistance(width / 2);
var link = svg.selectall('.link')
.data(links)
.enter().append('line')
.attr('class', 'link');
var node = svg.selectall('.node')
.data(nodes)
.enter().append('circle')
.attr('class', 'node');
force.on("start", function(){
svg.append("text")
.attr("class", "text")
.attr("x", 150)
.attr("y", 80)
.text("calculating...");
});
force.on('end', function() {
svg.select(".text").remove();
node.attr('r', width / 25)
.attr('cx', function(d) {
return d.x;
})
.attr('cy', function(d) {
return d.y;
});
link.attr('x1', function(d) {
return d.source.x;
})
.attr('y1', function(d) {
return d.source.y;
})
.attr('x2', function(d) {
return d.target.x;
})
.attr('y2', function(d) {
return d.target.y;
});
});
force.start();
<script src='https://d3js.org/d3.v3.min.js'></script>
edit: since you're using .on("end"
to paint your nodes, you'll have to wait until the force finishes.
Source: stackoverflow.com
Related Query
- D3 force directed uncaught type error
- Uncaught type error on d3.js
- Uncaught Type Error "this.node() is null" with graphviz/d3 and python http.server
- Why do I get an error on `SimulationNodeDatum` when using D3 on Angular 10 for force directed graph?
- Fix Node Position in D3 Force Directed Layout
- Highlight selected node, its links, and its children in a D3 force directed graph
- D3.js force directed graph, reduce edge crossings by making edges repel each other
- Updating links on a force directed graph from dynamic json data
- D3 force directed layout with bounding box
- Add text label to d3 node in Force directed Graph and resize on hover
- D3 force directed graph with drag and drop support to make selected node position fixed when dropped
- Add text/label onto links in D3 force directed graph
- semantic zooming of the force directed graph in d3
- Curved line on d3 force directed tree
- D3 force directed graph, different shape according to data and value given?
- Is there a tap and double tap event in d3.js force directed graph
- D3 highlight selected node, and its links to parent and ancestors in a force directed graph
- D3 - Pie Chart & Force Directed Labels
- Labels / text on the nodes of a D3 force directed graph
- D3 Force Directed Graph ajax update
- how to prevent overlapping of link in force directed graph?
- Introducing Arrow(directed), in Force Directed Graph d3
- d3.js force directed layout constrained by a shape
- d3: force directed graph: node filtering
- d3js force directed - On hover to node, highlight/colourup linked nodes and links?
- D3.js force directed graph, each group different color?
- How to have a text label on links in d3 force directed graphs
- Constructing Force Directed Graphs From Only Link Data
- d3 force directed layout - link distance priority
- Prevent node overlap in force directed graph
More Query from same tag
- how to plot one column as x axis and the other column as y axis in D3/javascripts
- Plotting of Network Topology graph based on json input
- Use variable to call data column in dataset d3.js
- d3.js extend bottom axis line to origin
- How to add external javascript file in Ipython notebook
- How can we detect Left click on path
- d3 equivalent for Ruby on Rails?
- Reusing Axis Scale overwrites set domain
- How to update handle location on slider of D3js dynamic chart?
- keep Dragging within shape D3 - ReactJS
- d3: timeseries from data
- How to manipulate JSON objects into an array with javascript
- Unable to change X/Y axis value in D3
- Plot a Network with specified coordinates in D3.js
- d3 no console errors and no SVG rendering
- how do i format the value shown in a d3 graph
- c3.js spline time series chart going backwards
- D3.js SVG on OpenLayers3 interactive map
- Plotted lat/long points on map are being placed incorrectly
- Multiline graph throws parse error on already parsed dates
- explanation of animating a sine wave example
- How can I set the transform origin to the middle of this rectangle in D3?
- Restrict drag and drop to a path object (D3.js)
- reload d3 graph when a 'dropdown menu value changes
- d3.js for graph analysis, highlight a node
- Draw imbricated blocks that are rotated and fliped with d3.js
- dartlang interop js and d3 integration not working in dart
- D3.js Making multiple selections from a checkbox and filter the nodes and links based on the selection
- Why does the dependency d in tickFormat: function(d) {} have a value other than the data I have brought in
- External javascript cannot run in D3JS