Guest User

Untitled

a guest
Jan 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. // Initialize the GraphComponent and place it in the div with CSS selector #graphComponent
  2. graphComponent = new yfiles.view.GraphComponent('#graphComponent')
  3. // conveniently store a reference to the graph that is displayed
  4. graph = graphComponent.graph;
  5.  
  6. graph.edgeDefaults.style = new yfiles.styles.PolylineEdgeStyle({stroke:"2px green", smoothingLength:20, targetArrow:"green default"})
  7. let styles = [];
  8. styles.push(new yfiles.styles.PolylineEdgeStyle({stroke:"4px blue", smoothingLength:20, targetArrow:"blue default"}));
  9. styles.push(new yfiles.styles.PolylineEdgeStyle({stroke:"2px gold", smoothingLength:20, targetArrow:"gold default"}));
  10. styles.push(new yfiles.styles.PolylineEdgeStyle({stroke:"2px red", smoothingLength:20, targetArrow:"red default"}));
  11. styles.push(new yfiles.styles.PolylineEdgeStyle({stroke:"2px gray", smoothingLength:20, targetArrow:"gray default"}));
  12.  
  13. graph.nodeDefaults.style = new yfiles.styles.ShapeNodeStyle({shape:"ellipse", fill:"white", stroke:"2px blue"})
  14.  
  15. // here we define the routes...
  16. let routes = [
  17. [1,2,3,4,5],
  18. [1,7,8,2,3,9,10,4,5,6],
  19. [1,7,13,7,8,2,3,9,10,4,5,6],
  20. [1,7,11,12,4,5,6],
  21. ];
  22.  
  23. let nodes = {};
  24.  
  25. // let's create the graph
  26. routes.forEach((route,routeIndex) => {
  27. // assign the lane to the route index - in case we disable optimization this will be the exact lane..
  28. let lane = routeIndex;
  29. // interconnect all nodes for a route
  30. let previous = null;
  31. route.forEach(element =>{
  32. let newNode;
  33. // see whether this is a new node or an existing one
  34. if (!(newNode = nodes[element])){
  35. // choose a row for the element: 0 for start nodes, 1 for center nodes, 2 for end nodes
  36. let row = element === 1 ? 0 : element === 6 ? 2 : 1;
  37. // create a new node and assign the tag data to it
  38. newNode = nodes[element] = graph.createNode({labels:[String(element)], tag:{element,routeIndex,lane,row}})}
  39. // for all but the first node
  40. if (previous){
  41. // see if there already is a connection between the two
  42. if (!graph.getEdge(previous, newNode)){
  43. // and if there isn't create one and tag it
  44. graph.createEdge({source: previous, target: newNode, tag: {routeIndex, lane}, style:styles[routeIndex % styles.length]});
  45. }
  46. }
  47. // update chain
  48. previous = newNode;
  49. });
  50. });
  51.  
  52. // configure the layout
  53. let layout = new yfiles.hierarchic.HierarchicLayout();
  54. // we want rounded corners so we start with 45 degree routing
  55. layout.edgeLayoutDescriptor.routingStyle = new yfiles.hierarchic.RoutingStyle(yfiles.hierarchic.EdgeRoutingStyle.OCTILINEAR);
  56. // lets add some more constraints
  57. let layoutData = new yfiles.hierarchic.HierarchicLayoutData();
  58. // tell it which edges to align - we align edges if they connect two nodes within a lane
  59. layoutData.criticalEdgePriorities.delegate = edge => edge.sourceNode.tag.lane === edge.targetNode.tag.lane ? 1 : 0;
  60. // optionally treat edges between lanes as undirected - yields more compact results
  61. //layoutData.edgeDirectedness.delegate = edge => edge.sourceNode.tag.lane !== edge.targetNode.tag.lane ? 0 : 1;
  62. // distribute the graph in three rows: start nodes, center nodes, end nodes
  63. // and each node in its lane/column
  64. layoutData.partitionGridData.grid = new yfiles.layout.PartitionGrid(3, 4)
  65. layoutData.partitionGridData.cellIds.contextDelegate = (node, grid) => {
  66. return grid.createCellId(node.tag.row, node.tag.lane)
  67. }
  68. // let the algorithm find a nice column ordering
  69. layoutData.partitionGridData.grid.optimizeColumnOrder = true;
  70. // configure where edges should enter/leave the nodes
  71. layoutData.sourcePortConstraints.delegate = edge => {
  72. // see if we cross a lane border
  73. let routeDelta = edge.sourceNode.tag.lane - edge.targetNode.tag.lane;
  74. if (routeDelta > 0){
  75. // and if so start at the west
  76. return yfiles.layout.PortConstraint.create("WEST", false);
  77. } else if (routeDelta < 0){
  78. // or east respectively
  79. return yfiles.layout.PortConstraint.create("EAST", false);
  80. }
  81. // all other edges should use default behavior
  82. return null;
  83. };
  84. // and the same for the target side
  85. layoutData.targetPortConstraints.delegate = edge => {
  86. let routeDelta = edge.sourceNode.tag.lane - edge.targetNode.tag.lane;
  87. if (routeDelta < 0){
  88. return yfiles.layout.PortConstraint.create("WEST", false);
  89. } else if (routeDelta > 0){
  90. return yfiles.layout.PortConstraint.create("EAST", false);
  91. }
  92. return null;
  93. };
  94.  
  95. // now run the layout and morph the animation for one second
  96. graphComponent.morphLayout({ layout, morphDuration: '1s', layoutData }).catch(console.log);
Add Comment
Please, Sign In to add comment