sigma.exporters.svg.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ;(function(undefined) {
  2. 'use strict';
  3. /**
  4. * Sigma SVG Exporter
  5. * ===================
  6. *
  7. * This plugin is designed to export a graph to a svg file that can be
  8. * downloaded or just used elsewhere.
  9. *
  10. * Author: Guillaume Plique (Yomguithereal)
  11. * Version: 0.0.1
  12. */
  13. // Terminating if sigma were not to be found
  14. if (typeof sigma === 'undefined')
  15. throw 'sigma.renderers.snapshot: sigma not in scope.';
  16. /**
  17. * Polyfills
  18. */
  19. var URL = this.URL || this.webkitURL || this;
  20. /**
  21. * Utilities
  22. */
  23. function createBlob(data) {
  24. return new Blob(
  25. [data],
  26. {type: 'image/svg+xml;charset=utf-8'}
  27. );
  28. }
  29. function download(string, filename) {
  30. // Creating blob href
  31. var blob = createBlob(string);
  32. // Anchor
  33. var o = {};
  34. o.anchor = document.createElement('a');
  35. o.anchor.setAttribute('href', URL.createObjectURL(blob));
  36. o.anchor.setAttribute('download', filename);
  37. // Click event
  38. var event = document.createEvent('MouseEvent');
  39. event.initMouseEvent('click', true, false, window, 0, 0, 0 ,0, 0,
  40. false, false, false, false, 0, null);
  41. URL.revokeObjectURL(blob);
  42. o.anchor.dispatchEvent(event);
  43. delete o.anchor;
  44. }
  45. /**
  46. * Defaults
  47. */
  48. var DEFAULTS = {
  49. size: '1000',
  50. width: '1000',
  51. height: '1000',
  52. classes: true,
  53. labels: true,
  54. data: false,
  55. download: false,
  56. filename: 'graph.svg'
  57. };
  58. var XMLNS = 'http://www.w3.org/2000/svg';
  59. /**
  60. * Subprocesses
  61. */
  62. function optimize(svg, prefix, params) {
  63. var nodeColorIndex = {},
  64. edgeColorIndex = {},
  65. count = 0,
  66. color,
  67. style,
  68. styleText = '',
  69. f,
  70. i,
  71. l;
  72. // Creating style tag if needed
  73. if (params.classes) {
  74. style = document.createElementNS(XMLNS, 'style');
  75. style.setAttribute('type', 'text/css')
  76. svg.insertBefore(style, svg.firstChild);
  77. }
  78. // Iterating over nodes
  79. var nodes = svg.querySelectorAll('[id="' + prefix + '-group-nodes"] > [class="' + prefix + '-node"]');
  80. for (i = 0, l = nodes.length, f = true; i < l; i++) {
  81. color = nodes[i].getAttribute('fill');
  82. if (!params.data)
  83. nodes[i].removeAttribute('data-node-id');
  84. if (params.classes) {
  85. if (!(color in nodeColorIndex)) {
  86. nodeColorIndex[color] = (f ? prefix + '-node' : 'c-' + (count++));
  87. styleText += '.' + nodeColorIndex[color] + '{fill: ' + color + '}';
  88. }
  89. if (nodeColorIndex[color] !== prefix + '-node')
  90. nodes[i].setAttribute('class', nodes[i].getAttribute('class') + ' ' + nodeColorIndex[color]);
  91. nodes[i].removeAttribute('fill');
  92. }
  93. f = false;
  94. }
  95. // Iterating over edges
  96. var edges = svg.querySelectorAll('[id="' + prefix + '-group-edges"] > [class="' + prefix + '-edge"]');
  97. for (i = 0, l = edges.length, f = true; i < l; i++) {
  98. color = edges[i].getAttribute('stroke');
  99. if (!params.data)
  100. edges[i].removeAttribute('data-edge-id');
  101. if (params.classes) {
  102. if (!(color in edgeColorIndex)) {
  103. edgeColorIndex[color] = (f ? prefix + '-edge' : 'c-' + (count++));
  104. styleText += '.' + edgeColorIndex[color] + '{stroke: ' + color + '}';
  105. }
  106. if (edgeColorIndex[color] !== prefix + '-edge')
  107. edges[i].setAttribute('class', edges[i].getAttribute('class') + ' ' + edgeColorIndex[color]);
  108. edges[i].removeAttribute('stroke');
  109. }
  110. f = false;
  111. }
  112. if (params.classes)
  113. style.appendChild(document.createTextNode(styleText));
  114. }
  115. /**
  116. * Extending prototype
  117. */
  118. sigma.prototype.toSVG = function(params) {
  119. params = params || {};
  120. var prefix = this.settings('classPrefix'),
  121. w = params.size || params.width || DEFAULTS.size,
  122. h = params.size || params.height || DEFAULTS.size;
  123. // Creating a dummy container
  124. var container = document.createElement('div');
  125. container.setAttribute('width', w);
  126. container.setAttribute('height', h);
  127. container.setAttribute('style', 'position:absolute; top: 0px; left:0px; width: ' + w + 'px; height: ' + h + 'px;');
  128. // Creating a camera
  129. var camera = this.addCamera();
  130. // Creating a svg renderer
  131. var renderer = this.addRenderer({
  132. camera: camera,
  133. container: container,
  134. type: 'svg',
  135. forceLabels: !!params.labels
  136. });
  137. // Refreshing
  138. renderer.resize(w, h);
  139. this.refresh();
  140. // Dropping camera and renderers before something nasty happens
  141. this.killRenderer(renderer);
  142. this.killCamera(camera);
  143. // Retrieving svg
  144. var svg = container.querySelector('svg');
  145. svg.removeAttribute('style');
  146. svg.setAttribute('width', w + 'px');
  147. svg.setAttribute('height', h + 'px');
  148. svg.setAttribute('x', '0px');
  149. svg.setAttribute('y', '0px');
  150. // svg.setAttribute('viewBox', '0 0 1000 1000');
  151. // Dropping labels
  152. if (!params.labels) {
  153. var labelGroup = svg.querySelector('[id="' + prefix + '-group-labels"]');
  154. svg.removeChild(labelGroup);
  155. }
  156. // Dropping hovers
  157. var hoverGroup = svg.querySelector('[id="' + prefix + '-group-hovers"]');
  158. svg.removeChild(hoverGroup);
  159. // Optims?
  160. params.classes = (params.classes !== false);
  161. if (!params.data || params.classes)
  162. optimize(svg, prefix, params);
  163. // Retrieving svg string
  164. var svgString = svg.outerHTML;
  165. // Paranoid cleanup
  166. container = null;
  167. // Output string
  168. var output = '<?xml version="1.0" encoding="utf-8"?>\n';
  169. output += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n';
  170. output += svgString;
  171. if (params.download)
  172. download(output, params.filename || DEFAULTS.filename);
  173. return output;
  174. };
  175. }).call(this);