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