view-grapher.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. var grapher = grapher || {};
  2. var dagre = dagre || require('./dagre');
  3. grapher.Graph = class {
  4. constructor(compound, options) {
  5. this._isCompound = compound;
  6. this._options = options;
  7. this._nodes = new Map();
  8. this._edges = new Map();
  9. this._children = {};
  10. this._children['\x00'] = {};
  11. this._parent = {};
  12. }
  13. graph() {
  14. return this._options;
  15. }
  16. setNode(node) {
  17. this._nodes.set(node.name, node);
  18. if (this._isCompound) {
  19. this._parent[node.name] = '\x00';
  20. this._children[node.name] = {};
  21. this._children['\x00'][node.name] = true;
  22. }
  23. return this;
  24. }
  25. setEdge(edge) {
  26. if (!this._nodes.has(edge.v)) {
  27. throw new grapher.Error();
  28. }
  29. if (!this._nodes.has(edge.w)) {
  30. throw new grapher.Error();
  31. }
  32. const key = edge.v + ' ' + edge.w + ' ';
  33. if (!this._edges.has(key)) {
  34. this._edges.set(key, edge);
  35. }
  36. return this;
  37. }
  38. setParent(node, parent) {
  39. if (!this._isCompound) {
  40. throw new Error("Cannot set parent in a non-compound graph");
  41. }
  42. parent += "";
  43. for (let ancestor = parent; ancestor; ancestor = this.parent(ancestor)) {
  44. if (ancestor === node) {
  45. throw new Error("Setting " + parent + " as parent of " + node + " would create a cycle");
  46. }
  47. }
  48. delete this._children[this._parent[node]][node];
  49. this._parent[node] = parent;
  50. this._children[parent][node] = true;
  51. return this;
  52. }
  53. nodes() {
  54. return this._nodes;
  55. }
  56. hasNode(key) {
  57. return this._nodes.has(key);
  58. }
  59. node(key) {
  60. return this._nodes.get(key);
  61. }
  62. edges() {
  63. return Array.from(this._edges.values());
  64. }
  65. edge(key) {
  66. return key;
  67. }
  68. parent(key) {
  69. if (this._isCompound) {
  70. const parent = this._parent[key];
  71. if (parent !== '\x00') {
  72. return parent;
  73. }
  74. }
  75. }
  76. children(key) {
  77. key = key === undefined ? '\x00' : key;
  78. if (this._isCompound) {
  79. const children = this._children[key];
  80. if (children) {
  81. return Object.keys(children);
  82. }
  83. }
  84. else if (key === '\x00') {
  85. return this.nodes().keys();
  86. }
  87. else if (this.hasNode(key)) {
  88. return [];
  89. }
  90. }
  91. build(document, originElement) {
  92. const createElement = (name) => {
  93. return document.createElementNS('http://www.w3.org/2000/svg', name);
  94. };
  95. const createGroup = (name) => {
  96. const element = createElement('g');
  97. element.setAttribute('id', name);
  98. element.setAttribute('class', name);
  99. originElement.appendChild(element);
  100. return element;
  101. };
  102. const clusterGroup = createGroup('clusters');
  103. const edgePathGroup = createGroup('edge-paths');
  104. const edgeLabelGroup = createGroup('edge-labels');
  105. const nodeGroup = createGroup('nodes');
  106. const edgePathGroupDefs = createElement('defs');
  107. edgePathGroup.appendChild(edgePathGroupDefs);
  108. const marker = (id) => {
  109. const element = createElement('marker');
  110. element.setAttribute('id', id);
  111. element.setAttribute('viewBox', '0 0 10 10');
  112. element.setAttribute('refX', 9);
  113. element.setAttribute('refY', 5);
  114. element.setAttribute('markerUnits', 'strokeWidth');
  115. element.setAttribute('markerWidth', 8);
  116. element.setAttribute('markerHeight', 6);
  117. element.setAttribute('orient', 'auto');
  118. const markerPath = createElement('path');
  119. markerPath.setAttribute('d', 'M 0 0 L 10 5 L 0 10 L 4 5 z');
  120. markerPath.style.setProperty('stroke-width', 1);
  121. element.appendChild(markerPath);
  122. return element;
  123. };
  124. edgePathGroupDefs.appendChild(marker("arrowhead-vee"));
  125. edgePathGroupDefs.appendChild(marker("arrowhead-vee-select"));
  126. for (const nodeId of this.nodes().keys()) {
  127. const node = this.node(nodeId);
  128. if (this.children(nodeId).length == 0) {
  129. // node
  130. node.build(document, nodeGroup);
  131. }
  132. else {
  133. // cluster
  134. node.rectangle = createElement('rect');
  135. if (node.rx) {
  136. node.rectangle.setAttribute('rx', node.rx);
  137. }
  138. if (node.ry) {
  139. node.rectangle.setAttribute('ry', node.ry);
  140. }
  141. node.element = createElement('g');
  142. node.element.setAttribute('class', 'cluster');
  143. node.element.appendChild(node.rectangle);
  144. clusterGroup.appendChild(node.element);
  145. }
  146. }
  147. for (const edgeId of this.edges()) {
  148. const edge = this.edge(edgeId);
  149. edge.build(document, edgePathGroup, edgeLabelGroup);
  150. }
  151. }
  152. layout() {
  153. dagre.layout(this);
  154. for (const nodeId of this.nodes().keys()) {
  155. const node = this.node(nodeId);
  156. if (this.children(nodeId).length == 0) {
  157. // node
  158. node.layout();
  159. }
  160. else {
  161. // cluster
  162. const node = this.node(nodeId);
  163. node.element.setAttribute('transform', 'translate(' + node.x + ',' + node.y + ')');
  164. node.rectangle.setAttribute('x', - node.width / 2);
  165. node.rectangle.setAttribute('y', - node.height / 2 );
  166. node.rectangle.setAttribute('width', node.width);
  167. node.rectangle.setAttribute('height', node.height);
  168. }
  169. }
  170. for (const edgeId of this.edges()) {
  171. const edge = this.edge(edgeId);
  172. edge.layout();
  173. }
  174. }
  175. };
  176. grapher.Node = class {
  177. constructor() {
  178. this._blocks = [];
  179. }
  180. header() {
  181. const block = new grapher.Node.Header();
  182. this._blocks.push(block);
  183. return block;
  184. }
  185. list() {
  186. const block = new grapher.Node.List();
  187. this._blocks.push(block);
  188. return block;
  189. }
  190. build(document, contextElement) {
  191. const createElement = (name) => {
  192. return document.createElementNS('http://www.w3.org/2000/svg', name);
  193. };
  194. this.label = createElement('g');
  195. this.element = createElement('g');
  196. if (this.id) {
  197. this.element.setAttribute('id', this.id);
  198. }
  199. this.element.setAttribute('class', this.class ? 'node ' + this.class : 'node');
  200. this.element.style.opacity = 0;
  201. this.element.appendChild(this.label);
  202. contextElement.appendChild(this.element);
  203. // contextElement.appendChild(this.label);
  204. let width = 0;
  205. let height = 0;
  206. const tops = [];
  207. for (const block of this._blocks) {
  208. tops.push(height);
  209. block.build(document, this.label);
  210. if (width < block.width) {
  211. width = block.width;
  212. }
  213. height = height + block.height;
  214. }
  215. for (let i = 0; i < this._blocks.length; i++) {
  216. const top = tops.shift();
  217. this._blocks[i].update(this.label, top, width, i == 0, i == this._blocks.length - 1);
  218. }
  219. const borderElement = createElement('path');
  220. borderElement.setAttribute('class', [ 'node', 'border' ].join(' '));
  221. borderElement.setAttribute('d', grapher.Node.roundedRect(0, 0, width, height, true, true, true, true));
  222. this.label.appendChild(borderElement);
  223. const nodeBox = this.label.getBBox();
  224. const nodeX = - nodeBox.width / 2;
  225. const nodeY = - nodeBox.height / 2;
  226. this.width = nodeBox.width;
  227. this.height = nodeBox.height;
  228. this.label.setAttribute('transform', 'translate(' + nodeX + ',' + nodeY + ')');
  229. }
  230. layout() {
  231. this.element.setAttribute('transform', 'translate(' + this.x + ',' + this.y + ')');
  232. this.element.style.opacity = 1;
  233. }
  234. static roundedRect(x, y, width, height, r1, r2, r3, r4) {
  235. const radius = 5;
  236. r1 = r1 ? radius : 0;
  237. r2 = r2 ? radius : 0;
  238. r3 = r3 ? radius : 0;
  239. r4 = r4 ? radius : 0;
  240. return "M" + (x + r1) + "," + y +
  241. "h" + (width - r1 - r2) +
  242. "a" + r2 + "," + r2 + " 0 0 1 " + r2 + "," + r2 +
  243. "v" + (height - r2 - r3) +
  244. "a" + r3 + "," + r3 + " 0 0 1 " + -r3 + "," + r3 +
  245. "h" + (r3 + r4 - width) +
  246. "a" + r4 + "," + r4 + " 0 0 1 " + -r4 + "," + -r4 +
  247. 'v' + (-height + r4 + r1) +
  248. "a" + r1 + "," + r1 + " 0 0 1 " + r1 + "," + -r1 +
  249. "z";
  250. }
  251. };
  252. grapher.Node.Header = class {
  253. constructor() {
  254. this._items = [];
  255. }
  256. add(id, classList, content, tooltip, handler) {
  257. this._items.push({
  258. id: id,
  259. classList: classList,
  260. content: content,
  261. tooltip: tooltip,
  262. handler: handler
  263. });
  264. }
  265. build(document, parentElement) {
  266. this._document = document;
  267. this._width = 0;
  268. this._height = 0;
  269. this._elements = [];
  270. let x = 0;
  271. const y = 0;
  272. for (const item of this._items) {
  273. const yPadding = 4;
  274. const xPadding = 7;
  275. const element = this.createElement('g');
  276. const classList = [ 'node-item' ];
  277. parentElement.appendChild(element);
  278. const pathElement = this.createElement('path');
  279. const textElement = this.createElement('text');
  280. element.appendChild(pathElement);
  281. element.appendChild(textElement);
  282. if (item.classList) {
  283. classList.push(...item.classList);
  284. }
  285. element.setAttribute('class', classList.join(' '));
  286. if (item.id) {
  287. element.setAttribute('id', item.id);
  288. }
  289. if (item.handler) {
  290. element.addEventListener('click', item.handler);
  291. }
  292. if (item.tooltip) {
  293. const titleElement = this.createElement('title');
  294. titleElement.textContent = item.tooltip;
  295. element.appendChild(titleElement);
  296. }
  297. if (item.content) {
  298. textElement.textContent = item.content;
  299. }
  300. const boundingBox = textElement.getBBox();
  301. const width = boundingBox.width + xPadding + xPadding;
  302. const height = boundingBox.height + yPadding + yPadding;
  303. this._elements.push({
  304. 'group': element,
  305. 'text': textElement,
  306. 'path': pathElement,
  307. 'x': x, 'y': y,
  308. 'width': width, 'height': height,
  309. 'tx': xPadding, 'ty': yPadding - boundingBox.y,
  310. });
  311. x += width;
  312. if (this._height < height) {
  313. this._height = height;
  314. }
  315. if (x > this._width) {
  316. this._width = x;
  317. }
  318. }
  319. }
  320. get width() {
  321. return this._width;
  322. }
  323. get height() {
  324. return this._height;
  325. }
  326. update(parentElement, top, width, first, last) {
  327. const dx = width - this._width;
  328. let i;
  329. let element;
  330. for (i = 0; i < this._elements.length; i++) {
  331. element = this._elements[i];
  332. if (i == 0) {
  333. element.width = element.width + dx;
  334. }
  335. else {
  336. element.x = element.x + dx;
  337. element.tx = element.tx + dx;
  338. }
  339. element.y = element.y + top;
  340. }
  341. for (i = 0; i < this._elements.length; i++) {
  342. element = this._elements[i];
  343. element.group.setAttribute('transform', 'translate(' + element.x + ',' + element.y + ')');
  344. const r1 = i == 0 && first;
  345. const r2 = i == this._elements.length - 1 && first;
  346. const r3 = i == this._elements.length - 1 && last;
  347. const r4 = i == 0 && last;
  348. element.path.setAttribute('d', grapher.Node.roundedRect(0, 0, element.width, element.height, r1, r2, r3, r4));
  349. element.text.setAttribute('x', 6);
  350. element.text.setAttribute('y', element.ty);
  351. }
  352. let lineElement;
  353. for (i = 0; i < this._elements.length; i++) {
  354. element = this._elements[i];
  355. if (i != 0) {
  356. lineElement = this.createElement('line');
  357. lineElement.setAttribute('class', 'node');
  358. lineElement.setAttribute('x1', element.x);
  359. lineElement.setAttribute('x2', element.x);
  360. lineElement.setAttribute('y1', top);
  361. lineElement.setAttribute('y2', top + this._height);
  362. parentElement.appendChild(lineElement);
  363. }
  364. }
  365. if (!first) {
  366. lineElement = this.createElement('line');
  367. lineElement.setAttribute('class', 'node');
  368. lineElement.setAttribute('x1', 0);
  369. lineElement.setAttribute('x2', width);
  370. lineElement.setAttribute('y1', top);
  371. lineElement.setAttribute('y2', top);
  372. parentElement.appendChild(lineElement);
  373. }
  374. }
  375. createElement(name) {
  376. return this._document.createElementNS('http://www.w3.org/2000/svg', name);
  377. }
  378. };
  379. grapher.Node.List = class {
  380. constructor() {
  381. this._items = [];
  382. }
  383. add(id, name, value, tooltip, separator) {
  384. this._items.push({ id: id, name: name, value: value, tooltip: tooltip, separator: separator });
  385. }
  386. get handler() {
  387. return this._handler;
  388. }
  389. set handler(handler) {
  390. this._handler = handler;
  391. }
  392. build(document, parentElement) {
  393. this._document = document;
  394. this._width = 0;
  395. this._height = 0;
  396. const x = 0;
  397. const y = 0;
  398. this._element = this.createElement('g');
  399. this._element.setAttribute('class', 'node-attribute');
  400. parentElement.appendChild(this._element);
  401. if (this._handler) {
  402. this._element.addEventListener('click', this._handler);
  403. }
  404. this._backgroundElement = this.createElement('path');
  405. this._element.appendChild(this._backgroundElement);
  406. this._element.setAttribute('transform', 'translate(' + x + ',' + y + ')');
  407. this._height += 3;
  408. for (const item of this._items) {
  409. const yPadding = 1;
  410. const xPadding = 6;
  411. const textElement = this.createElement('text');
  412. if (item.id) {
  413. textElement.setAttribute('id', item.id);
  414. }
  415. textElement.setAttribute('xml:space', 'preserve');
  416. this._element.appendChild(textElement);
  417. if (item.tooltip) {
  418. const titleElement = this.createElement('title');
  419. titleElement.textContent = item.tooltip;
  420. textElement.appendChild(titleElement);
  421. }
  422. const textNameElement = this.createElement('tspan');
  423. textNameElement.textContent = item.name;
  424. if (item.separator.trim() != '=') {
  425. textNameElement.style.fontWeight = 'bold';
  426. }
  427. textElement.appendChild(textNameElement);
  428. const textValueElement = this.createElement('tspan');
  429. textValueElement.textContent = item.separator + item.value;
  430. textElement.appendChild(textValueElement);
  431. const size = textElement.getBBox();
  432. const width = xPadding + size.width + xPadding;
  433. if (this._width < width) {
  434. this._width = width;
  435. }
  436. textElement.setAttribute('x', x + xPadding);
  437. textElement.setAttribute('y', this._height + yPadding - size.y);
  438. this._height += yPadding + size.height + yPadding;
  439. }
  440. this._height += 3;
  441. if (this._width < 75) {
  442. this._width = 75;
  443. }
  444. }
  445. get width() {
  446. return this._width;
  447. }
  448. get height() {
  449. return this._height;
  450. }
  451. update(parentElement, top, width , first, last) {
  452. this._element.setAttribute('transform', 'translate(0,' + top + ')');
  453. const r1 = first;
  454. const r2 = first;
  455. const r3 = last;
  456. const r4 = last;
  457. this._backgroundElement.setAttribute('d', grapher.Node.roundedRect(0, 0, width, this._height, r1, r2, r3, r4));
  458. if (!first) {
  459. const lineElement = this.createElement('line');
  460. lineElement.setAttribute('class', 'node');
  461. lineElement.setAttribute('x1', 0);
  462. lineElement.setAttribute('x2', width);
  463. lineElement.setAttribute('y1', 0);
  464. lineElement.setAttribute('y2', 0);
  465. this._element.appendChild(lineElement);
  466. }
  467. }
  468. createElement(name) {
  469. return this._document.createElementNS('http://www.w3.org/2000/svg', name);
  470. }
  471. };
  472. class Path {
  473. constructor() {
  474. this._x0 = null;
  475. this._y0 = null;
  476. this._x1 = null;
  477. this._y1 = null;
  478. this._data = '';
  479. }
  480. moveTo(x, y) {
  481. this._data += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y);
  482. }
  483. lineTo(x, y) {
  484. this._data += "L" + (this._x1 = +x) + "," + (this._y1 = +y);
  485. }
  486. bezierCurveTo(x1, y1, x2, y2, x, y) {
  487. this._data += "C" + (+x1) + "," + (+y1) + "," + (+x2) + "," + (+y2) + "," + (this._x1 = +x) + "," + (this._y1 = +y);
  488. }
  489. closePath() {
  490. if (this._x1 !== null) {
  491. this._x1 = this._x0;
  492. this._y1 = this._y0;
  493. this._data += "Z";
  494. }
  495. }
  496. get data() {
  497. return this._data;
  498. }
  499. }
  500. class Curve {
  501. constructor(context) {
  502. this._context = context;
  503. }
  504. lineStart() {
  505. this._x0 = NaN;
  506. this._x1 = NaN;
  507. this._y0 = NaN;
  508. this._y1 = NaN;
  509. this._point = 0;
  510. }
  511. lineEnd() {
  512. switch (this._point) {
  513. case 3:
  514. this.curve(this._x1, this._y1);
  515. this._context.lineTo(this._x1, this._y1);
  516. break;
  517. case 2:
  518. this._context.lineTo(this._x1, this._y1);
  519. break;
  520. }
  521. if (this._line || (this._line !== 0 && this._point === 1)) {
  522. this._context.closePath();
  523. }
  524. this._line = 1 - this._line;
  525. }
  526. point(x, y) {
  527. x = +x;
  528. y = +y;
  529. switch (this._point) {
  530. case 0:
  531. this._point = 1;
  532. if (this._line) {
  533. this._context.lineTo(x, y);
  534. }
  535. else {
  536. this._context.moveTo(x, y);
  537. }
  538. break;
  539. case 1:
  540. this._point = 2;
  541. break;
  542. case 2:
  543. this._point = 3;
  544. this._context.lineTo((5 * this._x0 + this._x1) / 6, (5 * this._y0 + this._y1) / 6);
  545. this.curve(x, y);
  546. break;
  547. default:
  548. this.curve(x, y);
  549. break;
  550. }
  551. this._x0 = this._x1;
  552. this._x1 = x;
  553. this._y0 = this._y1;
  554. this._y1 = y;
  555. }
  556. curve(x, y) {
  557. this._context.bezierCurveTo(
  558. (2 * this._x0 + this._x1) / 3,
  559. (2 * this._y0 + this._y1) / 3,
  560. (this._x0 + 2 * this._x1) / 3,
  561. (this._y0 + 2 * this._y1) / 3,
  562. (this._x0 + 4 * this._x1 + x) / 6,
  563. (this._y0 + 4 * this._y1 + y) / 6
  564. );
  565. }
  566. }
  567. grapher.Edge = class {
  568. constructor(from, to) {
  569. this.from = from;
  570. this.to = to;
  571. }
  572. get arrowhead() {
  573. return 'vee';
  574. }
  575. build(document, edgePathGroupElement, edgeLabelGroupElement) {
  576. const createElement = (name) => {
  577. return document.createElementNS('http://www.w3.org/2000/svg', name);
  578. };
  579. this.element = createElement('path');
  580. if (this.id) {
  581. this.element.setAttribute('id', this.id);
  582. }
  583. this.element.setAttribute('class', this.class ? 'edge-path ' + this.class : 'edge-path');
  584. edgePathGroupElement.appendChild(this.element);
  585. if (this.label) {
  586. const tspan = createElement('tspan');
  587. tspan.setAttribute('xml:space', 'preserve');
  588. tspan.setAttribute('dy', '1em');
  589. tspan.setAttribute('x', '1');
  590. tspan.appendChild(document.createTextNode(this.label));
  591. const text = createElement('text');
  592. text.appendChild(tspan);
  593. const textContainer = createElement('g');
  594. textContainer.appendChild(text);
  595. this.labelElement = createElement('g');
  596. this.labelElement.style.opacity = 0;
  597. this.labelElement.setAttribute('class', 'edge-label');
  598. if (this.id) {
  599. this.labelElement.setAttribute('id', 'edge-label-' + this.id);
  600. }
  601. this.labelElement.appendChild(textContainer);
  602. edgeLabelGroupElement.appendChild(this.labelElement);
  603. const edgeBox = textContainer.getBBox();
  604. const edgeX = - edgeBox.width / 2;
  605. const edgeY = - edgeBox.height / 2;
  606. textContainer.setAttribute('transform', 'translate(' + edgeX + ',' + edgeY + ')');
  607. this.width = edgeBox.width;
  608. this.height = edgeBox.height;
  609. }
  610. }
  611. layout() {
  612. const edgePath = grapher.Edge._computeCurvePath(this, this.from, this.to);
  613. this.element.setAttribute('d', edgePath);
  614. if (this.labelElement) {
  615. this.labelElement.setAttribute('transform', 'translate(' + this.x + ',' + this.y + ')');
  616. this.labelElement.style.opacity = 1;
  617. }
  618. }
  619. static _computeCurvePath(edge, tail, head) {
  620. const points = edge.points.slice(1, edge.points.length - 1);
  621. points.unshift(grapher.Edge._intersectRect(tail, points[0]));
  622. points.push(grapher.Edge._intersectRect(head, points[points.length - 1]));
  623. const path = new Path();
  624. const curve = new Curve(path);
  625. for (let i = 0; i < points.length; i++) {
  626. const point = points[i];
  627. if (i == 0) {
  628. curve.lineStart();
  629. }
  630. curve.point(point.x, point.y);
  631. if (i == points.length - 1) {
  632. curve.lineEnd();
  633. }
  634. }
  635. return path.data;
  636. }
  637. static _intersectRect(node, point) {
  638. const x = node.x;
  639. const y = node.y;
  640. const dx = point.x - x;
  641. const dy = point.y - y;
  642. let w = node.width / 2;
  643. let h = node.height / 2;
  644. let sx;
  645. let sy;
  646. if (Math.abs(dy) * w > Math.abs(dx) * h) {
  647. if (dy < 0) {
  648. h = -h;
  649. }
  650. sx = dy === 0 ? 0 : h * dx / dy;
  651. sy = h;
  652. }
  653. else {
  654. if (dx < 0) {
  655. w = -w;
  656. }
  657. sx = w;
  658. sy = dx === 0 ? 0 : w * dy / dx;
  659. }
  660. return {
  661. x: x + sx,
  662. y: y + sy
  663. };
  664. }
  665. };
  666. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  667. module.exports.Graph = grapher.Graph;
  668. module.exports.Node = grapher.Node;
  669. module.exports.Edge = grapher.Edge;
  670. }