Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | import { EventEmitter } from '@angular/core'; |
| 17 | import { Link } from './link'; |
| 18 | import { Node } from './node'; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 19 | import * as d3 from 'd3-force'; |
Sean Condon | 3dd062f | 2020-04-14 09:25:00 +0100 | [diff] [blame] | 20 | import {LogService} from 'org_onosproject_onos/web/gui2-fw-lib/public_api'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 21 | |
| 22 | const FORCES = { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 23 | COLLISION: 1, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 24 | GRAVITY: 0.4, |
| 25 | FRICTION: 0.7 |
| 26 | }; |
| 27 | |
| 28 | const CHARGES = { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 29 | device: -800, |
| 30 | host: -2000, |
| 31 | region: -800, |
| 32 | _def_: -1200 |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | const LINK_DISTANCE = { |
| 36 | // note: key is link.type |
| 37 | direct: 100, |
| 38 | optical: 120, |
Sean Condon | a3ce00b | 2019-04-10 11:44:01 +0100 | [diff] [blame] | 39 | UiEdgeLink: 3, |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 40 | UiDeviceLink: 100, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 41 | _def_: 50, |
| 42 | }; |
| 43 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 44 | /** |
| 45 | * note: key is link.type |
| 46 | * range: {0.0 ... 1.0} |
| 47 | */ |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 48 | const LINK_STRENGTH = { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 49 | _def_: 0.5 |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | export interface Options { |
| 53 | width: number; |
| 54 | height: number; |
| 55 | } |
| 56 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 57 | /** |
| 58 | * The inspiration for this approach comes from |
| 59 | * https://medium.com/netscape/visualizing-data-with-angular-and-d3-209dde784aeb |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 60 | * |
| 61 | * Do yourself a favour and read https://d3indepth.com/force-layout/ |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 62 | */ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 63 | export class ForceDirectedGraph { |
| 64 | public ticker: EventEmitter<d3.Simulation<Node, Link>> = new EventEmitter(); |
| 65 | public simulation: d3.Simulation<any, any>; |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 66 | public canvasOptions: Options; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 67 | public nodes: Node[] = []; |
| 68 | public links: Link[] = []; |
| 69 | |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 70 | constructor(options: Options, public log: LogService) { |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 71 | this.canvasOptions = options; |
| 72 | const ticker = this.ticker; |
| 73 | |
| 74 | // Creating the force simulation and defining the charges |
| 75 | this.simulation = d3.forceSimulation() |
| 76 | .force('charge', |
| 77 | d3.forceManyBody().strength(this.charges.bind(this))) |
| 78 | // .distanceMin(100).distanceMax(500)) |
| 79 | .force('gravity', |
| 80 | d3.forceManyBody().strength(FORCES.GRAVITY)) |
| 81 | .force('friction', |
| 82 | d3.forceManyBody().strength(FORCES.FRICTION)) |
| 83 | .force('center', |
| 84 | d3.forceCenter(this.canvasOptions.width / 2, this.canvasOptions.height / 2)) |
| 85 | .force('x', d3.forceX()) |
| 86 | .force('y', d3.forceY()) |
| 87 | .on('tick', () => { |
| 88 | ticker.emit(this.simulation); // ForceSvgComponent.ngOnInit listens |
| 89 | }); |
| 90 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Assigning updated node and restarting the simulation |
| 95 | * Setting alpha to 0.3 and it will count down to alphaTarget=0 |
| 96 | */ |
| 97 | public reinitSimulation() { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 98 | this.simulation.nodes(this.nodes); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 99 | this.simulation.force('link', |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 100 | d3.forceLink(this.links) |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 101 | .strength(LINK_STRENGTH._def_) |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 102 | .distance(this.distance.bind(this)) |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 103 | ); |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 104 | this.simulation.alpha(0.3).restart(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 107 | charges(node: Node) { |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 108 | const nodeType = node.nodeType; |
| 109 | return CHARGES[nodeType] || CHARGES._def_; |
| 110 | } |
| 111 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 112 | distance(link: Link) { |
| 113 | const linkType = link.type; |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 114 | return LINK_DISTANCE[linkType] || LINK_DISTANCE._def_; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 117 | stopSimulation() { |
| 118 | this.simulation.stop(); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 119 | this.log.debug('Simulation stopped'); |
| 120 | } |
| 121 | |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 122 | public restartSimulation(alpha: number = 0.3) { |
| 123 | this.simulation.alpha(alpha).restart(); |
| 124 | this.log.debug('Simulation restarted. Alpha:', alpha); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 125 | } |
| 126 | } |