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.

713 lines
17 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>vis.js | DataSet documentation</title>
  5. <link href="css/prettify.css" type="text/css" rel="stylesheet" />
  6. <link href='css/style.css' type='text/css' rel='stylesheet'>
  7. <script type="text/javascript" src="lib/prettify/prettify.js"></script>
  8. </head>
  9. <body onload="prettyPrint();">
  10. <div id="container">
  11. <h1>DataSet documentation</h1>
  12. <h2 id="Contents">Contents</h2>
  13. <ul>
  14. <li><a href="#Overview">Overview</a></li>
  15. <li><a href="#Example">Example</a></li>
  16. <li><a href="#Construction">Construction</a></li>
  17. <li><a href="#Data_Manipulation">Data Manipulation</a></li>
  18. <li><a href="#Data_Filtering">Data Filtering</a></li>
  19. <li><a href="#Data_Formatting">Data Formatting</a></li>
  20. <li><a href="#Subscriptions">Subscriptions</a></li>
  21. <li><a href="#Data_Policy">Data Policy</a></li>
  22. </ul>
  23. <h2 id="Overview">Overview</h2>
  24. <p>
  25. Vis.js comes with a flexible DataSet, which can be used to hold and
  26. manipulate unstructured data and listen for changes in the data.
  27. The DataSet is key/value based. Data items can be added, updated and
  28. removed from the DatSet, and one can subscribe to changes in the DataSet.
  29. The data in the DataSet can be filtered and ordered, and fields (like
  30. dates) can be converted to a specific type. Data can be normalized when
  31. appending it to the DataSet as well.
  32. </p>
  33. <h2 id="Example">Example</h2>
  34. <p>
  35. The following example shows how to use a DataSet.
  36. </p>
  37. <pre class="prettyprint lang-js">
  38. // create a DataSet
  39. var options = {};
  40. var data = new vis.DataSet(options);
  41. // add items
  42. // note that the data items can contain different properties and data formats
  43. data.add([
  44. {id: 1, text: 'item 1', date: new Date(2013, 6, 20), group: 1, first: true},
  45. {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
  46. {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
  47. {id: 4, text: 'item 4'}
  48. ]);
  49. // subscribe to any change in the DataSet
  50. data.on('*', function (event, properties, senderId) {
  51. console.log('event', event, properties);
  52. });
  53. // update an existing item
  54. data.update({id: 2, group: 1});
  55. // remove an item
  56. data.remove(4);
  57. // get all ids
  58. var ids = data.getIds();
  59. console.log('ids', ids);
  60. // get a specific item
  61. var item1 = data.get(1);
  62. console.log('item1', item1);
  63. // retrieve a filtered subset of the data
  64. var items = data.get({
  65. filter: function (item) {
  66. return item.group == 1;
  67. }
  68. });
  69. console.log('filtered items', items);
  70. // retrieve formatted items
  71. var items = data.get({
  72. fields: ['id', 'date'],
  73. convert: {
  74. date: 'ISODate'
  75. }
  76. });
  77. console.log('formatted items', items);
  78. </pre>
  79. <h2 id="Construction">Construction</h2>
  80. <p>
  81. A DataSet can be constructed as:
  82. </p>
  83. <pre class="prettyprint lang-js">
  84. var data = new vis.DataSet([data] [, options])
  85. </pre>
  86. <p>
  87. After construction, data can be added to the DataSet using the methods
  88. <code>add</code> and <code>update</code>, as described in section
  89. <a href="#Data_Manipulation">Data Manipulation</a>.
  90. </p>
  91. <p>
  92. The parameter <code>data</code>code> is optional and can be an Array or
  93. Google DataTable with items.
  94. </p>
  95. <p>
  96. The parameter <code>options</code> is optional and is an object which can
  97. contain the following properties:
  98. </p>
  99. <table>
  100. <tr>
  101. <th>Name</th>
  102. <th>Type</th>
  103. <th>Default value</th>
  104. <th>Description</th>
  105. </tr>
  106. <tr>
  107. <td>fieldId</td>
  108. <td>String</td>
  109. <td>"id"</td>
  110. <td>
  111. The name of the field containing the id of the items.
  112. When data is fetched from a server which uses some specific
  113. field to identify items, this field name can be specified
  114. in the DataSet using the option <code>fieldId</code>.
  115. For example <a href="http://couchdb.apache.org/"
  116. target="_blank">CouchDB</a> uses the field
  117. <code>"_id"</code> to identify documents.
  118. </td>
  119. </tr>
  120. <tr>
  121. <td>convert</td>
  122. <td>Object.&lt;String,&nbsp;String&gt;</td>
  123. <td>none</td>
  124. <td>
  125. An object containing field names as key, and data types as
  126. value. By default, the type of the properties of items are left
  127. unchanged. Item properties can be normalized by specifying a
  128. field type. This is useful for example to automatically convert
  129. stringified dates coming from a server into JavaScript Date
  130. objects. The available data types are listed in section
  131. <a href="#Data_Types">Data Types</a>.
  132. </td>
  133. </tr>
  134. </table>
  135. <h2 id="Data_Manipulation">Data Manipulation</h2>
  136. <p>
  137. The data in a DataSet can be manipulated using the methods
  138. <a href="#Add"><code>add</code></a>,
  139. <a href="#Update"><code>update</code></a>,
  140. and <a href="#Remove"><code>remove</code></a>.
  141. The DataSet can be emptied using the method
  142. <a href="#Clear"><code>clear</code></a>.
  143. </p>
  144. <pre class="prettyprint lang-js">
  145. // create a DataSet
  146. var data = new vis.DataSet();
  147. // add items
  148. data.add([
  149. {id: 1, text: 'item 1'},
  150. {id: 2, text: 'item 2'},
  151. {id: 3, text: 'item 3'}
  152. ]);
  153. // update an item
  154. data.update({id: 2, text: 'item 2 (updated)'});
  155. // remove an item
  156. data.remove(3);
  157. </pre>
  158. <h3 id="Add">Add</h3>
  159. <p>
  160. Add a data item or an array with items.
  161. </p>
  162. Syntax:
  163. <pre class="prettyprint lang-js">var addedIds = DataSet.add(data [, senderId])</pre>
  164. The argument <code>data</code> can contain:
  165. <ul>
  166. <li>
  167. An <code>Object</code> containing a single item to be
  168. added. The item must contain an id.
  169. </li>
  170. <li>
  171. An <code>Array</code> or
  172. <code>google.visualization.DataTable</code> containing
  173. a list with items to be added. Each item must contain
  174. an id.
  175. </li>
  176. </ul>
  177. <p>
  178. After the items are added to the DataSet, the DataSet will
  179. trigger an event <code>add</code>. When a <code>senderId</code>
  180. is provided, this id will be passed with the triggered
  181. event to all subscribers.
  182. </p>
  183. <p>
  184. The method will throw an Error when an item with the same id
  185. as any of the added items already exists.
  186. </p>
  187. <h3 id="Update">Update</h3>
  188. <p>
  189. Update a data item or an array with items.
  190. </p>
  191. Syntax:
  192. <pre class="prettyprint lang-js">var updatedIds = DataSet.update(data [, senderId])</pre>
  193. The argument <code>data</code> can contain:
  194. <ul>
  195. <li>
  196. An <code>Object</code> containing a single item to be
  197. updated. The item must contain an id.
  198. </li>
  199. <li>
  200. An <code>Array</code> or
  201. <code>google.visualization.DataTable</code> containing
  202. a list with items to be updated. Each item must contain
  203. an id.
  204. </li>
  205. </ul>
  206. <p>
  207. The provided properties will be merged in the existing item.
  208. When an item does not exist, it will be created.
  209. </p>
  210. <p>
  211. After the items are updated, the DataSet will
  212. trigger an event <code>add</code> for the added items, and
  213. an event <code>update</code>. When a <code>senderId</code>
  214. is provided, this id will be passed with the triggered
  215. event to all subscribers.
  216. </p>
  217. <h3 id="Remove">Remove</h3>
  218. <p>
  219. Remove a data item or an array with items.
  220. </p>
  221. Syntax:
  222. <pre class="prettyprint lang-js">var removedIds = DataSet.remove(id [, senderId])</pre>
  223. <p>
  224. The argument <code>id</code> can be:
  225. </p>
  226. <ul>
  227. <li>
  228. A <code>Number</code> or <code>String</code> containing the id
  229. of a single item to be removed.
  230. </li>
  231. <li>
  232. An <code>Object</code> containing the item to be deleted.
  233. The item will be deleted by its id.
  234. </li>
  235. <li>
  236. An Array containing ids or items to be removed.
  237. </li>
  238. </ul>
  239. <p>
  240. The method ignores removal of non-existing items, and returns an array
  241. containing the ids of the items which are actually removed from the
  242. DataSet.
  243. </p>
  244. <p>
  245. After the items are removed, the DataSet will
  246. trigger an event <code>remove</code> for the removed items.
  247. When a <code>senderId</code> is provided, this id will be passed with
  248. the triggered event to all subscribers.
  249. </p>
  250. <h3 id="Clear">Clear</h3>
  251. <p>
  252. Clear the complete DataSet.
  253. </p>
  254. Syntax:
  255. <pre class="prettyprint lang-js">var removedIds = DataSet.clear([senderId])</pre>
  256. <p>
  257. After the items are removed, the DataSet will
  258. trigger an event <code>remove</code> for all removed items.
  259. When a <code>senderId</code> is provided, this id will be passed with
  260. the triggered event to all subscribers.
  261. </p>
  262. <h2 id="Data_Filtering">Data Filtering</h2>
  263. <p>
  264. Data can be retrieved from the DataSet using the method <code>get</code>.
  265. This method can return a single item or a list with items.
  266. </p>
  267. <p>A single item can be retrieved by its id:</p>
  268. <pre class="prettyprint lang-js">
  269. var item1 = dataset.get(1);
  270. </pre>
  271. <p>A selection of items can be retrieved by providing an array with ids:</p>
  272. <pre class="prettyprint lang-js">
  273. var items = dataset.get([1, 3, 4]); // retrieve items 1, 3, and 4
  274. </pre>
  275. <p>All items can be retrieved by simply calling <code>get</code> without
  276. specifying an id:</p>
  277. <pre class="prettyprint lang-js">
  278. var items = dataset.get(); // retrieve all items
  279. </pre>
  280. <p>
  281. Items can be filtered on specific properties by providing a filter
  282. function. A filter function is executed for each of the items in the
  283. DataSet, and is called with the item as parameter. The function must
  284. return a boolean. All items for which the filter function returns
  285. true will be emitted.
  286. </p>
  287. <pre class="prettyprint lang-js">
  288. // retrieve all items having a property group with value 2
  289. var group2 = dataset.get({
  290. filter: function (item) {
  291. return (item.group == 2);
  292. }
  293. });
  294. // retrieve all items having a property balance with a value above zero
  295. var positiveBalance = dataset.get({
  296. filter: function (item) {
  297. return (item.balance > 0);
  298. }
  299. });
  300. </pre>
  301. <h2 id="Data_Formatting">Data Formatting</h2>
  302. <p>
  303. The DataSet contains functionality to format data retrieved via the
  304. method <code>get</code>. The method <code>get</code> has the following
  305. syntax:
  306. </p>
  307. <pre class="prettyprint lang-js">
  308. var item = DataSet.get(id, options); // retrieve a single item
  309. var items = DataSet.get(ids, options); // retrieve a selection of items
  310. var items = DataSet.get(options); // retrieve all items or a filtered set
  311. </pre>
  312. <p>
  313. Where <code>options</code> is an Object which can have the following
  314. properties:
  315. </p>
  316. <table>
  317. <tr>
  318. <th>Name</th>
  319. <th>Type</th>
  320. <th>Description</th>
  321. </tr>
  322. <tr>
  323. <td>fields</td>
  324. <td>String[&nbsp;]</td>
  325. <td>
  326. An array with field names.
  327. By default, all properties of the items are emitted.
  328. When <code>fields</code> is defined, only the properties
  329. whose name is specified in <code>fields</code> will be included
  330. in the returned items.
  331. </td>
  332. </tr>
  333. <tr>
  334. <td>convert</td>
  335. <td>Object.&lt;String,&nbsp;String&gt;</td>
  336. <td>
  337. An object containing field names as key, and data types as value.
  338. By default, the type of the properties of an item are left
  339. unchanged. When a field type is specified, this field in the
  340. items will be converted to the specified type. This can be used
  341. for example to convert ISO strings containing a date to a
  342. JavaScript Date object, or convert strings to numbers or vice
  343. versa. The available data types are listed in section
  344. <a href="#Data_Types">Data Types</a>.
  345. </td>
  346. </tr>
  347. <tr>
  348. <td>filter</td>
  349. <td>Function</td>
  350. <td>Items can be filtered on specific properties by providing a filter
  351. function. A filter function is executed for each of the items in the
  352. DataSet, and is called with the item as parameter. The function must
  353. return a boolean. All items for which the filter function returns
  354. true will be emitted.
  355. See section <a href="#Data_Filtering">Data Filtering</a>.</td>
  356. </tr>
  357. <tr>
  358. <td>order</td>
  359. <td>String | Function</td>
  360. <td>Order the items by a field name or custom sort function.</td>
  361. </tr>
  362. </table>
  363. <p>
  364. The following example demonstrates formatting properties and filtering
  365. properties from items.
  366. </p>
  367. <pre class="prettyprint lang-js">
  368. // create a DataSet
  369. var data = new vis.DataSet();
  370. data.add([
  371. {id: 1, text: 'item 1', date: '2013-06-20', group: 1, first: true},
  372. {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
  373. {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
  374. {id: 4, text: 'item 4'}
  375. ]);
  376. // retrieve formatted items
  377. var items = data.get({
  378. fields: ['id', 'date', 'group'], // output the specified fields only
  379. convert: {
  380. date: 'Date', // convert the date fields to Date objects
  381. group: 'String' // convert the group fields to Strings
  382. }
  383. });
  384. </pre>
  385. <h3 id="Data_Types">Data Types</h3>
  386. <p>
  387. DataSet supports the following data types:
  388. </p>
  389. <table style="width: 100%">
  390. <tr>
  391. <th>Name</th>
  392. <th>Description</th>
  393. <th>Examples</th>
  394. </tr>
  395. <tr>
  396. <td>Boolean</td>
  397. <td>A JavaScript Boolean</td>
  398. <td>
  399. <code>true</code><br>
  400. <code>false</code>
  401. </td>
  402. </tr>
  403. <tr>
  404. <td>Number</td>
  405. <td>A JavaScript Number</td>
  406. <td>
  407. <code>32</code><br>
  408. <code>2.4</code>
  409. </td>
  410. </tr>
  411. <tr>
  412. <td>String</td>
  413. <td>A JavaScript String</td>
  414. <td>
  415. <code>"hello world"</code><br>
  416. <code>"2013-06-28"</code>
  417. </td>
  418. </tr>
  419. <tr>
  420. <td>Date</td>
  421. <td>A JavaScript Date object</td>
  422. <td>
  423. <code>new Date()</code><br>
  424. <code>new Date(2013, 5, 28)</code><br>
  425. <code>new Date(1372370400000)</code>
  426. </td>
  427. </tr>
  428. <tr>
  429. <td>Moment</td>
  430. <td>A Moment object, created with
  431. <a href="http://momentjs.com/" target="_blank">moment.js</a></td>
  432. <td>
  433. <code>moment()</code><br>
  434. <code>moment('2013-06-28')</code>
  435. </td>
  436. </tr>
  437. <tr>
  438. <td>ISODate</td>
  439. <td>A string containing an ISO Date</td>
  440. <td>
  441. <code>new Date().toISOString()</code><br>
  442. <code>"2013-06-27T22:00:00.000Z"</code>
  443. </td>
  444. </tr>
  445. <tr>
  446. <td>ASPDate</td>
  447. <td>A string containing an ASP Date</td>
  448. <td>
  449. <code>"/Date(1372370400000)/"</code><br>
  450. <code>"/Date(1198908717056-0700)/"</code>
  451. </td>
  452. </tr>
  453. </table>
  454. <h2 id="Subscriptions">Subscriptions</h2>
  455. <p>
  456. One can subscribe on changes in a DataSet.
  457. A subscription can be created using the method <code>on</code>,
  458. and removed with <code>off</code>.
  459. </p>
  460. <pre class="prettyprint lang-js">
  461. // create a DataSet
  462. var data = new vis.DataSet();
  463. // subscribe to any change in the DataSet
  464. data.on('*', function (event, properties, senderId) {
  465. console.log('event:', event, 'properties:', properties, 'senderId:', senderId);
  466. });
  467. // add an item
  468. data.add({id: 1, text: 'item 1'}); // triggers an 'add' event
  469. data.update({id: 1, text: 'item 1 (updated)'}); // triggers an 'update' event
  470. data.remove(1); // triggers an 'remove' event
  471. </pre>
  472. <h3 id="On">On</h3>
  473. <p>
  474. Subscribe to an event.
  475. </p>
  476. Syntax:
  477. <pre class="prettyprint lang-js">DataSet.on(event, callback)</pre>
  478. Where:
  479. <ul>
  480. <li>
  481. <code>event</code> is a String containing any of the events listed
  482. in section <a href="#Events">Events</a>.
  483. </li>
  484. <li>
  485. <code>callback</code> is a callback function which will be called
  486. each time the event occurs. The callback function is described in
  487. section <a href="#Callback">Callback</a>.
  488. </li>
  489. </ul>
  490. <h3 id="Off">Off</h3>
  491. <p>
  492. Unsubscribe from an event.
  493. </p>
  494. Syntax:
  495. <pre class="prettyprint lang-js">DataSet.off(event, callback)</pre>
  496. Where <code>event</code> and <code>callback</code> correspond with the
  497. parameters used to <a href="#On">subscribe</a> to the event.
  498. <h3 id="Events">Events</h3>
  499. <p>
  500. The following events are available for subscription:
  501. </p>
  502. <table>
  503. <tr>
  504. <th>Event</th>
  505. <th>Description</th>
  506. </tr>
  507. <tr>
  508. <td>add</td>
  509. <td>
  510. The <code>add</code> event is triggered when an item
  511. or a set of items is added, or when an item is updated while
  512. not yet existing.
  513. </td>
  514. </tr>
  515. <tr>
  516. <td>update</td>
  517. <td>
  518. The <code>update</code> event is triggered when an existing item
  519. or a set of existing items is updated.
  520. </td>
  521. </tr>
  522. <tr>
  523. <td>remove</td>
  524. <td>
  525. The <code>remove</code> event is triggered when an item
  526. or a set of items is removed.
  527. </td>
  528. </tr>
  529. <tr>
  530. <td>*</td>
  531. <td>
  532. The <code>*</code> event is triggered when any of the events
  533. <code>add</code>, <code>update</code>, and <code>remove</code>
  534. occurs.
  535. </td>
  536. </tr>
  537. </table>
  538. <h3 id="Callback">Callback</h3>
  539. <p>
  540. The callback functions of subscribers are called with the following
  541. parameters:
  542. </p>
  543. <pre class="prettyprint lang-js">
  544. function (event, properties, senderId) {
  545. // handle the event
  546. });
  547. </pre>
  548. <p>
  549. where the parameters are defined as
  550. </p>
  551. <table>
  552. <tr>
  553. <th>Parameter</th>
  554. <th>Type</th>
  555. <th>Description</th>
  556. </tr>
  557. <tr>
  558. <td>event</td>
  559. <td>String</td>
  560. <td>
  561. Any of the available events: <code>add</code>,
  562. <code>update</code>, or <code>remove</code>.
  563. </td>
  564. </tr>
  565. <tr>
  566. <td>properties</td>
  567. <td>Object&nbsp;|&nbsp;null</td>
  568. <td>
  569. Optional properties providing more information on the event.
  570. In case of the events <code>add</code>,
  571. <code>update</code>, and <code>remove</code>,
  572. <code>properties</code> is always an object containing a property
  573. items, which contains an array with the ids of the affected
  574. items.
  575. </td>
  576. </tr>
  577. <tr>
  578. <td>senderId</td>
  579. <td>String&nbsp;|&nbsp;Number</td>
  580. <td>
  581. An senderId, optionally provided by the application code
  582. which triggered the event. If senderId is not provided, the
  583. argument will be <code>null</code>.
  584. </td>
  585. </tr>
  586. </table>
  587. <h2 id="Data_Policy">Data Policy</h2>
  588. <p>
  589. All code and data is processed and rendered in the browser.
  590. No data is sent to any server.
  591. </p>
  592. </div>
  593. </body>
  594. </html>