Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 1 | import './coarse.component.scss'; |
| 2 | import * as d3 from 'd3'; |
| 3 | import * as $ from 'jquery'; |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 4 | import * as _ from 'lodash'; |
Matteo Scandolo | 7218159 | 2017-07-25 14:49:40 -0700 | [diff] [blame] | 5 | import {IXosServiceGraphStore} from '../../services/service-graph.store'; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 6 | import {IXosServiceGraph, IXosServiceGraphNode, IXosServiceGraphLink} from '../../interfaces'; |
| 7 | import {XosServiceGraphConfig as config} from '../../graph.config'; |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 8 | import {IXosDebouncer} from '../../../core/services/helpers/debounce.helper'; |
| 9 | import {Subscription} from 'rxjs'; |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 10 | import {IXosGraphHelpers} from '../../services/d3-helpers/graph.helpers'; |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 11 | import {IXosServiceGraphReducer, IXosServiceGraphExtender} from '../../services/graph.extender'; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 12 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 13 | class XosCoarseTenancyGraphCtrl { |
| 14 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 15 | static $inject = [ |
| 16 | '$log', |
| 17 | 'XosServiceGraphStore', |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 18 | 'XosDebouncer', |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 19 | 'XosGraphHelpers', |
| 20 | 'XosServiceGraphExtender' |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 21 | ]; |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 22 | |
| 23 | public graph: IXosServiceGraph; |
| 24 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 25 | private CoarseGraphSubscription: Subscription; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 26 | private svg; |
| 27 | private forceLayout; |
| 28 | private linkGroup; |
| 29 | private nodeGroup; |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 30 | private textSize = 20; |
| 31 | private textOffset = this.textSize / 4; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 32 | |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 33 | // debounced functions |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 34 | private renderGraph; |
| 35 | |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 36 | constructor ( |
| 37 | private $log: ng.ILogService, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 38 | private XosServiceGraphStore: IXosServiceGraphStore, |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 39 | private XosDebouncer: IXosDebouncer, |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 40 | private XosGraphHelpers: IXosGraphHelpers, |
| 41 | private XosServiceGraphExtender: IXosServiceGraphExtender |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 42 | ) { |
| 43 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | $onInit() { |
| 47 | this.renderGraph = this.XosDebouncer.debounce(this._renderGraph, 500, this); |
| 48 | |
| 49 | this.CoarseGraphSubscription = this.XosServiceGraphStore.getCoarse() |
| 50 | .subscribe( |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 51 | (graph: IXosServiceGraph) => { |
| 52 | this.$log.debug(`[XosCoarseTenancyGraph] Coarse Event and render`, graph); |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 53 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 54 | // id there are no data, do nothing |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 55 | if (graph.nodes.length === 0) { |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 56 | return; |
| 57 | } |
Matteo Scandolo | bafd8d6 | 2017-03-29 23:23:00 -0700 | [diff] [blame] | 58 | this.graph = graph; |
| 59 | |
| 60 | _.forEach(this.XosServiceGraphExtender.getCoarse(), (r: IXosServiceGraphReducer) => { |
| 61 | graph = r.reducer(graph); |
| 62 | }); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 63 | this.renderGraph(); |
| 64 | }, |
| 65 | err => { |
| 66 | this.$log.error(`[XosCoarseTenancyGraph] Coarse Event error`, err); |
| 67 | }); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 68 | |
| 69 | this.handleSvg(); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 70 | this.setupForceLayout(); |
| 71 | |
| 72 | $(window).on('resize', () => { |
| 73 | this.setupForceLayout(); |
| 74 | this.renderGraph(); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | $onDestroy() { |
| 79 | this.CoarseGraphSubscription.unsubscribe(); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private _renderGraph() { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 83 | if (!angular.isDefined(this.graph) || !angular.isDefined(this.graph.nodes) || !angular.isDefined(this.graph.links)) { |
| 84 | return; |
| 85 | } |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 86 | this.addNodeLinksToForceLayout(this.graph); |
| 87 | this.renderNodes(this.graph.nodes); |
| 88 | this.renderLinks(this.graph.links); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 91 | private getSvgDimensions(): {width: number, height: number} { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 92 | return { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 93 | width: $('xos-coarse-tenancy-graph svg').width() || 0, |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 94 | height: $('xos-coarse-tenancy-graph svg').height() || 0 |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 95 | }; |
| 96 | } |
| 97 | |
| 98 | private handleSvg() { |
| 99 | this.svg = d3.select('svg'); |
| 100 | |
| 101 | this.svg.append('svg:defs') |
| 102 | .selectAll('marker') |
| 103 | .data(config.markers) |
| 104 | .enter() |
| 105 | .append('svg:marker') |
| 106 | .attr('id', d => d.id) |
| 107 | .attr('viewBox', d => d.viewBox) |
| 108 | .attr('refX', d => d.refX) |
| 109 | .attr('refY', d => d.refY) |
| 110 | .attr('markerWidth', d => d.width) |
| 111 | .attr('markerHeight', d => d.height) |
| 112 | .attr('orient', 'auto') |
| 113 | .attr('class', d => `${d.id}-marker`) |
| 114 | .append('svg:path') |
| 115 | .attr('d', d => d.path); |
| 116 | |
| 117 | this.linkGroup = this.svg.append('g') |
| 118 | .attr({ |
| 119 | class: 'link-group' |
| 120 | }); |
| 121 | |
| 122 | this.nodeGroup = this.svg.append('g') |
| 123 | .attr({ |
| 124 | class: 'node-group' |
| 125 | }); |
| 126 | } |
| 127 | |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 128 | private collide(n: any) { |
| 129 | const svgDim = this.getSvgDimensions(); |
| 130 | const x = Math.max(n.width / 2, Math.min(n.x, svgDim.width - (n.width / 2))); |
| 131 | const y = Math.max(n.height / 2, Math.min(n.y, svgDim.height - (n.height / 2))); |
| 132 | return `${x}, ${y}`; |
| 133 | } |
| 134 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 135 | private setupForceLayout() { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 136 | |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 137 | let svgDim = this.getSvgDimensions(); |
| 138 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 139 | const tick = () => { |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 140 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 141 | this.nodeGroup.selectAll('g.node') |
| 142 | .attr({ |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 143 | transform: d => `translate(${this.collide(d)})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 144 | }); |
| 145 | |
| 146 | this.linkGroup.selectAll('line') |
| 147 | .attr({ |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 148 | x1: l => l.source.x || 0, |
| 149 | y1: l => l.source.y || 0, |
| 150 | x2: l => l.target.x || 0, |
| 151 | y2: l => l.target.y || 0, |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 152 | }); |
| 153 | }; |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 154 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 155 | this.forceLayout = d3.layout.force() |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 156 | .size([svgDim.width, svgDim.height]) |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 157 | .linkDistance(config.force.linkDistance) |
| 158 | .charge(config.force.charge) |
| 159 | .gravity(config.force.gravity) |
| 160 | .on('tick', tick); |
| 161 | } |
| 162 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 163 | private addNodeLinksToForceLayout(data: IXosServiceGraph) { |
| 164 | this.forceLayout |
| 165 | .nodes(data.nodes) |
| 166 | .links(data.links) |
| 167 | .start(); |
| 168 | } |
| 169 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 170 | private renderNodes(nodes: IXosServiceGraphNode[]) { |
| 171 | const self = this; |
| 172 | const node = this.nodeGroup |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 173 | .selectAll('g.node') |
| 174 | .data(nodes, n => n.id); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 175 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 176 | const svgDim = this.getSvgDimensions(); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 177 | const entering = node.enter() |
| 178 | .append('g') |
| 179 | .attr({ |
Matteo Scandolo | 7218159 | 2017-07-25 14:49:40 -0700 | [diff] [blame] | 180 | id: n => n.id, |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 181 | class: n => `node ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`, |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 182 | transform: `translate(${svgDim.width / 2}, ${svgDim.height / 2})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 183 | }) |
| 184 | .call(this.forceLayout.drag) |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 185 | .on('mousedown', () => { |
| 186 | d3.event.stopPropagation(); |
| 187 | }) |
| 188 | .on('mouseup', (d) => { |
| 189 | d.fixed = true; |
| 190 | }); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 191 | |
| 192 | entering.append('rect') |
| 193 | .attr({ |
| 194 | rx: config.node.radius, |
| 195 | ry: config.node.radius |
| 196 | }); |
| 197 | |
| 198 | entering.append('text') |
| 199 | .attr({ |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 200 | 'text-anchor': 'middle', |
| 201 | 'transform': `translate(0,${this.textOffset})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 202 | }) |
| 203 | .text(n => n.label); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 204 | // .text(n => `${n.id} - ${n.label}`); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 205 | |
| 206 | const existing = node.selectAll('rect'); |
| 207 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 208 | // resize node > rect as contained text |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 209 | |
| 210 | existing.each(function(d: any) { |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 211 | const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 212 | const rect = d3.select(this); |
| 213 | rect.attr({ |
| 214 | width: textBBox.width + config.node.padding, |
| 215 | height: textBBox.height + config.node.padding, |
| 216 | x: textBBox.x - (config.node.padding / 2), |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 217 | y: (textBBox.y + self.textOffset) - (config.node.padding / 2) |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 218 | }); |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 219 | d.width = textBBox.width + config.node.padding; |
| 220 | d.height = textBBox.height + config.node.padding; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 221 | }); |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 222 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | private renderLinks(links: IXosServiceGraphLink[]) { |
| 226 | const link = this.linkGroup |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 227 | .selectAll('line') |
| 228 | .data(links, l => l.id); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 229 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 230 | const entering = link.enter(); |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 231 | |
| 232 | // TODO read classes from graph links |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 233 | |
| 234 | entering.append('line') |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 235 | .attr({ |
Matteo Scandolo | 7218159 | 2017-07-25 14:49:40 -0700 | [diff] [blame] | 236 | id: n => n.id, |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 237 | class: l => `link ${this.XosGraphHelpers.parseElemClasses(l.d3Class)}`, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 238 | 'marker-start': 'url(#arrow)' |
| 239 | }); |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | export const XosCoarseTenancyGraph: angular.IComponentOptions = { |
| 244 | template: require('./coarse.component.html'), |
| 245 | controllerAs: 'vm', |
| 246 | controller: XosCoarseTenancyGraphCtrl, |
| 247 | }; |