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.

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