javascript - d3js - force: nodes and links from json file not loaded in my svg during redrawing -
i'm still trying load nodes , links extern json file. works when outside redraw()
function. goal load them , add additional nodes , link svg
. thats why created redraw()
function make great part of job. inspired force example , other example. nothing appears (i have no error). have ?
my code :
var width = 960, height = 500; var selected_node = null, selected_link = null; var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height) .attr("pointer-events", "all"); var visual = svg .append('svg:g') .append('svg:g') .on("mousemove", mousemove) .on("click", click); visual.append('svg:rect') .attr('width', width) .attr('height', height) .attr('fill', 'white'); var force = d3.layout.force() .size([width, height]) .charge(-400) .on("tick", tick); // future link var drag_line = visual.append("line") .attr("class", "drag_line") .attr("x1", 0) .attr("y1", 0) .attr("x2", 0) .attr("y2", 0); var nodes = force.nodes(), links = force.links(); var node = visual.selectall(".node"), link = visual.selectall(".link"); // allows drag actions var drag = force.drag() .on("dragstart", dragstart); d3.json("graph.json", function(error, graph) { if (error) console.log("error: " + error); nodes = graph.nodes; links = graph.links; }); // add properties links , nodes function tick() { 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; }); node.attr("cx", function (d) { return d.x; }) .attr("cy", function (d) { return d.y; }); } function dragstart(d) { d3.select(this).classed("selected", "selected"); } function redraw() { console.log("redraw start"); force .nodes(nodes) .links(links) link = link.data(links); link.enter().append("line") .attr("class", "link"); link.exit().remove(); node = node.data(nodes); node.enter().append("circle") .attr("class", "node") .attr("r", 6) .attr("fixed", true) .call(drag); node.exit().transition() .attr("r", 0) .remove(); force.start(); } redraw();
as user3714582 proposed, solution call redraw()
(the second function call) @ end of first function. if has add, he/she's welcome.
Comments
Post a Comment