| stevenvanrossem | 77524dc | 2017-04-23 02:15:03 +0200 | [diff] [blame] | 1 | var width = 960, |
| 2 | height = 500, |
| stevenvanrossem | f371201 | 2017-05-04 00:01:52 +0200 | [diff] [blame] | 3 | color = d3.scale.category10(); |
| stevenvanrossem | 77524dc | 2017-04-23 02:15:03 +0200 | [diff] [blame] | 4 | |
| stevenvanrossem | 7f397b6 | 2017-04-27 15:09:55 +0200 | [diff] [blame] | 5 | var svg = d3.select("#table_graph").append("svg") |
| stevenvanrossem | 77524dc | 2017-04-23 02:15:03 +0200 | [diff] [blame] | 6 | .attr("width", width) |
| 7 | .attr("height", height); |
| 8 | |
| 9 | var force = d3.layout.force() |
| 10 | .gravity(0.05) |
| 11 | .distance(100) |
| 12 | .charge(-100) |
| 13 | .size([width, height]); |
| 14 | |
| 15 | //d3.json("js/graph.json", function(error, json) { |
| 16 | d3.json("http://127.0.0.1:5001/restapi/network/d3jsgraph", function(error, json) { |
| 17 | if (error) throw error; |
| 18 | |
| 19 | force |
| 20 | .nodes(json.nodes) |
| 21 | .links(json.links) |
| 22 | .start(); |
| 23 | |
| 24 | var link = svg.selectAll(".link") |
| 25 | .data(json.links) |
| 26 | .enter().append("line") |
| 27 | .attr("class", "link"); |
| 28 | |
| 29 | var node = svg.selectAll(".node") |
| 30 | .data(json.nodes) |
| stevenvanrossem | 5f5fd44 | 2017-05-04 00:42:31 +0200 | [diff] [blame^] | 31 | .enter().append("g") |
| stevenvanrossem | 77524dc | 2017-04-23 02:15:03 +0200 | [diff] [blame] | 32 | .attr("class", "node") |
| stevenvanrossem | 5f5fd44 | 2017-05-04 00:42:31 +0200 | [diff] [blame^] | 33 | .call(force.drag) |
| 34 | .on("click", click); |
| stevenvanrossem | 77524dc | 2017-04-23 02:15:03 +0200 | [diff] [blame] | 35 | |
| 36 | //node.append("image") |
| 37 | // .attr("xlink:href", "https://github.com/favicon.ico") |
| 38 | // .attr("x", -8) |
| 39 | // .attr("y", -8) |
| 40 | // .attr("width", 16) |
| 41 | // .attr("height", 16); |
| 42 | node.append("circle") |
| 43 | .attr("r", 10) |
| stevenvanrossem | f371201 | 2017-05-04 00:01:52 +0200 | [diff] [blame] | 44 | .style("fill", function(d) { return color(d.group); }); |
| stevenvanrossem | 77524dc | 2017-04-23 02:15:03 +0200 | [diff] [blame] | 45 | |
| 46 | node.append("text") |
| 47 | .attr("dx", 12) |
| 48 | .attr("dy", ".35em") |
| 49 | .text(function(d) { return d.name }); |
| 50 | |
| 51 | force.on("tick", function() { |
| 52 | link.attr("x1", function(d) { return d.source.x; }) |
| 53 | .attr("y1", function(d) { return d.source.y; }) |
| 54 | .attr("x2", function(d) { return d.target.x; }) |
| 55 | .attr("y2", function(d) { return d.target.y; }); |
| 56 | |
| 57 | node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); |
| 58 | }); |
| stevenvanrossem | 5f5fd44 | 2017-05-04 00:42:31 +0200 | [diff] [blame^] | 59 | |
| 60 | // action to take on mouse click |
| 61 | function click() { |
| 62 | d3.select(this).select("text").transition() |
| 63 | .duration(750) |
| 64 | .attr("x", 22) |
| 65 | .style("stroke", "lightsteelblue") |
| 66 | .style("stroke-width", ".5px") |
| 67 | .style("font", "20px sans-serif"); |
| 68 | d3.select(this).select("circle").transition() |
| 69 | .duration(750) |
| 70 | .attr("r", 16); |
| 71 | } |
| 72 | |
| stevenvanrossem | 77524dc | 2017-04-23 02:15:03 +0200 | [diff] [blame] | 73 | }); |