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 | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 5 | import {IXosServiceGraphStore} from '../../services/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 | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 180 | class: n => `node ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`, |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 181 | transform: `translate(${svgDim.width / 2}, ${svgDim.height / 2})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 182 | }) |
| 183 | .call(this.forceLayout.drag) |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 184 | .on('mousedown', () => { |
| 185 | d3.event.stopPropagation(); |
| 186 | }) |
| 187 | .on('mouseup', (d) => { |
| 188 | d.fixed = true; |
| 189 | }); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 190 | |
| 191 | entering.append('rect') |
| 192 | .attr({ |
| 193 | rx: config.node.radius, |
| 194 | ry: config.node.radius |
| 195 | }); |
| 196 | |
| 197 | entering.append('text') |
| 198 | .attr({ |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 199 | 'text-anchor': 'middle', |
| 200 | 'transform': `translate(0,${this.textOffset})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 201 | }) |
| 202 | .text(n => n.label); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 203 | // .text(n => `${n.id} - ${n.label}`); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 204 | |
| 205 | const existing = node.selectAll('rect'); |
| 206 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 207 | // resize node > rect as contained text |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 208 | |
| 209 | existing.each(function(d: any) { |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 210 | const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 211 | const rect = d3.select(this); |
| 212 | rect.attr({ |
| 213 | width: textBBox.width + config.node.padding, |
| 214 | height: textBBox.height + config.node.padding, |
| 215 | x: textBBox.x - (config.node.padding / 2), |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 216 | y: (textBBox.y + self.textOffset) - (config.node.padding / 2) |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 217 | }); |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 218 | d.width = textBBox.width + config.node.padding; |
| 219 | d.height = textBBox.height + config.node.padding; |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 220 | }); |
Max Chu | 2bfddde | 2017-06-29 13:41:52 -0700 | [diff] [blame] | 221 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | private renderLinks(links: IXosServiceGraphLink[]) { |
| 225 | const link = this.linkGroup |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 226 | .selectAll('line') |
| 227 | .data(links, l => l.id); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 228 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 229 | const entering = link.enter(); |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 230 | |
| 231 | // TODO read classes from graph links |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 232 | |
| 233 | entering.append('line') |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 234 | .attr({ |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 235 | class: l => `link ${this.XosGraphHelpers.parseElemClasses(l.d3Class)}`, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 236 | 'marker-start': 'url(#arrow)' |
| 237 | }); |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | export const XosCoarseTenancyGraph: angular.IComponentOptions = { |
| 242 | template: require('./coarse.component.html'), |
| 243 | controllerAs: 'vm', |
| 244 | controller: XosCoarseTenancyGraphCtrl, |
| 245 | }; |