Matteo Scandolo | 70bc45f | 2016-05-06 14:10:11 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceGrid') |
| 5 | .directive('serviceGraph', function(){ |
| 6 | return { |
| 7 | restrict: 'E', |
| 8 | scope: {}, |
| 9 | bindToController: true, |
| 10 | controllerAs: 'vm', |
| 11 | templateUrl: 'templates/service-graph.tpl.html', |
Matteo Scandolo | 819d13d | 2016-05-06 16:52:58 -0700 | [diff] [blame] | 12 | controller: function($element, GraphService){ |
Matteo Scandolo | 70bc45f | 2016-05-06 14:10:11 -0700 | [diff] [blame] | 13 | |
Matteo Scandolo | 819d13d | 2016-05-06 16:52:58 -0700 | [diff] [blame] | 14 | let svg; |
| 15 | let el = $element[0]; |
| 16 | let node; |
| 17 | let link; |
| 18 | |
| 19 | const tick = (e) => { |
| 20 | // Push different nodes in different directions for clustering. |
| 21 | |
| 22 | node |
| 23 | // .attr('cx', d => d.x) |
| 24 | // .attr('cy', d => d.y) |
| 25 | .attr({ |
| 26 | transform: d => `translate(${d.x}, ${d.y})` |
| 27 | }); |
| 28 | |
| 29 | link.attr('x1', d => d.source.x) |
| 30 | .attr('y1', d => d.source.y) |
| 31 | .attr('x2', d => d.target.x) |
| 32 | .attr('y2', d => d.target.y); |
| 33 | } |
| 34 | |
| 35 | GraphService.loadCoarseData() |
| 36 | .then((res) => { |
| 37 | |
| 38 | // build links |
| 39 | res.tenants = res.tenants.map(t => { |
| 40 | return { |
| 41 | source: t.provider_service, |
| 42 | target: t.subscriber_service |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | // add xos as a node |
| 47 | res.services.push({ |
| 48 | name: 'XOS', |
| 49 | class: 'xos', |
| 50 | x: el.clientWidth / 2, |
| 51 | y: el.clientHeight / 2, |
| 52 | fixed: true |
| 53 | }) |
| 54 | |
| 55 | handleSvg(el); |
| 56 | |
| 57 | var force = d3.layout.force() |
| 58 | .nodes(res.services) |
| 59 | .links(res.tenants) |
| 60 | .charge(-1060) |
| 61 | .gravity(0.1) |
| 62 | .linkDistance(200) |
| 63 | .size([el.clientWidth, el.clientHeight]) |
| 64 | .on('tick', tick) |
| 65 | .start(); |
| 66 | |
| 67 | link = svg.selectAll('.link') |
| 68 | .data(res.tenants).enter().insert('line') |
| 69 | .attr('class', 'link'); |
| 70 | |
| 71 | node = svg.selectAll('.node') |
| 72 | .data(res.services) |
| 73 | .enter().append('g') |
| 74 | .call(force.drag) |
| 75 | .on("mousedown", function() { d3.event.stopPropagation(); }); |
| 76 | |
| 77 | node.append('circle') |
| 78 | .attr({ |
| 79 | class: d => `node ${d.class || ''}`, |
| 80 | r: 10 |
| 81 | }); |
| 82 | |
| 83 | node.append('text') |
| 84 | .attr({ |
| 85 | 'text-anchor': 'middle' |
| 86 | }) |
| 87 | .text(d => d.name) |
| 88 | |
| 89 | node.select('circle') |
| 90 | .attr({ |
| 91 | r: function(d){ |
| 92 | let parent = d3.select(this).node().parentNode; |
| 93 | let sib = d3.select(parent).select('text').node().getBBox() |
| 94 | return (sib.width / 2) + 10 |
| 95 | |
| 96 | } |
| 97 | }) |
| 98 | |
| 99 | }) |
| 100 | |
| 101 | const handleSvg = (el) => { |
| 102 | d3.select(el).select('svg').remove(); |
| 103 | |
| 104 | svg = d3.select(el) |
| 105 | .append('svg') |
| 106 | .style('width', `${el.clientWidth}px`) |
| 107 | .style('height', `${el.clientHeight}px`); |
| 108 | } |
Matteo Scandolo | 70bc45f | 2016-05-06 14:10:11 -0700 | [diff] [blame] | 109 | } |
| 110 | }; |
| 111 | }) |
Matteo Scandolo | 3adf879 | 2016-05-06 15:21:27 -0700 | [diff] [blame] | 112 | })(); |
| 113 | |
| 114 | // Draw services around xos and calculate coarse tenant as links |
| 115 | |
| 116 | // var width = 960, height = 500; |
| 117 | |
| 118 | // var fill = d3.scale.category10(); |
| 119 | |
| 120 | // var nodes = [ |
| 121 | // {id: 1}, |
| 122 | // {id: 2}, |
| 123 | // {id: 3}, |
| 124 | // {id: 4}, |
| 125 | // {id: 5} |
| 126 | // ]; |
| 127 | |
| 128 | // var links = [ |
| 129 | // {source: 1, target: 2}, |
| 130 | // {source: 2, target: 3} |
| 131 | // ]; |
| 132 | |
| 133 | // var svg = d3.select("body").append("svg") |
| 134 | // .attr("width", width) |
| 135 | // .attr("height", height); |
| 136 | |
| 137 | // var force = d3.layout.force() |
| 138 | // .nodes(nodes) |
| 139 | // .links(links) |
| 140 | // .charge(-8*12) |
| 141 | // .gravity(0.1) |
| 142 | // .size([width, height]) |
| 143 | // .on("tick", tick) |
| 144 | // .start(); |
| 145 | |
| 146 | // svg.append('circle') |
| 147 | // .attr({ |
| 148 | // "class": "xos", |
| 149 | // r: 20, |
| 150 | // cx: () => width / 2, |
| 151 | // cy: () => height / 2, |
| 152 | // }) |
| 153 | |
| 154 | // var node = svg.selectAll(".node") |
| 155 | // .data(nodes) |
| 156 | // .enter().append("circle") |
| 157 | // .attr("class", "node") |
| 158 | // .attr("cx", ({ x }) => x) |
| 159 | // .attr("cy", ({ y }) => y) |
| 160 | // .attr("r", 8) |
| 161 | // .style("fill", ({}, index) => fill(index & 3)) |
| 162 | // .style("stroke", ({}, index) => d3.rgb(fill(index & 3)).darker(2)) |
| 163 | // .call(force.drag) |
| 164 | // .on("mousedown", ({}) => d3.event.stopPropagation()); |
| 165 | |
| 166 | // var link = svg.selectAll(".link") |
| 167 | // .data(links).enter().insert("line") |
| 168 | // .attr("class", "link"); |
| 169 | |
| 170 | // function tick(e) { |
| 171 | // // Push different nodes in different directions for clustering. |
| 172 | |
| 173 | // node.attr("cx", function(d) { return d.x; }) |
| 174 | // .attr("cy", function(d) { return d.y; }); |
| 175 | |
| 176 | // link.attr("x1", function(d) { return d.source.x; }) |
| 177 | // .attr("y1", function(d) { return d.source.y; }) |
| 178 | // .attr("x2", function(d) { return d.target.x; }) |
| 179 | // .attr("y2", function(d) { return d.target.y; }); |
| 180 | // } |