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.

359 lines
11 KiB

11 years ago
11 years ago
11 years ago
8 years ago
9 years ago
  1. # vis.js
  2. [![Join the chat at https://gitter.im/vis-js/Lobby](https://badges.gitter.im/vis-js/Lobby.svg)](https://gitter.im/vis-js/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
  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. ## Badges
  20. [![NPM](https://nodei.co/npm/vis.png?downloads=true&downloadRank=true)](https://nodei.co/npm/vis/)
  21. [![Dependency Status](https://david-dm.org/almende/vis/status.svg)](https://david-dm.org/almende/vis)
  22. [![devDependency Status](https://david-dm.org/almende/vis/dev-status.svg)](https://david-dm.org/almende/vis?type=dev)
  23. [![last version on CDNJS](https://img.shields.io/cdnjs/v/vis.svg)](https://cdnjs.com/libraries/vis)
  24. [![GitHub contributors](https://img.shields.io/github/contributors/almende/vis.svg)](https://github.com/almende/vis/graphs/contributors)
  25. [![GitHub stars](https://img.shields.io/github/stars/almende/vis.svg)](https://github.com/almende/vis/stargazers)
  26. [![GitHub issues](https://img.shields.io/github/issues/almende/vis.svg)](https://github.com/almende/vis/issues)
  27. [![Pending Pull-Requests](http://githubbadges.herokuapp.com/almende/vis/pulls.svg)](https://github.com/almende/vis/pulls)
  28. ## Install
  29. Install via npm:
  30. $ npm install vis
  31. Install via bower:
  32. $ bower install vis
  33. Link via cdnjs: http://cdnjs.com
  34. Or download the library from the github project:
  35. [https://github.com/almende/vis.git](https://github.com/almende/vis.git).
  36. ## Load
  37. To use a component, include the javascript and css files of vis in your web page:
  38. ```html
  39. <!DOCTYPE HTML>
  40. <html>
  41. <head>
  42. <script src="webroot/vis/dist/vis.js"></script>
  43. <link href="webroot/vis/dist/vis.css" rel="stylesheet" type="text/css" />
  44. </head>
  45. <body>
  46. <script type="text/javascript">
  47. // ... load a visualization
  48. </script>
  49. </body>
  50. </html>
  51. ```
  52. or load vis.js using require.js. Note that vis.css must be loaded too.
  53. ```js
  54. require.config({
  55. paths: {
  56. vis: 'path/to/vis/dist',
  57. }
  58. });
  59. require(['vis'], function (math) {
  60. // ... load a visualization
  61. });
  62. ```
  63. A timeline can be instantiated as:
  64. ```js
  65. var timeline = new vis.Timeline(container, data, options);
  66. ```
  67. Where `container` is an HTML element, `data` is an Array with data or a DataSet,
  68. and `options` is an optional object with configuration options for the
  69. component.
  70. ## Example
  71. A basic example on loading a Timeline is shown below. More examples can be
  72. found in the [examples directory](https://github.com/almende/vis/tree/master/examples)
  73. of the project.
  74. ```html
  75. <!DOCTYPE HTML>
  76. <html>
  77. <head>
  78. <title>Timeline basic demo</title>
  79. <script src="vis/dist/vis.js"></script>
  80. <link href="vis/dist/vis.css" rel="stylesheet" type="text/css" />
  81. <style type="text/css">
  82. body, html {
  83. font-family: sans-serif;
  84. }
  85. </style>
  86. </head>
  87. <body>
  88. <div id="visualization"></div>
  89. <script type="text/javascript">
  90. var container = document.getElementById('visualization');
  91. var data = [
  92. {id: 1, content: 'item 1', start: '2013-04-20'},
  93. {id: 2, content: 'item 2', start: '2013-04-14'},
  94. {id: 3, content: 'item 3', start: '2013-04-18'},
  95. {id: 4, content: 'item 4', start: '2013-04-16', end: '2013-04-19'},
  96. {id: 5, content: 'item 5', start: '2013-04-25'},
  97. {id: 6, content: 'item 6', start: '2013-04-27'}
  98. ];
  99. var options = {};
  100. var timeline = new vis.Timeline(container, data, options);
  101. </script>
  102. </body>
  103. </html>
  104. ```
  105. ## Build
  106. To build the library from source, clone the project from github
  107. $ git clone git://github.com/almende/vis.git
  108. The source code uses the module style of node (require and module.exports) to
  109. organize dependencies. To install all dependencies and build the library,
  110. run `npm install` in the root of the project.
  111. $ cd vis
  112. $ npm install
  113. Then, the project can be build running:
  114. $ npm run build
  115. To automatically rebuild on changes in the source files, once can use
  116. $ npm run watch
  117. This will both build and minify the library on changes. Minifying is relatively
  118. slow, so when only the non-minified library is needed, one can use the
  119. `watch-dev` script instead:
  120. $ npm run watch-dev
  121. ## Custom builds
  122. 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*.
  123. 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.
  124. *Note that hammer.js version 2 is required as of v4.*
  125. ### Prerequisites
  126. Before you can do a build:
  127. - Install *node.js* and *npm* on your system: https://nodejs.org/
  128. - Install the following modules using npm: `browserify`, `babelify`, and `uglify-js`:
  129. ```
  130. $ [sudo] npm install -g browserify babelify uglify-js
  131. ```
  132. - Download or clone the vis.js project:
  133. ```
  134. $ git clone https://github.com/almende/vis.git
  135. ```
  136. - Install the dependencies of vis.js by running `npm install` in the root of the project:
  137. ```
  138. $ cd vis
  139. $ npm install
  140. ```
  141. ### Examples of custom builds
  142. #### Example 1: Bundle only a single visualization type
  143. 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:
  144. ```js
  145. exports.DataSet = require('./lib/DataSet');
  146. exports.Timeline = require('./lib/timeline/Timeline');
  147. ```
  148. Then create a custom bundle using browserify, like:
  149. $ browserify custom.js -t [ babelify --presets [es2015] ] -o dist/vis-custom.js -s vis
  150. 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:
  151. $ uglifyjs dist/vis-custom.js -o dist/vis-custom.min.js
  152. The custom bundle can now be loaded like:
  153. ```html
  154. <!DOCTYPE HTML>
  155. <html>
  156. <head>
  157. <script src="dist/vis-custom.min.js"></script>
  158. <link href="dist/vis.min.css" rel="stylesheet" type="text/css" />
  159. </head>
  160. <body>
  161. ...
  162. </body>
  163. </html>
  164. ```
  165. #### Example 2: Exclude external libraries
  166. 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:
  167. $ browserify index.js -t [ babelify --presets [es2015] ] -o dist/vis-custom.js -s vis -x moment -x hammerjs
  168. 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:
  169. $ uglifyjs dist/vis-custom.js -o dist/vis-custom.min.js
  170. The custom bundle can now be loaded as:
  171. ```html
  172. <!DOCTYPE HTML>
  173. <html>
  174. <head>
  175. <!-- load external dependencies -->
  176. <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
  177. <script src="http://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
  178. <!-- load vis.js -->
  179. <script src="dist/vis-custom.min.js"></script>
  180. <link href="dist/vis.min.css" rel="stylesheet" type="text/css" />
  181. </head>
  182. <body>
  183. ...
  184. </body>
  185. </html>
  186. ```
  187. #### Example 3: Bundle vis.js as part of your (commonjs) application
  188. When writing a web application with commonjs modules, vis.js can be packaged automatically into the application. Create a file **app.js** containing:
  189. ```js
  190. var moment = require('moment');
  191. var DataSet = require('vis/lib/DataSet');
  192. var Timeline = require('vis/lib/timeline/Timeline');
  193. var container = document.getElementById('visualization');
  194. var data = new DataSet([
  195. {id: 1, content: 'item 1', start: moment('2013-04-20')},
  196. {id: 2, content: 'item 2', start: moment('2013-04-14')},
  197. {id: 3, content: 'item 3', start: moment('2013-04-18')},
  198. {id: 4, content: 'item 4', start: moment('2013-04-16'), end: moment('2013-04-19')},
  199. {id: 5, content: 'item 5', start: moment('2013-04-25')},
  200. {id: 6, content: 'item 6', start: moment('2013-04-27')}
  201. ]);
  202. var options = {};
  203. var timeline = new Timeline(container, data, options);
  204. ```
  205. The application can be bundled and minified:
  206. $ browserify app.js -o dist/app-bundle.js -t babelify
  207. $ uglifyjs dist/app-bundle.js -o dist/app-bundle.min.js
  208. And loaded into a webpage:
  209. ```html
  210. <!DOCTYPE HTML>
  211. <html>
  212. <head>
  213. <link href="node_modules/vis/dist/vis.min.css" rel="stylesheet" type="text/css" />
  214. </head>
  215. <body>
  216. <div id="visualization"></div>
  217. <script src="dist/app-bundle.min.js"></script>
  218. </body>
  219. </html>
  220. ```
  221. #### Example 4: Integrate vis.js components directly in your webpack build
  222. You can integrate e.g. the timeline component directly in you webpack build.
  223. Therefor you just import the component-files from root direcory (starting with "index-").
  224. ```js
  225. var visTimeline = require('vis/index-timeline-graph2d');
  226. var container = document.getElementById('visualization');
  227. var data = new DataSet();
  228. var timeline = new Timeline(container, data, {});
  229. ```
  230. To get this to work you'll need to add the some babel-loader-setting:
  231. ```js
  232. module: {
  233. loaders: [{
  234. test: /node_modules[\\\/]vis[\\\/].*\.js$/,
  235. loader: 'babel',
  236. query: {
  237. cacheDirectory: true,
  238. presets: ["es2015"],
  239. plugins: [
  240. "transform-es3-property-literals",
  241. "transform-es3-member-expression-literals",
  242. "transform-runtime"
  243. ]
  244. }
  245. }]
  246. }
  247. ```
  248. ## Test
  249. To test the library, install the project dependencies once:
  250. $ npm install
  251. Then run the tests:
  252. $ npm run test
  253. ## License
  254. Copyright (C) 2010-2017 Almende B.V. and Contributors
  255. Vis.js is dual licensed under both
  256. * The Apache 2.0 License
  257. http://www.apache.org/licenses/LICENSE-2.0
  258. and
  259. * The MIT License
  260. http://opensource.org/licenses/MIT
  261. Vis.js may be distributed under either license.