| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2017 SONATA-NFV, IMEC and Paderborn University |
| 3 | ALL RIGHTS RESERVED. |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | Neither the name of the SONATA-NFV, Paderborn University |
| 18 | nor the names of its contributors may be used to endorse or promote |
| 19 | products derived from this software without specific prior written |
| 20 | permission. |
| 21 | |
| 22 | This work has been performed in the framework of the SONATA project, |
| 23 | funded by the European Commission under Grant number 671517 through |
| 24 | the Horizon 2020 and 5G-PPP programmes. The authors would like to |
| 25 | acknowledge the contributions of their colleagues of the SONATA |
| 26 | partner consortium (www.sonata-nfv.eu). |
| 27 | */ |
| 28 | |
| 29 | //functions to make the nodes stick after they have been manually maoved |
| 30 | function tick() { |
| 31 | link.attr("x1", function(d) { return d.source.x; }) |
| 32 | .attr("y1", function(d) { return d.source.y; }) |
| 33 | .attr("x2", function(d) { return d.target.x; }) |
| 34 | .attr("y2", function(d) { return d.target.y; }); |
| 35 | |
| 36 | node.attr("cx", function(d) { return d.x; }) |
| 37 | .attr("cy", function(d) { return d.y; }); |
| 38 | } |
| 39 | |
| 40 | function dragstart(d) { |
| 41 | d3.select(this).classed("fixed", d.fixed = true); |
| 42 | } |
| 43 | |
| 44 | var width = 960, |
| 45 | height = 500, |
| 46 | color = d3.scale.category10(); |
| 47 | |
| 48 | var svg = d3.select("#table_graph").append("svg") |
| 49 | .attr("width", width) |
| 50 | .attr("height", height); |
| 51 | |
| 52 | var force = d3.layout.force() |
| 53 | .gravity(0.05) |
| 54 | .distance(100) |
| 55 | .charge(-100) |
| 56 | .size([width, height]) |
| 57 | .on("tick", tick); |
| 58 | |
| 59 | var drag = force.drag() |
| 60 | .on("dragstart", dragstart); |
| 61 | |
| 62 | d3.json("/restapi/network/d3jsgraph", function(error, json) { |
| 63 | if (error) throw error; |
| 64 | |
| 65 | force |
| 66 | .nodes(json.nodes) |
| 67 | .links(json.links) |
| 68 | .start(); |
| 69 | |
| 70 | var link = svg.selectAll(".link") |
| 71 | .data(json.links) |
| 72 | .enter().append("line") |
| 73 | .attr("class", "link"); |
| 74 | |
| 75 | var node = svg.selectAll(".node") |
| 76 | .data(json.nodes) |
| 77 | .enter().append("g") |
| 78 | .attr("class", "node") |
| 79 | .call(drag); |
| 80 | |
| 81 | node.append("circle") |
| 82 | .attr("r", 10) |
| 83 | .style("fill", function(d) { return color(d.group); }); |
| 84 | |
| 85 | node.append("text") |
| 86 | .attr("dx", 12) |
| 87 | .attr("dy", ".35em") |
| 88 | .text(function(d) { return d.name }); |
| 89 | |
| 90 | force.on("tick", function() { |
| 91 | link.attr("x1", function(d) { return d.source.x; }) |
| 92 | .attr("y1", function(d) { return d.source.y; }) |
| 93 | .attr("x2", function(d) { return d.target.x; }) |
| 94 | .attr("y2", function(d) { return d.target.y; }); |
| 95 | |
| 96 | node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); |
| 97 | }); |
| 98 | |
| 99 | |
| 100 | }); |