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 | |
| 91 | private getSvgDimensions(): {width: number, heigth: number} { |
| 92 | return { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 93 | width: $('xos-coarse-tenancy-graph svg').width() || 0, |
| 94 | heigth: $('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 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 128 | private setupForceLayout() { |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 129 | |
| 130 | const tick = () => { |
| 131 | this.nodeGroup.selectAll('g.node') |
| 132 | .attr({ |
| 133 | transform: d => `translate(${d.x}, ${d.y})` |
| 134 | }); |
| 135 | |
| 136 | this.linkGroup.selectAll('line') |
| 137 | .attr({ |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 138 | x1: l => l.source.x || 0, |
| 139 | y1: l => l.source.y || 0, |
| 140 | x2: l => l.target.x || 0, |
| 141 | y2: l => l.target.y || 0, |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 142 | }); |
| 143 | }; |
| 144 | const svgDim = this.getSvgDimensions(); |
| 145 | this.forceLayout = d3.layout.force() |
| 146 | .size([svgDim.width, svgDim.heigth]) |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 147 | .linkDistance(config.force.linkDistance) |
| 148 | .charge(config.force.charge) |
| 149 | .gravity(config.force.gravity) |
| 150 | .on('tick', tick); |
| 151 | } |
| 152 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 153 | private addNodeLinksToForceLayout(data: IXosServiceGraph) { |
| 154 | this.forceLayout |
| 155 | .nodes(data.nodes) |
| 156 | .links(data.links) |
| 157 | .start(); |
| 158 | } |
| 159 | |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 160 | private renderNodes(nodes: IXosServiceGraphNode[]) { |
| 161 | const self = this; |
| 162 | const node = this.nodeGroup |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 163 | .selectAll('g.node') |
| 164 | .data(nodes, n => n.id); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 165 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 166 | const svgDim = this.getSvgDimensions(); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 167 | const entering = node.enter() |
| 168 | .append('g') |
| 169 | .attr({ |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 170 | class: n => `node ${this.XosGraphHelpers.parseElemClasses(n.d3Class)}`, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 171 | transform: `translate(${svgDim.width / 2}, ${svgDim.heigth / 2})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 172 | }) |
| 173 | .call(this.forceLayout.drag) |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 174 | .on('mousedown', () => { |
| 175 | d3.event.stopPropagation(); |
| 176 | }) |
| 177 | .on('mouseup', (d) => { |
| 178 | d.fixed = true; |
| 179 | }); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 180 | |
| 181 | entering.append('rect') |
| 182 | .attr({ |
| 183 | rx: config.node.radius, |
| 184 | ry: config.node.radius |
| 185 | }); |
| 186 | |
| 187 | entering.append('text') |
| 188 | .attr({ |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 189 | 'text-anchor': 'middle', |
| 190 | 'transform': `translate(0,${this.textOffset})` |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 191 | }) |
| 192 | .text(n => n.label); |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 193 | // .text(n => `${n.id} - ${n.label}`); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 194 | |
| 195 | const existing = node.selectAll('rect'); |
| 196 | |
| 197 | |
| 198 | // resize node > rect as contained text |
| 199 | existing.each(function() { |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 200 | const textBBox = self.XosGraphHelpers.getSiblingTextBBox(this); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 201 | const rect = d3.select(this); |
| 202 | rect.attr({ |
| 203 | width: textBBox.width + config.node.padding, |
| 204 | height: textBBox.height + config.node.padding, |
| 205 | x: textBBox.x - (config.node.padding / 2), |
Matteo Scandolo | 6a7435f | 2017-03-24 18:07:17 -0700 | [diff] [blame] | 206 | y: (textBBox.y + self.textOffset) - (config.node.padding / 2) |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 207 | }); |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | private renderLinks(links: IXosServiceGraphLink[]) { |
| 212 | const link = this.linkGroup |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 213 | .selectAll('line') |
| 214 | .data(links, l => l.id); |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 215 | |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 216 | const entering = link.enter(); |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 217 | |
| 218 | // TODO read classes from graph links |
Matteo Scandolo | 0c61c9b | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 219 | |
| 220 | entering.append('line') |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 221 | .attr({ |
Matteo Scandolo | 7629cc4 | 2017-03-13 14:12:15 -0700 | [diff] [blame] | 222 | class: l => `link ${this.XosGraphHelpers.parseElemClasses(l.d3Class)}`, |
Matteo Scandolo | a160eef | 2017-03-06 17:21:26 -0800 | [diff] [blame] | 223 | 'marker-start': 'url(#arrow)' |
| 224 | }); |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | export const XosCoarseTenancyGraph: angular.IComponentOptions = { |
| 229 | template: require('./coarse.component.html'), |
| 230 | controllerAs: 'vm', |
| 231 | controller: XosCoarseTenancyGraphCtrl, |
| 232 | }; |