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.

1001 lines
29 KiB

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