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.

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