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.

708 lines
20 KiB

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