简体   繁体   中英

D3 flow graph was working, but doesn't display anything now

I have some d3 code that uses force layout and I can't get it to display onscreen. It was working earlier, so I know it is something small.

I have recently added code to pin the node when they are double clicked and the opposite when double clicked again. They should also change colour.

 // get the data links = [ { "source": "George Washington", "target": "John Adams", "value": "0" }, { "source": "John Adams", "target": "George Washington", "value": "1" }, { "source": "John Adams", "target": "Thomas Jefferson", "value": "0" }, { "source": "Thomas Jefferson", "target": "John Adams", "value": "1" }, { "source": "Thomas Jefferson", "target": "Aaron Burr", "value": "0" }, { "source": "Thomas Jefferson", "target": "George Clinton", "value": "0" }, { "source": "Thomas Jefferson", "target": "James Madison", "value": "1" }, { "source": "James Madison", "target": "George Clinton", "value": "0" }, { "source": "James Madison", "target": "Elbridge Gerry", "value": "0" }, { "source": "James Madison", "target": "James Monroe", "value": "1" }, { "source": "James Monroe", "target": "Daniel D. Tompkins", "value": "0" }, { "source": "James Monroe", "target": "John Quincy Adams", "value": "1" }, { "source": "John Quincy Adams", "target": "John C. Calhoun", "value": "0" }, { "source": "John Quincy Adams", "target": "Andrew Jackson", "value": "1" }, { "source": "Andrew Jackson", "target": "John C. Calhoun", "value": "0" }, { "source": "Andrew Jackson", "target": "Martin Van Buren", "value": "0" }, { "source": "Martin Van Buren", "target": "Andrew Jackson", "value": "1" }, { "source": "Martin Van Buren", "target": "Richard M. Johnson", "value": "0" }, { "source": "Martin Van Buren", "target": "William Henry Harrison", "value": "1" }, { "source": "William Henry Harrison", "target": "John Tyler", "value": "0" }, { "source": "John Tyler", "target": "William Henry Harrison", "value": "1" }, { "source": "John Tyler", "target": "Office Vacant", "value": "0" }, { "source": "John Tyler", "target": "James K. Polk", "value": "1" }, { "source": "James K. Polk", "target": "George M. Dallas", "value": "0" }, { "source": "James K. Polk", "target": "Zachary Taylor", "value": "1" }, { "source": "Zachary Taylor", "target": "Millard Fillmore", "value": "0" }, { "source": "Millard Fillmore", "target": "Zachary Taylor", "value": "1" }, { "source": "Millard Filmore", "target": "Office Vacant", "value": "0" }, { "source": "Millard Filmore", "target": "Franklin Pierce", "value": "1" }, { "source": "Franklin Pierce", "target": "William R. King", "value": "0" }, { "source": "Franklin Pierce", "target": "James Buchanan", "value": "1" }, { "source": "James Buchanan", "target": "John C. Breckinridge", "value": "0" }, { "source": "James Buchanan", "target": "Abraham Lincoln", "value": "1" }, { "source": "Abraham Lincoln", "target": "Hannibal Hamlin", "value": "0" }, { "source": "Abraham Lincoln", "target": "Andrew Johnson", "value": "0" }, { "source": "Andrew Johnson", "target": "Abraham Lincoln", "value": "1" }, { "source": "Andrew Johnson", "target": "Office Vacant", "value": "0" }, { "source": "Andrew Johnson", "target": "Ulysses S. Grant", "value": "1" }, { "source": "Ulysses S. Grant", "target": "Schuyler Colfax", "value": "0" }, { "source": "Ulysses S. Grant", "target": "Henry Wilson", "value": "0" }, { "source": "Ulysses S. Grant", "target": "Rutherford B. Hayes", "value": "1" }, { "source": "Rutherford B. Hayes", "target": "William A. Wheeler", "value": "0" }, { "source": "Rutherford B. Hayes", "target": "James A. Garfield", "value": "1" }, { "source": "James A. Garfield", "target": "Chester A. Arthur", "value": "0" }, { "source": "Chester A. Arthur", "target": "James A. Garfield", "value": "1" }, { "source": "Chester A. Arthur", "target": "Office Vacant", "value": "0" }, { "source": "Chester A. Arthur", "target": "Grover Cleveland", "value": "1" }, { "source": "Grover Cleveland", "target": "Thomas A. Hendricks", "value": "0" }, { "source": "Grover Cleveland", "target": "Benjamin Harrison", "value": "1" }, { "source": "Benjamin Harrison", "target": "Levi P. Morton", "value": "0" }, { "source": "Benjamin Harrison", "target": "Grover Cleveland", "value": "1" }, { "source": "Grover Cleveland", "target": "Adlai Stevenson", "value": "0" }, { "source": "Grover Cleveland", "target": "William McKinley", "value": "1" }, { "source": "William McKinley", "target": "Garret Hobart", "value": "0" }, { "source": "William McKinley", "target": "Theodore Roosevelt", "value": "0" }, { "source": "Theodore Roosevelt", "target": "William McKinley", "value": "1" }, { "source": "Theodore Roosevelt", "target": "Office Vacant", "value": "0" }, { "source": "Theodore Roosevelt", "target": "Charles W. Fairbanks", "value": "0" }, { "source": "Theodore Roosevelt", "target": "William Howard Taft", "value": "1" }, { "source": "William Howard Taft", "target": "James S. Sherman", "value": "0" }, { "source": "William Howard Taft", "target": "Office Vacant", "value": "0" }, { "source": "William Howard Taft", "target": "Woodrow Wilson", "value": "1" }, { "source": "Woodrow Wilson", "target": "Thomas R. Marshall", "value": "0" }, { "source": "Woodrow Wilson", "target": "Warren G. Harding", "value": "1" }, { "source": "Warren G. Harding", "target": "Calvin Coolidge", "value": "0" }, { "source": "Calvin Coolidge", "target": "Warren G. Harding", "value": "1" }, { "source": "Calvin Coolidge", "target": "Office Vacant", "value": "0" }, { "source": "Calvin Coolidge", "target": "Charles G. Dawes", "value": "0" }, { "source": "Calvin Coolidge", "target": "Herbert Hoover", "value": "1" }, { "source": "Herbert Hoover", "target": "Charles Curtis", "value": "0" }, { "source": "Herbert Hoover", "target": "Franklin D. Roosevelt", "value": "1" }, { "source": "Franklin D. Roosevelt", "target": "John N. Garner", "value": "0" }, { "source": "Franklin D. Roosevelt", "target": "Henry A. Wallace", "value": "0" }, { "source": "Franklin D. Roosevelt", "target": "Harry S. Truman", "value": "0" }, { "source": "Harry S. Truman", "target": "Franklin D. Roosevelt", "value": "1" }, { "source": "Harry S. Truman", "target": "Office Vacant", "value": "0" }, { "source": "Harry S. Truman", "target": "Alben W. Barkley", "value": "0" }, { "source": "Harry S. Truman", "target": "Dwight D Eisenhower", "value": "1" }, { "source": "Dwight D Eisenhower", "target": "Richard Nixon", "value": "0" }, { "source": "Dwight D Eisenhower", "target": "John F. Kennedy", "value": "1" }, { "source": "John F. Kennedy", "target": "Lyndon B. Johnson", "value": "0" }, { "source": "Lyndon B. Johnson", "target": "John F. Kennedy", "value": "1" }, { "source": "Lyndon B. Johnson", "target": "Office Vacant", "value": "0" }, { "source": "Lyndon B. Johnson", "target": "Hubert Humphrey", "value": "0" }, { "source": "Lyndon B. Johnson", "target": "Richard Nixon", "value": "1" }, { "source": "Richard Nixon", "target": "Spiro Agnew", "value": "0" }, { "source": "Richard Nixon", "target": "Office Vacant", "value": "0" }, { "source": "Richard Nixon", "target": "Gerald Ford", "value": "0" }, { "source": "Gerald Ford", "target": "Richard Nixon", "value": "1" }, { "source": "Gerald Ford", "target": "Office Vacant", "value": "0" }, { "source": "Gerald Ford", "target": "Nelson Rockefeller", "value": "0" }, { "source": "Gerald Ford", "target": "Jimmy Carter", "value": "1" }, { "source": "Jimmy Carter", "target": "Walter Mondale", "value": "0" }, { "source": "Jimmy Carter", "target": "Ronald Reagan", "value": "1" }, { "source": "Ronald Reagan", "target": "George HW Bush", "value": "0" }, { "source": "George HW Bush", "target": "Ronald Reagan", "value": "1" }, { "source": "George HW Bush", "target": "Dan Quayle", "value": "0" }, { "source": "George HW Bush", "target": "Bill Clinton", "value": "1" }, { "source": "Bill Clinton", "target": "Al Gore", "value": "0" }, { "source": "Bill Clinton", "target": "George W. Bush", "value": "1" }, { "source": "Geore W. Bush", "target": "Dick Cheney", "value": "0" }, { "source": "Geore W. Bush", "target": "Barack Obama", "value": "1" }, { "source": "Barack Obama", "target": "Joe Biden", "value": "0" }, { "source": "Barack Obama", "target": "Donald Trump", "value": "1" }, { "source": "Donald Trump", "target": "Mike Pence", "value": "0" } ]; var nodes = {}; // Compute the distinct nodes from the links. links.forEach(function(link) { link.source = nodes[link.source] || (nodes[link.source] = {name: link.source}); link.target = nodes[link.target] || (nodes[link.target] = {name: link.target}); }); var width = 1200, height = 700, color = d3.scale.category20c(); var force = d3.layout.force() .nodes(d3.values(nodes)) .links(links) .size([width, height]) .linkDistance(60) .charge(-250) .on("tick", tick) .start(); // Set the range var v = d3.scale.linear().range([0, 100]); // Scale the range of the data v.domain([0, d3.max(links, function(d) { return d.value; })]); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); // build the arrow. svg.append("svg:defs").selectAll("marker") .data(["end"]) // Different link/path types can be defined here .enter().append("svg:marker") // This section adds in the arrows .attr("id", String) .attr("viewBox", "0 -5 10 10") .attr("refX", 15) .attr("refY", -1.5) .attr("markerWidth", 6) .attr("markerHeight", 6) .attr("orient", "auto") .append("svg:path") .attr("d", "M0,-5L10,0L0,5") // add the links and the arrows var path = svg.append("svg:g").selectAll("path") .data(force.links()) .enter().append("svg:path") .attr("class", function(d) { return "link " + d.type; }) .style("stroke", function(d){ if (d.value == 1){ return d3.rgb(255, 0, 0); } else { return d3.rgb(0, 0, 255); } }); // define the nodes var node = svg.selectAll(".node") .data(force.nodes()) .enter().append("g") .attr("class", "node") .call(force.drag); svg.selectAll(".node").on('dblclick', function(d){ console.log("CLICKETY CLICK"); console.log(d); if(!d.fixed) { d.fixed = 1; d3.select(this) .select("circle") .style("fill", "red"); }else { d.fixed = 0; d3.select(this) .select("circle") .style("fill", "LightGray"); } }); var selection = d3.select("body").datum(43).append('h1'); // add the nodes node.append("circle") .attr("r", function(d,i){ return (3 * d.weight) + 3; }) .style("fill", "LightGray"); node.append("text") .attr("dx", 10) .attr("dy", function(d){ return (5 * d.weight) + 3; }) .text(function(d) { return d.name; }); // nodeUpdate.select("circle") // .style("fill", "red"); // add the curvy lines function tick() { path.attr("d", function(d) { var dx = d.target.x - d.source.x, dy = d.target.y - d.source.y, dr = Math.sqrt(dx * dx + dy * dy); return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y; }); node .attr("transform", function(d) { return "translate(" + dx + "," + dy + ")"; }); };
 path.link { fill: none; stroke: #666; stroke-width: 1.5px; } circle { fill: #ccc; stroke: #fff; stroke: black; stroke-width: 1.5px; } text { fill: #000; font: 10px sans-serif; pointer-events: none; }
 <!DOCTYPE html> <meta charset="utf-8"> <script src="https://d3js.org/d3.v3.min.js"></script> <link rel="stylesheet" type="text/css" href="graph.css"> <script src="graph.js"></script> <body> </body> </html>

Has anybody got any idea why nothing is getting displayed?

I needed to defer the script

<!DOCTYPE html>

  <meta charset="utf-8">

  <script src="https://d3js.org/d3.v3.min.js" defer></script>
  <link rel="stylesheet" type="text/css" href="graph.css">
  <script src="graph.js" defer></script>

<body>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM