Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shiny.addCustomMessageHandler("jsondata", function(links) {
- console.log("got new data:", links);
- console.log(links[0]["Source"]);
- //for(i=0;i<links.length;i++) {
- //var allnodes += links[i]["Source"] ;
- //}
- var width = 950, height=1000,color = d3.scale.category20();
- var svg = d3.select("body").append("svg")
- .attr("width", width)
- .attr("height",height);
- var force = d3.layout.force()
- .size([width,height])
- .linkDistance(130)
- .charge(function (d) {return d.weight * - 150});
- var nodesByName = {};
- //console.log(links);
- //console.log(typeof(links));
- //create nodes for each unique source and target in csv
- links.forEach(function(link) {
- link.source = nodeByName(link.Source);
- link.target = nodeByName(link.Target);
- });
- //extract array of nodes from map by name
- var nodes = d3.values(nodesByName);
- //create the link lines
- var link = svg.selectAll(".link")
- .data(links)
- .enter().append("line")
- .attr("class", "link");
- // Create the node circles.
- var node = svg.selectAll(".node")
- .data(nodes)
- .enter().append("circle")
- .attr("class", "node")
- .attr("r", 4.5)
- .style("fill", function(d) { return color(d.name); })
- .call(force.drag);
- //node.append("svg:text")
- // .text(function (d) { return d.source; })
- // .attr("x", function(d) { return d.x; })
- // .attr("y", function(d) { return d.y; });
- node.append("text")
- .attr("x",12)
- .attr("dy",".35em")
- .text(function(d) { return d.name; });
- // Start the force layout.
- force.nodes(nodes)
- .links(links)
- .on("tick", tick)
- .start();
- 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 nodeByName(name) {
- return nodesByName[name] || (nodesByName[name] = {name: name});
- }
- });
- No Source Target value
- 1 a b 3
- 2 b c 9
- 3 a c 7
- 4 b e 4
- 5 c d 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement