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.

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