view-grapher.js 21 KB

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