vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
11 KiB

11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
  1. vis.js
  2. ==================
  3. <a href="https://github.com/almende/vis/blob/develop/misc/we_need_help.md">
  4. <img align="right" src="https://raw.githubusercontent.com/almende/vis/master/misc/we_need_help.png">
  5. </a>
  6. Vis.js is a dynamic, browser based visualization library.
  7. The library is designed to be easy to use, handle large amounts
  8. of dynamic data, and enable manipulation of the data.
  9. The library consists of the following components:
  10. - DataSet and DataView. A flexible key/value based data set. Add, update, and
  11. remove items. Subscribe on changes in the data set. A DataSet can filter and
  12. order items, and convert fields of items.
  13. - DataView. A filtered and/or formatted view on a DataSet.
  14. - Graph2d. Plot data on a timeline with lines or barcharts.
  15. - Graph3d. Display data in a three dimensional graph.
  16. - Network. Display a network (force directed graph) with nodes and edges.
  17. - Timeline. Display different types of data on a timeline.
  18. The vis.js library was initially developed by [Almende B.V](http://almende.com).
  19. ## Install
  20. Install via npm:
  21. $ npm install vis
  22. Install via bower:
  23. $ bower install vis
  24. Link via cdnjs: http://cdnjs.com
  25. Or download the library from the github project:
  26. [https://github.com/almende/vis.git](https://github.com/almende/vis.git).
  27. ### Installing module `canvas`
  28. Module `canvas` is only required if you need to run `vis.js` on `node.js` and require actual output. It is not required for regular usage in a browser.
  29. Currently, the unit tests use a mock object for canvas which has limited but adequate functionality. If `canvas` is installed, that will be used silently in place of the mock object.
  30. The issue with `canvas` is that it has an external dependency to `cairo`. This needs to be installed outside of the regular install as done by `npm`. Please consult [`node-canvas` github page](https://github.com/Automattic/node-canvas/wiki#desktop) for the correct installation procecure your platform
  31. ## Load
  32. To use a component, include the javascript and css files of vis in your web page:
  33. ```html
  34. <!DOCTYPE HTML>
  35. <html>
  36. <head>
  37. <script src="webroot/vis/dist/vis.js"></script>
  38. <link href="webroot/vis/dist/vis.css" rel="stylesheet" type="text/css" />
  39. </head>
  40. <body>
  41. <script type="text/javascript">
  42. // ... load a visualization
  43. </script>
  44. </body>
  45. </html>
  46. ```
  47. or load vis.js using require.js. Note that vis.css must be loaded too.
  48. ```js
  49. require.config({
  50. paths: {
  51. vis: 'path/to/vis/dist',
  52. }
  53. });
  54. require(['vis'], function (math) {
  55. // ... load a visualization
  56. });
  57. ```
  58. A timeline can be instantiated as:
  59. ```js
  60. var timeline = new vis.Timeline(container, data, options);
  61. ```
  62. Where `container` is an HTML element, `data` is an Array with data or a DataSet,
  63. and `options` is an optional object with configuration options for the
  64. component.
  65. ## Example
  66. A basic example on loading a Timeline is shown below. More examples can be
  67. found in the [examples directory](https://github.com/almende/vis/tree/master/examples)
  68. of the project.
  69. ```html
  70. <!DOCTYPE HTML>
  71. <html>
  72. <head>
  73. <title>Timeline basic demo</title>
  74. <script src="vis/dist/vis.js"></script>
  75. <link href="vis/dist/vis.css" rel="stylesheet" type="text/css" />
  76. <style type="text/css">
  77. body, html {
  78. font-family: sans-serif;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <div id="visualization"></div>
  84. <script type="text/javascript">
  85. var container = document.getElementById('visualization');
  86. var data = [
  87. {id: 1, content: 'item 1', start: '2013-04-20'},
  88. {id: 2, content: 'item 2', start: '2013-04-14'},
  89. {id: 3, content: 'item 3', start: '2013-04-18'},
  90. {id: 4, content: 'item 4', start: '2013-04-16', end: '2013-04-19'},
  91. {id: 5, content: 'item 5', start: '2013-04-25'},
  92. {id: 6, content: 'item 6', start: '2013-04-27'}
  93. ];
  94. var options = {};
  95. var timeline = new vis.Timeline(container, data, options);
  96. </script>
  97. </body>
  98. </html>
  99. ```
  100. ## Build
  101. To build the library from source, clone the project from github
  102. $ git clone git://github.com/almende/vis.git
  103. The source code uses the module style of node (require and module.exports) to
  104. organize dependencies. To install all dependencies and build the library,
  105. run `npm install` in the root of the project.
  106. $ cd vis
  107. $ npm install
  108. Then, the project can be build running:
  109. $ npm run build
  110. To automatically rebuild on changes in the source files, once can use
  111. $ npm run watch
  112. This will both build and minify the library on changes. Minifying is relatively
  113. slow, so when only the non-minified library is needed, one can use the
  114. `watch-dev` script instead:
  115. $ npm run watch-dev
  116. ## Custom builds
  117. The folder `dist` contains bundled versions of vis.js for direct use in the browser. These bundles contain all the visualizations and include external dependencies such as *hammer.js* and *moment.js*.
  118. The source code of vis.js consists of commonjs modules, which makes it possible to create custom bundles using tools like [Browserify](http://browserify.org/) or [Webpack](http://webpack.github.io/). This can be bundling just one visualization like the Timeline, or bundling vis.js as part of your own browserified web application.
  119. *Note that hammer.js version 2 is required as of v4.*
  120. ### Prerequisites
  121. Before you can do a build:
  122. - Install *node.js* and *npm* on your system: https://nodejs.org/
  123. - Install the following modules using npm: `browserify`, `babelify`, and `uglify-js`:
  124. ```
  125. $ [sudo] npm install -g browserify babelify uglify-js
  126. ```
  127. - Download or clone the vis.js project:
  128. ```
  129. $ git clone https://github.com/almende/vis.git
  130. ```
  131. - Install the dependencies of vis.js by running `npm install` in the root of the project:
  132. ```
  133. $ cd vis
  134. $ npm install
  135. ```
  136. ### Examples of custom builds
  137. #### Example 1: Bundle only a single visualization type
  138. For example, to create a bundle with just the Timeline and DataSet, create an index file named **custom.js** in the root of the project, containing:
  139. ```js
  140. exports.DataSet = require('./lib/DataSet');
  141. exports.Timeline = require('./lib/timeline/Timeline');
  142. ```
  143. Then create a custom bundle using browserify, like:
  144. $ browserify custom.js -t [ babelify --presets [es2015] ] -o dist/vis-custom.js -s vis
  145. This will generate a custom bundle *vis-custom.js*, which exposes the namespace `vis` containing only `DataSet` and `Timeline`. The generated bundle can be minified using uglifyjs:
  146. $ uglifyjs dist/vis-custom.js -o dist/vis-custom.min.js
  147. The custom bundle can now be loaded like:
  148. ```html
  149. <!DOCTYPE HTML>
  150. <html>
  151. <head>
  152. <script src="dist/vis-custom.min.js"></script>
  153. <link href="dist/vis.min.css" rel="stylesheet" type="text/css" />
  154. </head>
  155. <body>
  156. ...
  157. </body>
  158. </html>
  159. ```
  160. #### Example 2: Exclude external libraries
  161. The default bundle `vis.js` is standalone and includes external dependencies such as *hammer.js* and *moment.js*. When these libraries are already loaded by the application, vis.js does not need to include these dependencies itself too. To build a custom bundle of vis.js excluding *moment.js* and *hammer.js*, run browserify in the root of the project:
  162. $ browserify index.js -t [ babelify --presets [es2015] ] -o dist/vis-custom.js -s vis -x moment -x hammerjs
  163. This will generate a custom bundle *vis-custom.js*, which exposes the namespace `vis`, and has *moment.js* and *hammer.js* excluded. The generated bundle can be minified with uglifyjs:
  164. $ uglifyjs dist/vis-custom.js -o dist/vis-custom.min.js
  165. The custom bundle can now be loaded as:
  166. ```html
  167. <!DOCTYPE HTML>
  168. <html>
  169. <head>
  170. <!-- load external dependencies -->
  171. <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
  172. <script src="http://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
  173. <!-- load vis.js -->
  174. <script src="dist/vis-custom.min.js"></script>
  175. <link href="dist/vis.min.css" rel="stylesheet" type="text/css" />
  176. </head>
  177. <body>
  178. ...
  179. </body>
  180. </html>
  181. ```
  182. #### Example 3: Bundle vis.js as part of your (commonjs) application
  183. When writing a web application with commonjs modules, vis.js can be packaged automatically into the application. Create a file **app.js** containing:
  184. ```js
  185. var moment = require('moment');
  186. var DataSet = require('vis/lib/DataSet');
  187. var Timeline = require('vis/lib/timeline/Timeline');
  188. var container = document.getElementById('visualization');
  189. var data = new DataSet([
  190. {id: 1, content: 'item 1', start: moment('2013-04-20')},
  191. {id: 2, content: 'item 2', start: moment('2013-04-14')},
  192. {id: 3, content: 'item 3', start: moment('2013-04-18')},
  193. {id: 4, content: 'item 4', start: moment('2013-04-16'), end: moment('2013-04-19')},
  194. {id: 5, content: 'item 5', start: moment('2013-04-25')},
  195. {id: 6, content: 'item 6', start: moment('2013-04-27')}
  196. ]);
  197. var options = {};
  198. var timeline = new Timeline(container, data, options);
  199. ```
  200. The application can be bundled and minified:
  201. $ browserify app.js -o dist/app-bundle.js -t babelify
  202. $ uglifyjs dist/app-bundle.js -o dist/app-bundle.min.js
  203. And loaded into a webpage:
  204. ```html
  205. <!DOCTYPE HTML>
  206. <html>
  207. <head>
  208. <link href="node_modules/vis/dist/vis.min.css" rel="stylesheet" type="text/css" />
  209. </head>
  210. <body>
  211. <div id="visualization"></div>
  212. <script src="dist/app-bundle.min.js"></script>
  213. </body>
  214. </html>
  215. ```
  216. #### Example 4: Integrate vis.js components directly in your webpack build
  217. You can integrate e.g. the timeline component directly in you webpack build.
  218. Therefor you can e.g. import the component-files from root direcory (starting with "index-").
  219. ```js
  220. import { DataSet, Timeline } from 'vis/index-timeline-graph2d';
  221. var container = document.getElementById('visualization');
  222. var data = new DataSet();
  223. var timeline = new Timeline(container, data, {});
  224. ```
  225. To get this to work you'll need to add some babel-loader-setting to your webpack-config:
  226. ```js
  227. module: {
  228. module: {
  229. rules: [{
  230. test: /node_modules[\\\/]vis[\\\/].*\.js$/,
  231. loader: 'babel-loader',
  232. query: {
  233. cacheDirectory: true,
  234. presets: [ "babel-preset-es2015" ].map(require.resolve),
  235. plugins: [
  236. "transform-es3-property-literals", // #2452
  237. "transform-es3-member-expression-literals", // #2566
  238. "transform-runtime" // #2566
  239. ]
  240. }
  241. }]
  242. }
  243. }
  244. ```
  245. There is also an [demo-project](https://github.com/mojoaxel/vis-webpack-demo) showing the integration of vis.js using webpack.
  246. ## Test
  247. To test the library, install the project dependencies once:
  248. $ npm install
  249. Then run the tests:
  250. $ npm run test
  251. ## License
  252. Copyright (C) 2010-2017 Almende B.V. and Contributors
  253. Vis.js is dual licensed under both
  254. * The Apache 2.0 License
  255. http://www.apache.org/licenses/LICENSE-2.0
  256. and
  257. * The MIT License
  258. http://opensource.org/licenses/MIT
  259. Vis.js may be distributed under either license.