/README.md

http://github.com/anvaka/VivaGraphJS · Markdown · 220 lines · 169 code · 51 blank · 0 comment · 0 complexity · f544bd4b3b1fd474171c45751af62b0a MD5 · raw file

  1. VivaGraph [![build status](https://github.com/anvaka/VivaGraphJS/actions/workflows/tests.yaml/badge.svg)](https://github.com/anvaka/VivaGraphJS/actions/workflows/tests.yaml)
  2. ==================================================
  3. **VivaGraphJS** is designed to be extensible and to support different
  4. rendering engines and layout algorithms. Underlying algorithms have been broken out into [ngraph](https://github.com/anvaka/ngraph).
  5. The larger family of modules can be found by [querying npm for "ngraph"](https://www.npmjs.com/search?q=ngraph).
  6. [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/anvaka/VivaGraphJS?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
  7. Enough talking. Show me the demo!
  8. ----------------------------------------------------
  9. Some examples of library usage in the real projects:
  10. * [Amazon Visualization](http://www.yasiv.com/amazon#/Search?q=graph%20drawing&category=Books&lang=US) Shows related products on Amazon.com, uses SVG as graph output
  11. * [Graph Viewer](http://www.yasiv.com/graphs#Bai/rw496) visualization of sparse matrices collection of the University of Florida. WebGL based.
  12. * [Vkontakte Visualization](http://www.yasiv.com/vk) friendship visualization of the largest social network in Russia [vk.com](https://vk.com). WebGL based.
  13. To start using the library include `vivagraph.js` script from the [dist](https://github.com/anvaka/VivaGraphJS/tree/master/dist)
  14. folder. The following code is the minimum required to render a graph with two nodes and one edge:
  15. ```javascript
  16. var graph = Viva.Graph.graph();
  17. graph.addLink(1, 2);
  18. var renderer = Viva.Graph.View.renderer(graph);
  19. renderer.run();
  20. ```
  21. This will instantiate a graph inside `document.body`:
  22. ![Simple graph](https://github.com/anvaka/VivaGraphJS/raw/master/packages/Images/mingraph.png)
  23. If you want to render graph in your own DOM element:
  24. ```javascript
  25. var graph = Viva.Graph.graph();
  26. graph.addLink(1, 2);
  27. // specify where it should be rendered:
  28. var renderer = Viva.Graph.View.renderer(graph, {
  29. container: document.getElementById('graphDiv')
  30. });
  31. renderer.run();
  32. ```
  33. The code above adds a link to the graph between nodes `1` and `2`. Since nodes
  34. are not yet in the graph they will be created. It's equivalent to
  35. ```javascript
  36. var graph = Viva.Graph.graph();
  37. graph.addNode(1);
  38. graph.addNode(2);
  39. graph.addLink(1, 2);
  40. var renderer = Viva.Graph.View.renderer(graph);
  41. renderer.run();
  42. ```
  43. Customization
  44. ----------------------------------------------------
  45. VivaGraphJS is all about customization. You can easily change the appearance of
  46. nodes and links. You can also change the layouting algorithm and medium that
  47. displays elements of the graph. For example: The following code allows you to
  48. use WebGL-based rendering, instead of the default SVG.
  49. ```javascript
  50. var graph = Viva.Graph.graph();
  51. graph.addLink(1, 2);
  52. var graphics = Viva.Graph.View.webglGraphics();
  53. var renderer = Viva.Graph.View.renderer(graph,
  54. {
  55. graphics : graphics
  56. });
  57. renderer.run();
  58. ```
  59. `graphics` class is responsible for rendering nodes and links on the page. And `renderer` orchestrates the process. To change nodes appearance tell `graphics` how to represent them. Here is an example of graph with six people who I follow at github:
  60. ```javascript
  61. var graph = Viva.Graph.graph();
  62. // Construct the graph
  63. graph.addNode('anvaka', {url : 'https://secure.gravatar.com/avatar/91bad8ceeec43ae303790f8fe238164b'});
  64. graph.addNode('manunt', {url : 'https://secure.gravatar.com/avatar/c81bfc2cf23958504617dd4fada3afa8'});
  65. graph.addNode('thlorenz', {url : 'https://secure.gravatar.com/avatar/1c9054d6242bffd5fd25ec652a2b79cc'});
  66. graph.addNode('bling', {url : 'https://secure.gravatar.com/avatar/24a5b6e62e9a486743a71e0a0a4f71af'});
  67. graph.addNode('diyan', {url : 'https://secure.gravatar.com/avatar/01bce7702975191fdc402565bd1045a8?'});
  68. graph.addNode('pocheptsov', {url : 'https://secure.gravatar.com/avatar/13da974fc9716b42f5d62e3c8056c718'});
  69. graph.addNode('dimapasko', {url : 'https://secure.gravatar.com/avatar/8e587a4232502a9f1ca14e2810e3c3dd'});
  70. graph.addLink('anvaka', 'manunt');
  71. graph.addLink('anvaka', 'thlorenz');
  72. graph.addLink('anvaka', 'bling');
  73. graph.addLink('anvaka', 'diyan');
  74. graph.addLink('anvaka', 'pocheptsov');
  75. graph.addLink('anvaka', 'dimapasko');
  76. // Set custom nodes appearance
  77. var graphics = Viva.Graph.View.svgGraphics();
  78. graphics.node(function(node) {
  79. // The function is called every time renderer needs a ui to display node
  80. return Viva.Graph.svg('image')
  81. .attr('width', 24)
  82. .attr('height', 24)
  83. .link(node.data.url); // node.data holds custom object passed to graph.addNode();
  84. })
  85. .placeNode(function(nodeUI, pos){
  86. // Shift image to let links go to the center:
  87. nodeUI.attr('x', pos.x - 12).attr('y', pos.y - 12);
  88. });
  89. var renderer = Viva.Graph.View.renderer(graph, {
  90. graphics : graphics
  91. });
  92. renderer.run();
  93. ```
  94. The result is:
  95. ![Custom nodes](https://github.com/anvaka/VivaGraphJS/raw/master/packages/Images/customNode.png)
  96. Tuning layout algorithm
  97. ----------------------------------------------------
  98. Graphs vary by their nature. Some graphs have hundreds of nodes and few edges (or links), some might connect every node with every other. Tuning the physics often helps get the best layout.
  99. Consider the following example:
  100. ```javascript
  101. var graphGenerator = Viva.Graph.generator();
  102. var graph = graphGenerator.grid(3, 3);
  103. var renderer = Viva.Graph.View.renderer(graph);
  104. renderer.run();
  105. ```
  106. Graph generators are part of the library, which can produce classic graphs.
  107. `grid` generator creates a grid with given number of columns and rows. But with
  108. default parameters the rendering is pretty ugly:
  109. ![Grid 3x3 bad](https://github.com/anvaka/VivaGraphJS/raw/master/packages/Images/gridBad.png)
  110. Let's tweak the original code:
  111. ```javascript
  112. var graphGenerator = Viva.Graph.generator();
  113. var graph = graphGenerator.grid(3, 3);
  114. var layout = Viva.Graph.Layout.forceDirected(graph, {
  115. springLength : 10,
  116. springCoeff : 0.0005,
  117. dragCoeff : 0.02,
  118. gravity : -1.2
  119. });
  120. var renderer = Viva.Graph.View.renderer(graph, {
  121. layout : layout
  122. });
  123. renderer.run();
  124. ```
  125. Now the result is much better:
  126. ![Grid 3x3](https://github.com/anvaka/VivaGraphJS/raw/master/packages/Images/gridGood.png)
  127. You can tune values during simulation with `layout.simulator.springLength(newValue)`, `layout.simulator.springCoeff(newValue)`, etc. See all the values that you can tune in [this source file](https://github.com/anvaka/ngraph.physics.simulator/blob/master/index.js#L12).
  128. Tuning layout algorithm is definitely one of the hardest part of using this library.
  129. It has to be improved in future to simplify usage. Each of the force directed
  130. algorithm parameters are described in the source code.
  131. Design philosophy/roadmap
  132. -------------------------
  133. Until version 0.7.x VivaGraph was a single monolithic code base. Starting from
  134. 0.7.x the library is bundled from small npm modules into `Viva` namespace.
  135. All these modules are part of a larger [ngraph](https://github.com/anvaka/ngraph)
  136. family. `ngraph` modules support rendering graphs into images, 3D rendering,
  137. integration with [gephi](https://gephi.org/), pagerank calculation and many more.
  138. Version 0.7 is a compromise between maximum backward compatibility and ngraph
  139. flexibility. Eventually I hope to further simplify API and provide interface
  140. for custom builds.
  141. Upgrade guide
  142. -------------
  143. Please refer the [upgrade guide](https://github.com/anvaka/VivaGraphJS/blob/master/docs/upgrade_guide.md) to see how to update older versions of the library to the latest one.
  144. Local Build
  145. -----------
  146. Run the following script:
  147. ```
  148. git clone https://github.com/anvaka/VivaGraphJS.git
  149. cd ./VivaGraphJS
  150. npm install
  151. gulp release
  152. ```
  153. The combined/minified code should be stored in ```dist``` folder.
  154. Looking for alternatives?
  155. -------------------------
  156. I'm trying to put up a list of all known graph drawing libraries.
  157. Please [find it here](http://anvaka.github.io/graph-drawing-libraries/#/all)
  158. I need your feedback
  159. ----------------------------------------------------
  160. Disclaimer: I wrote this library to learn JavaScript. By no means I pretend to
  161. be an expert in the language and chosen approach to design may not be the optimal.
  162. I would love to hear your feedback and suggestions.
  163. Though I implemented this library from scratch, I went through many existing
  164. libraries to pick the best (at my view) out of them. If you are evaluating libraries
  165. for your project make sure to [check them out](http://anvaka.github.io/graph-drawing-libraries/#/all)
  166. as well.
  167. My goal is to create highly performant javascript library, which serves in the
  168. field of graph drawing. To certain extent I achieved it. But I have no doubt
  169. there is much more to improve here.