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.

955 lines
25 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. <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-61231638-1', 'auto');ga('send', 'pageview');</script></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="#Methods">Methods</a></li>
  18. <li><a href="#Properties">Properties</a></li>
  19. <li><a href="#Subscriptions">Subscriptions</a></li>
  20. <li><a href="#Data_Manipulation">Data Manipulation</a></li>
  21. <li><a href="#Data_Selection">Data Selection</a></li>
  22. <li><a href="#Data_Policy">Data Policy</a></li>
  23. </ul>
  24. <h2 id="Overview">Overview</h2>
  25. <p>
  26. Vis.js comes with a flexible DataSet, which can be used to hold and
  27. manipulate unstructured data and listen for changes in the data.
  28. The DataSet is key/value based. Data items can be added, updated and
  29. removed from the DatSet, and one can subscribe to changes in the DataSet.
  30. The data in the DataSet can be filtered and ordered, and fields (like
  31. dates) can be converted to a specific type. Data can be normalized when
  32. appending it to the DataSet as well.
  33. </p>
  34. <h2 id="Example">Example</h2>
  35. <p>
  36. The following example shows how to use a DataSet.
  37. </p>
  38. <pre class="prettyprint lang-js">
  39. // create a DataSet
  40. var options = {};
  41. var data = new vis.DataSet(options);
  42. // add items
  43. // note that the data items can contain different properties and data formats
  44. data.add([
  45. {id: 1, text: 'item 1', date: new Date(2013, 6, 20), group: 1, first: true},
  46. {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
  47. {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
  48. {id: 4, text: 'item 4'}
  49. ]);
  50. // subscribe to any change in the DataSet
  51. data.on('*', function (event, properties, senderId) {
  52. console.log('event', event, properties);
  53. });
  54. // update an existing item
  55. data.update({id: 2, group: 1});
  56. // remove an item
  57. data.remove(4);
  58. // get all ids
  59. var ids = data.getIds();
  60. console.log('ids', ids);
  61. // get a specific item
  62. var item1 = data.get(1);
  63. console.log('item1', item1);
  64. // retrieve a filtered subset of the data
  65. var items = data.get({
  66. filter: function (item) {
  67. return item.group == 1;
  68. }
  69. });
  70. console.log('filtered items', items);
  71. // retrieve formatted items
  72. var items = data.get({
  73. fields: ['id', 'date'],
  74. type: {
  75. date: 'ISODate'
  76. }
  77. });
  78. console.log('formatted items', items);
  79. </pre>
  80. <h2 id="Construction">Construction</h2>
  81. <p>
  82. A DataSet can be constructed as:
  83. </p>
  84. <pre class="prettyprint lang-js">
  85. var data = new vis.DataSet([data] [, options])
  86. </pre>
  87. <p>
  88. After construction, data can be added to the DataSet using the methods
  89. <code>add</code> and <code>update</code>, as described in section
  90. <a href="#Data_Manipulation">Data Manipulation</a>.
  91. </p>
  92. <p>
  93. The parameter <code>data</code> is optional and can be an Array or
  94. <a href="https://developers.google.com/chart/interactive/docs/reference#DataTable" target="_blank">Google DataTable</a> with items.
  95. </p>
  96. <p>
  97. The parameter <code>options</code> is optional and is an object which can
  98. contain the following properties:
  99. </p>
  100. <table>
  101. <tr>
  102. <th>Name</th>
  103. <th>Type</th>
  104. <th>Default value</th>
  105. <th>Description</th>
  106. </tr>
  107. <tr>
  108. <td>fieldId</td>
  109. <td>String</td>
  110. <td>"id"</td>
  111. <td>
  112. The name of the field containing the id of the items.
  113. When data is fetched from a server which uses some specific
  114. field to identify items, this field name can be specified
  115. in the DataSet using the option <code>fieldId</code>.
  116. For example <a href="http://couchdb.apache.org/"
  117. target="_blank">CouchDB</a> uses the field
  118. <code>"_id"</code> to identify documents.
  119. </td>
  120. </tr>
  121. <tr>
  122. <td>type</td>
  123. <td>Object.&lt;String,&nbsp;String&gt;</td>
  124. <td>none</td>
  125. <td>
  126. An object containing field names as key, and data types as
  127. value. By default, the type of the properties of items are left
  128. unchanged. Item properties can be normalized by specifying a
  129. field type. This is useful for example to automatically convert
  130. stringified dates coming from a server into JavaScript Date
  131. objects. The available data types are listed in section
  132. <a href="#Data_Types">Data Types</a>.
  133. </td>
  134. </tr>
  135. <tr>
  136. <td>queue</td>
  137. <td>Object | boolean</td>
  138. <td>none</td>
  139. <td>
  140. Queue data changes ('add', 'update', 'remove') and flush them at once.
  141. The queue can be flushed manually by calling
  142. <code>DataSet.flush()</code>, or can be flushed after a configured delay
  143. or maximum number of entries.
  144. <br>
  145. <br>
  146. When <code>queue</code> is true, a queue is created
  147. with default options. Options can be specified by providing an object:
  148. <ul>
  149. <li><code>delay: number</code><br>
  150. The queue will be flushed automatically after an inactivity of this
  151. delay in milliseconds. Default value is <code>null</code>.
  152. <li><code>max: number</code><br>
  153. When the queue exceeds the given maximum number
  154. of entries, the queue is flushed automatically.
  155. Default value is <code>Infinity</code>.
  156. </li>
  157. </ul>
  158. </td>
  159. </tr>
  160. </table>
  161. <h2 id="Methods">Methods</h2>
  162. <p>DataSet contains the following methods.</p>
  163. <table>
  164. <colgroup>
  165. <col width="200">
  166. </colgroup>
  167. <tr>
  168. <th>Method</th>
  169. <th>Return Type</th>
  170. <th>Description</th>
  171. </tr>
  172. <tr>
  173. <td>add(data [, senderId])</td>
  174. <td>Number[]</td>
  175. <td>Add one or multiple items to the DataSet. <code>data</code> can be a single item or an array with items. Adding an item will fail when there already is an item with the same id. The function returns an array with the ids of the added items. See section <a href="#Data_Manipulation">Data Manipulation</a>.</td>
  176. </tr>
  177. <tr>
  178. <td>clear([senderId])</td>
  179. <td>Number[]</td>
  180. <td>Clear all data from the DataSet. The function returns an array with the ids of the removed items.</td>
  181. </tr>
  182. <tr>
  183. <td>distinct(field)</td>
  184. <td>Array</td>
  185. <td>Find all distinct values of a specified field. Returns an unordered array containing all distinct values. If data items do not contain the specified field are ignored.</td>
  186. </tr>
  187. <tr>
  188. <td>flush()</td>
  189. <td>none</td>
  190. <td>Flush queued changes. Only available when the DataSet is configured with the option <code>queue</code>, see section <a href="#Construction">Construction</a>.</td>
  191. </tr>
  192. <tr>
  193. <td>forEach(callback [, options])</td>
  194. <td>none</td>
  195. <td>
  196. Execute a callback function for every item in the dataset.
  197. The available options are described in section <a href="#Data_Selection">Data Selection</a>.
  198. </td>
  199. </tr>
  200. <tr>
  201. <td>
  202. get([options] [, data])<br>
  203. get(id [,options] [, data])<br>
  204. get(ids [, options] [, data])
  205. </td>
  206. <td>Object | Array | DataTable</td>
  207. <td>
  208. Get a single item, multiple items, or all items from the DataSet.
  209. Usage examples can be found in section <a href="#Getting_Data">Getting Data</a>, and the available <code>options</code> are described in section <a href="#Data_Selection">Data Selection</a>. If parameter <code>data</code> is provided, items will be appended to this array or table, which is required in case of Google DataTable.
  210. </td>
  211. </tr>
  212. <tr>
  213. <td>
  214. getDataSet()
  215. </td>
  216. <td>DataSet</td>
  217. <td>
  218. Get the DataSet itself. In case of a DataView, this function does not
  219. return the DataSet to which the DataView is connected.
  220. </td>
  221. </tr>
  222. <tr>
  223. <td>
  224. getIds([options])
  225. </td>
  226. <td>Number[]</td>
  227. <td>
  228. Get ids of all items or of a filtered set of items.
  229. Available <code>options</code> are described in section <a href="#Data_Selection">Data Selection</a>, except that options <code>fields</code> and <code>type</code> are not applicable in case of <code>getIds</code>.
  230. </td>
  231. </tr>
  232. <tr>
  233. <td>map(callback [, options])</td>
  234. <td>Array</td>
  235. <td>
  236. Map every item in the DataSet.
  237. The available options are described in section <a href="#Data_Selection">Data Selection</a>.
  238. </td>
  239. </tr>
  240. <tr>
  241. <td>max(field)</td>
  242. <td>Object | null</td>
  243. <td>
  244. Find the item with maximum value of specified field. Returns <code>null</code> if no item is found.
  245. </td>
  246. </tr>
  247. <tr>
  248. <td>min(field)</td>
  249. <td>Object | null</td>
  250. <td>
  251. Find the item with minimum value of specified field. Returns <code>null</code> if no item is found.
  252. </td>
  253. </tr>
  254. <tr>
  255. <td>off(event, callback)</td>
  256. <td>none</td>
  257. <td>
  258. Unsubscribe from an event, remove an event listener. See section <a href="#Subscriptions">Subscriptions</a>.
  259. </td>
  260. </tr>
  261. <tr>
  262. <td>on(event, callback)</td>
  263. <td>none</td>
  264. <td>
  265. Subscribe to an event, add an event listener. See section <a href="#Subscriptions">Subscriptions</a>.
  266. </td>
  267. </tr>
  268. <tr>
  269. <td>
  270. remove(id [, senderId])<br>
  271. remove(ids [, senderId])
  272. </td>
  273. <td>Number[]</td>
  274. <td>
  275. Remove one or multiple items by id or by the items themselves. Returns an array with the ids of the removed items. See section <a href="#Data_Manipulation">Data Manipulation</a>.
  276. </td>
  277. </tr>
  278. <tr>
  279. <td>
  280. setOptions(options)
  281. </td>
  282. <td>none</td>
  283. <td>
  284. Set options for the DataSet. Available options:
  285. <ul>
  286. <li>
  287. <code>queue</code><br>
  288. Queue data changes ('add', 'update', 'remove') and flush them at once.
  289. The queue can be flushed manually by calling
  290. <code>DataSet.flush()</code>, or can be flushed after a configured delay
  291. or maximum number of entries.
  292. <br>
  293. <br>
  294. When <code>queue</code> is true, a queue is created with default options.
  295. When <code>queue</code> is false, an existing queue will be flushed and removed.
  296. Options can be specified by providing an object:
  297. <ul>
  298. <li><code>delay: number</code><br>
  299. The queue will be flushed automatically after an inactivity of this
  300. delay in milliseconds. Default value is <code>null</code>.
  301. <li><code>max: number</code><br>
  302. When the queue exceeds the given maximum number
  303. of entries, the queue is flushed automatically.
  304. Default value is <code>Infinity</code>.
  305. </li>
  306. </ul>
  307. </li>
  308. </ul>
  309. </td>
  310. </tr>
  311. <tr>
  312. <td>
  313. update(data [, senderId])
  314. </td>
  315. <td>Number[]</td>
  316. <td>
  317. Update on ore multiple existing items. <code>data</code> can be a single item or an array with items. When an item doesn't exist, it will be created. Returns an array with the ids of the removed items. See section <a href="#Data_Manipulation">Data Manipulation</a>.
  318. </td>
  319. </tr>
  320. </table>
  321. <h2 id="Properties">Properties</h2>
  322. <p>DataSet contains the following properties.</p>
  323. <table>
  324. <colgroup>
  325. <col width="200">
  326. </colgroup>
  327. <tr>
  328. <th>Property</th>
  329. <th>Type</th>
  330. <th>Description</th>
  331. </tr>
  332. <tr>
  333. <td>length</td>
  334. <td>Number</td>
  335. <td>The number of items in the DataSet.</td>
  336. </tr>
  337. </table>
  338. <h2 id="Subscriptions">Subscriptions</h2>
  339. <p>
  340. One can subscribe on changes in a DataSet.
  341. A subscription can be created using the method <code>on</code>,
  342. and removed with <code>off</code>.
  343. </p>
  344. <pre class="prettyprint lang-js">
  345. // create a DataSet
  346. var data = new vis.DataSet();
  347. // subscribe to any change in the DataSet
  348. data.on('*', function (event, properties, senderId) {
  349. console.log('event:', event, 'properties:', properties, 'senderId:', senderId);
  350. });
  351. // add an item
  352. data.add({id: 1, text: 'item 1'}); // triggers an 'add' event
  353. data.update({id: 1, text: 'item 1 (updated)'}); // triggers an 'update' event
  354. data.remove(1); // triggers an 'remove' event
  355. </pre>
  356. <h3 id="On">On</h3>
  357. <p>
  358. Subscribe to an event.
  359. </p>
  360. Syntax:
  361. <pre class="prettyprint lang-js">DataSet.on(event, callback)</pre>
  362. Where:
  363. <ul>
  364. <li>
  365. <code>event</code> is a String containing any of the events listed
  366. in section <a href="#Events">Events</a>.
  367. </li>
  368. <li>
  369. <code>callback</code> is a callback function which will be called
  370. each time the event occurs. The callback function is described in
  371. section <a href="#Callback">Callback</a>.
  372. </li>
  373. </ul>
  374. <h3 id="Off">Off</h3>
  375. <p>
  376. Unsubscribe from an event.
  377. </p>
  378. Syntax:
  379. <pre class="prettyprint lang-js">DataSet.off(event, callback)</pre>
  380. Where <code>event</code> and <code>callback</code> correspond with the
  381. parameters used to <a href="#On">subscribe</a> to the event.
  382. <h3 id="Events">Events</h3>
  383. <p>
  384. The following events are available for subscription:
  385. </p>
  386. <table>
  387. <tr>
  388. <th>Event</th>
  389. <th>Description</th>
  390. </tr>
  391. <tr>
  392. <td>add</td>
  393. <td>
  394. The <code>add</code> event is triggered when an item
  395. or a set of items is added, or when an item is updated while
  396. not yet existing.
  397. </td>
  398. </tr>
  399. <tr>
  400. <td>update</td>
  401. <td>
  402. The <code>update</code> event is triggered when an existing item
  403. or a set of existing items is updated.
  404. </td>
  405. </tr>
  406. <tr>
  407. <td>remove</td>
  408. <td>
  409. The <code>remove</code> event is triggered when an item
  410. or a set of items is removed.
  411. </td>
  412. </tr>
  413. <tr>
  414. <td>*</td>
  415. <td>
  416. The <code>*</code> event is triggered when any of the events
  417. <code>add</code>, <code>update</code>, and <code>remove</code>
  418. occurs.
  419. </td>
  420. </tr>
  421. </table>
  422. <h3 id="Callback">Callback</h3>
  423. <p>
  424. The callback functions of subscribers are called with the following
  425. parameters:
  426. </p>
  427. <pre class="prettyprint lang-js">
  428. function (event, properties, senderId) {
  429. // handle the event
  430. });
  431. </pre>
  432. <p>
  433. where the parameters are defined as
  434. </p>
  435. <table>
  436. <tr>
  437. <th>Parameter</th>
  438. <th>Type</th>
  439. <th>Description</th>
  440. </tr>
  441. <tr>
  442. <td>event</td>
  443. <td>String</td>
  444. <td>
  445. Any of the available events: <code>add</code>,
  446. <code>update</code>, or <code>remove</code>.
  447. </td>
  448. </tr>
  449. <tr>
  450. <td>properties</td>
  451. <td>Object&nbsp;|&nbsp;null</td>
  452. <td>
  453. Optional properties providing more information on the event.
  454. In case of the events <code>add</code>,
  455. <code>update</code>, and <code>remove</code>,
  456. <code>properties</code> is always an object containing a property
  457. <code>items</code>, which contains an array with the ids of the affected
  458. items. The <code>update</code> event has an extra field <code>data</code>
  459. containing the original data of the updated items, i.e. the gives the
  460. changed fields of the changed items.
  461. </td>
  462. </tr>
  463. <tr>
  464. <td>senderId</td>
  465. <td>String&nbsp;|&nbsp;Number</td>
  466. <td>
  467. An senderId, optionally provided by the application code
  468. which triggered the event. If senderId is not provided, the
  469. argument will be <code>null</code>.
  470. </td>
  471. </tr>
  472. </table>
  473. <h2 id="Data_Manipulation">Data Manipulation</h2>
  474. <p>
  475. The data in a DataSet can be manipulated using the methods
  476. <a href="#Add"><code>add</code></a>,
  477. <a href="#Update"><code>update</code></a>,
  478. and <a href="#Remove"><code>remove</code></a>.
  479. The DataSet can be emptied using the method
  480. <a href="#Clear"><code>clear</code></a>.
  481. </p>
  482. <pre class="prettyprint lang-js">
  483. // create a DataSet
  484. var data = new vis.DataSet();
  485. // add items
  486. data.add([
  487. {id: 1, text: 'item 1'},
  488. {id: 2, text: 'item 2'},
  489. {id: 3, text: 'item 3'}
  490. ]);
  491. // update an item
  492. data.update({id: 2, text: 'item 2 (updated)'});
  493. // remove an item
  494. data.remove(3);
  495. </pre>
  496. <h3 id="Add">Add</h3>
  497. <p>
  498. Add a data item or an array with items.
  499. </p>
  500. Syntax:
  501. <pre class="prettyprint lang-js">var addedIds = DataSet.add(data [, senderId])</pre>
  502. The argument <code>data</code> can contain:
  503. <ul>
  504. <li>
  505. An <code>Object</code> containing a single item to be
  506. added. The item must contain an id.
  507. </li>
  508. <li>
  509. An <code>Array</code> or
  510. <code>google.visualization.DataTable</code> containing
  511. a list with items to be added. Each item must contain
  512. an id.
  513. </li>
  514. </ul>
  515. <p>
  516. After the items are added to the DataSet, the DataSet will
  517. trigger an event <code>add</code>. When a <code>senderId</code>
  518. is provided, this id will be passed with the triggered
  519. event to all subscribers.
  520. </p>
  521. <p>
  522. The method will throw an Error when an item with the same id
  523. as any of the added items already exists.
  524. </p>
  525. <h3 id="Update">Update</h3>
  526. <p>
  527. Update a data item or an array with items.
  528. </p>
  529. Syntax:
  530. <pre class="prettyprint lang-js">var updatedIds = DataSet.update(data [, senderId])</pre>
  531. The argument <code>data</code> can contain:
  532. <ul>
  533. <li>
  534. An <code>Object</code> containing a single item to be
  535. updated. The item must contain an id.
  536. </li>
  537. <li>
  538. An <code>Array</code> or
  539. <code>google.visualization.DataTable</code> containing
  540. a list with items to be updated. Each item must contain
  541. an id.
  542. </li>
  543. </ul>
  544. <p>
  545. The provided properties will be merged in the existing item.
  546. When an item does not exist, it will be created.
  547. </p>
  548. <p>
  549. After the items are updated, the DataSet will
  550. trigger an event <code>add</code> for the added items, and
  551. an event <code>update</code>. When a <code>senderId</code>
  552. is provided, this id will be passed with the triggered
  553. event to all subscribers.
  554. </p>
  555. <h3 id="Remove">Remove</h3>
  556. <p>
  557. Remove a data item or an array with items.
  558. </p>
  559. Syntax:
  560. <pre class="prettyprint lang-js">var removedIds = DataSet.remove(id [, senderId])</pre>
  561. <p>
  562. The argument <code>id</code> can be:
  563. </p>
  564. <ul>
  565. <li>
  566. A <code>Number</code> or <code>String</code> containing the id
  567. of a single item to be removed.
  568. </li>
  569. <li>
  570. An <code>Object</code> containing the item to be deleted.
  571. The item will be deleted by its id.
  572. </li>
  573. <li>
  574. An Array containing ids or items to be removed.
  575. </li>
  576. </ul>
  577. <p>
  578. The method ignores removal of non-existing items, and returns an array
  579. containing the ids of the items which are actually removed from the
  580. DataSet.
  581. </p>
  582. <p>
  583. After the items are removed, the DataSet will
  584. trigger an event <code>remove</code> for the removed items.
  585. When a <code>senderId</code> is provided, this id will be passed with
  586. the triggered event to all subscribers.
  587. </p>
  588. <h3 id="Clear">Clear</h3>
  589. <p>
  590. Clear the complete DataSet.
  591. </p>
  592. Syntax:
  593. <pre class="prettyprint lang-js">var removedIds = DataSet.clear([senderId])</pre>
  594. <p>
  595. After the items are removed, the DataSet will
  596. trigger an event <code>remove</code> for all removed items.
  597. When a <code>senderId</code> is provided, this id will be passed with
  598. the triggered event to all subscribers.
  599. </p>
  600. <h2 id="Data_Selection">Data Selection</h2>
  601. <p>
  602. The DataSet contains functionality to format, filter, and sort data retrieved via the
  603. methods <code>get</code>, <code>getIds</code>, <code>forEach</code>, and <code>map</code>. These methods have the following syntax:
  604. </p>
  605. <pre class="prettyprint lang-js">
  606. DataSet.get([id] [, options] [, data]);
  607. DataSet.getIds([options]);
  608. DataSet.forEach(callback [, options]);
  609. DataSet.map(callback [, options]);
  610. </pre>
  611. <p>
  612. Where <code>options</code> is an Object which can have the following
  613. properties:
  614. </p>
  615. <table>
  616. <tr>
  617. <th>Name</th>
  618. <th>Type</th>
  619. <th>Description</th>
  620. </tr>
  621. <tr>
  622. <td>fields</td>
  623. <td>String[&nbsp;]</td>
  624. <td>
  625. An array with field names.
  626. By default, all properties of the items are emitted.
  627. When <code>fields</code> is defined, only the properties
  628. whose name is specified in <code>fields</code> will be included
  629. in the returned items.
  630. </td>
  631. </tr>
  632. <tr>
  633. <td>type</td>
  634. <td>Object.&lt;String,&nbsp;String&gt;</td>
  635. <td>
  636. An object containing field names as key, and data types as value.
  637. By default, the type of the properties of an item are left
  638. unchanged. When a field type is specified, this field in the
  639. items will be converted to the specified type. This can be used
  640. for example to convert ISO strings containing a date to a
  641. JavaScript Date object, or convert strings to numbers or vice
  642. versa. The available data types are listed in section
  643. <a href="#Data_Types">Data Types</a>.
  644. </td>
  645. </tr>
  646. <tr>
  647. <td>filter</td>
  648. <td>Function</td>
  649. <td>Items can be filtered on specific properties by providing a filter
  650. function. A filter function is executed for each of the items in the
  651. DataSet, and is called with the item as parameter. The function must
  652. return a boolean. All items for which the filter function returns
  653. true will be emitted.
  654. See section <a href="#Data_Filtering">Data Filtering</a>.</td>
  655. </tr>
  656. <tr>
  657. <td>order</td>
  658. <td>String | Function</td>
  659. <td>Order the items by a field name or custom sort function.</td>
  660. </tr>
  661. <tr>
  662. <td>returnType</td>
  663. <td>String</td>
  664. <td>Determine the type of output of the get function. Allowed values are <code>Array | Object | DataTable</code>.
  665. The <code>DataTable</code> refers to a Google DataTable. The default returnType is an array. The object type will return a JSON object with the ID's as keys.</td>
  666. </tr>
  667. </table>
  668. <p>
  669. The following example demonstrates formatting properties and filtering
  670. properties from items.
  671. </p>
  672. <pre class="prettyprint lang-js">
  673. // create a DataSet
  674. var data = new vis.DataSet();
  675. data.add([
  676. {id: 1, text: 'item 1', date: '2013-06-20', group: 1, first: true},
  677. {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
  678. {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
  679. {id: 4, text: 'item 4'}
  680. ]);
  681. // retrieve formatted items
  682. var items = data.get({
  683. fields: ['id', 'date', 'group'], // output the specified fields only
  684. type: {
  685. date: 'Date', // convert the date fields to Date objects
  686. group: 'String' // convert the group fields to Strings
  687. }
  688. });
  689. </pre>
  690. <h3 id="Getting_Data">Getting Data</h3>
  691. <p>
  692. Data can be retrieved from the DataSet using the method <code>get</code>.
  693. This method can return a single item or a list with items.
  694. </p>
  695. <p>A single item can be retrieved by its id:</p>
  696. <pre class="prettyprint lang-js">
  697. var item1 = dataset.get(1);
  698. </pre>
  699. <p>A selection of items can be retrieved by providing an array with ids:</p>
  700. <pre class="prettyprint lang-js">
  701. var items = dataset.get([1, 3, 4]); // retrieve items 1, 3, and 4
  702. </pre>
  703. <p>All items can be retrieved by simply calling <code>get</code> without
  704. specifying an id:</p>
  705. <pre class="prettyprint lang-js">
  706. var items = dataset.get(); // retrieve all items
  707. </pre>
  708. <h3 id="Data_Filtering">Data Filtering</h3>
  709. <p>
  710. Items can be filtered on specific properties by providing a filter
  711. function. A filter function is executed for each of the items in the
  712. DataSet, and is called with the item as parameter. The function must
  713. return a boolean. All items for which the filter function returns
  714. true will be emitted.
  715. </p>
  716. <pre class="prettyprint lang-js">
  717. // retrieve all items having a property group with value 2
  718. var group2 = dataset.get({
  719. filter: function (item) {
  720. return (item.group == 2);
  721. }
  722. });
  723. // retrieve all items having a property balance with a value above zero
  724. var positiveBalance = dataset.get({
  725. filter: function (item) {
  726. return (item.balance > 0);
  727. }
  728. });
  729. </pre>
  730. <h3 id="Data_Types">Data Types</h3>
  731. <p>
  732. DataSet supports the following data types:
  733. </p>
  734. <table style="width: 100%">
  735. <tr>
  736. <th>Name</th>
  737. <th>Description</th>
  738. <th>Examples</th>
  739. </tr>
  740. <tr>
  741. <td>Boolean</td>
  742. <td>A JavaScript Boolean</td>
  743. <td>
  744. <code>true</code><br>
  745. <code>false</code>
  746. </td>
  747. </tr>
  748. <tr>
  749. <td>Number</td>
  750. <td>A JavaScript Number</td>
  751. <td>
  752. <code>32</code><br>
  753. <code>2.4</code>
  754. </td>
  755. </tr>
  756. <tr>
  757. <td>String</td>
  758. <td>A JavaScript String</td>
  759. <td>
  760. <code>"hello world"</code><br>
  761. <code>"2013-06-28"</code>
  762. </td>
  763. </tr>
  764. <tr>
  765. <td>Date</td>
  766. <td>A JavaScript Date object</td>
  767. <td>
  768. <code>new Date()</code><br>
  769. <code>new Date(2013, 5, 28)</code><br>
  770. <code>new Date(1372370400000)</code>
  771. </td>
  772. </tr>
  773. <tr>
  774. <td>Moment</td>
  775. <td>A Moment object, created with
  776. <a href="http://momentjs.com/" target="_blank">moment.js</a></td>
  777. <td>
  778. <code>moment()</code><br>
  779. <code>moment('2013-06-28')</code>
  780. </td>
  781. </tr>
  782. <tr>
  783. <td>ISODate</td>
  784. <td>A string containing an ISO Date</td>
  785. <td>
  786. <code>new Date().toISOString()</code><br>
  787. <code>"2013-06-27T22:00:00.000Z"</code>
  788. </td>
  789. </tr>
  790. <tr>
  791. <td>ASPDate</td>
  792. <td>A string containing an ASP Date</td>
  793. <td>
  794. <code>"/Date(1372370400000)/"</code><br>
  795. <code>"/Date(1198908717056-0700)/"</code>
  796. </td>
  797. </tr>
  798. </table>
  799. <h2 id="Data_Policy">Data Policy</h2>
  800. <p>
  801. All code and data is processed and rendered in the browser.
  802. No data is sent to any server.
  803. </p>
  804. </div>
  805. </body>
  806. </html>