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