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.

702 lines
20 KiB

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