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.

390 lines
12 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>DataView - 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>DataView</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="#Getting_Data">Getting Data</a></li>
  68. <li><a href="#Subscriptions">Subscriptions</a></li>
  69. </ul>
  70. <h2 id="Overview">Overview</h2>
  71. <p>
  72. A DataView offers a filtered and/or formatted view on a
  73. <a href="dataset.html">DataSet</a>.
  74. One can subscribe to changes in a DataView, and easily get filtered or
  75. formatted data without having to specify filters and field types all
  76. the time.
  77. </p>
  78. <h2 id="Example">Example</h2>
  79. <p>
  80. The following example shows how to use a DataView.
  81. </p>
  82. <pre class="prettyprint lang-js">
  83. // create a DataSet
  84. var data = new vis.DataSet();
  85. data.add([
  86. {id: 1, text: 'item 1', date: new Date(2013, 6, 20), group: 1, first: true},
  87. {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
  88. {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
  89. {id: 4, text: 'item 4'}
  90. ]);
  91. // create a DataView
  92. // the view will only contain items having a property group with value 1,
  93. // and will only output fields id, text, and date.
  94. var view = new vis.DataView(data, {
  95. filter: function (item) {
  96. return (item.group == 1);
  97. },
  98. fields: ['id', 'text', 'date']
  99. });
  100. // subscribe to any change in the DataView
  101. view.on('*', function (event, properties, senderId) {
  102. console.log('event', event, properties);
  103. });
  104. // update an item in the data set
  105. data.update({id: 2, group: 1});
  106. // get all ids in the view
  107. var ids = view.getIds();
  108. console.log('ids', ids); // will output [1, 2]
  109. // get all items in the view
  110. var items = view.get();
  111. </pre>
  112. <h2 id="Construction">Construction</h2>
  113. <p>
  114. A DataView can be constructed as:
  115. </p>
  116. <pre class="prettyprint lang-js">
  117. var data = new vis.DataView(dataset, options)
  118. </pre>
  119. <p>
  120. where:
  121. </p>
  122. <ul>
  123. <li>
  124. <code>dataset</code> is a DataSet or DataView.
  125. </li>
  126. <li>
  127. <code>options</code> is an object which can
  128. contain the following properties. Note that these properties
  129. are exactly the same as the properties available in methods
  130. <code>DataSet.get</code> and <code>DataView.get</code>.
  131. <table class="options">
  132. <tr>
  133. <th>Name</th>
  134. <th>Type</th>
  135. <th>Default</th>
  136. <th>Description</th>
  137. </tr>
  138. <tr>
  139. <td>convert</td>
  140. <td>Object.&lt;String,&nbsp;String&gt;</td>
  141. <td>none</td>
  142. <td>
  143. An object containing field names as key, and data types as value.
  144. By default, the type of the properties of an item are left
  145. unchanged. When a field type is specified, this field in the
  146. items will be converted to the specified type. This can be used
  147. for example to convert ISO strings containing a date to a
  148. JavaScript Date object, or convert strings to numbers or vice
  149. versa. The available data types are listed in section
  150. <a href="dataset.html#Data_Types">Data Types</a>.
  151. </td>
  152. </tr>
  153. <tr>
  154. <td>fields</td>
  155. <td>String[&nbsp;] | Object.&lt;String,&nbsp;String&gt;</td>
  156. <td>none</td>
  157. <td>
  158. An array with field names, or an object with current field name and
  159. new field name that the field is returned as.
  160. By default, all properties of the items are emitted.
  161. When <code>fields</code> is defined, only the properties
  162. whose name is specified in <code>fields</code> will be included
  163. in the returned items.
  164. </td>
  165. </tr>
  166. <tr>
  167. <td>filter</td>
  168. <td>function</td>
  169. <td>none</td>
  170. <td>Items can be filtered on specific properties by providing a filter
  171. function. A filter function is executed for each of the items in the
  172. DataSet, and is called with the item as parameter. The function must
  173. return a boolean. All items for which the filter function returns
  174. true will be emitted.
  175. See also section <a href="dataset.html#Data_Filtering">Data Filtering</a>.</td>
  176. </tr>
  177. </table>
  178. </li>
  179. </ul>
  180. <h2 id="Methods">Methods</h2>
  181. <p>DataView contains the following methods.</p>
  182. <table class="methods">
  183. <tr>
  184. <th>Method</th>
  185. <th>Return Type</th>
  186. <th>Description</th>
  187. </tr>
  188. <tr>
  189. <td>
  190. get([options] [, data])<br>
  191. get(id [,options] [, data])<br>
  192. get(ids&nbsp;[,&nbsp;options]&nbsp;[,&nbsp;data])
  193. </td>
  194. <td>Object | Array</td>
  195. <td>
  196. Get a single item, multiple items, or all items from the DataView.
  197. 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>.
  198. </td>
  199. </tr>
  200. <tr>
  201. <td>
  202. getDataSet()
  203. </td>
  204. <td>DataSet</td>
  205. <td>
  206. Get the DataSet to which the DataView is connected.
  207. </td>
  208. </tr>
  209. <tr>
  210. <td>
  211. getIds([options])
  212. </td>
  213. <td>Number[]</td>
  214. <td>
  215. Get ids of all items or of a filtered set of items.
  216. Available <code>options</code> are described in section <a href="dataset.html#Data_Selection">Data Selection</a>, except that options <code>fields</code> and <code>type</code> are not applicable in case of <code>getIds</code>.
  217. </td>
  218. </tr>
  219. <tr>
  220. <td>off(event, callback)</td>
  221. <td>none</td>
  222. <td>
  223. Unsubscribe from an event, remove an event listener. See section <a href="#Subscriptions">Subscriptions</a>.
  224. </td>
  225. </tr>
  226. <tr>
  227. <td>on(event, callback)</td>
  228. <td>none</td>
  229. <td>
  230. Subscribe to an event, add an event listener. See section <a href="#Subscriptions">Subscriptions</a>.
  231. </td>
  232. </tr>
  233. <tr>
  234. <td>refresh()</td>
  235. <td>none</td>
  236. <td>
  237. Refresh the filter results of a DataView. Useful when the filter function contains dynamic properties, like:
  238. <pre class="prettyprint lang-js">var data = new vis.DataSet(...);
  239. var view = new vis.DataView(data, {
  240. filter: function (item) {
  241. return item.value > threshold;
  242. }
  243. });</pre>
  244. In this example, <code>threshold</code> is an external parameter. When the value of <code>threshold</code> changes, the DataView must be notified that the filter results may have changed by calling <code>DataView.refresh()</code>.
  245. </td>
  246. </tr>
  247. <tr>
  248. <td>
  249. setDataSet(data)
  250. </td>
  251. <td>none</td>
  252. <td>
  253. Replace the DataSet of the DataView. Parameter <code>data</code> can be a DataSet or a DataView.
  254. </td>
  255. </tr>
  256. </table>
  257. <h2 id="Properties">Properties</h2>
  258. <p>DataView contains the following properties.</p>
  259. <table>
  260. <colgroup>
  261. <col width="200">
  262. </colgroup>
  263. <tr>
  264. <th>Property</th>
  265. <th>Type</th>
  266. <th>Description</th>
  267. </tr>
  268. <tr>
  269. <td>length</td>
  270. <td>Number</td>
  271. <td>The number of items in the DataView.</td>
  272. </tr>
  273. </table>
  274. <h2 id="Getting_Data">Getting Data</h2>
  275. <p>
  276. Data of the DataView can be retrieved using the method <code>get</code>.
  277. </p>
  278. <pre class="prettyprint lang-js">
  279. var items = view.get();
  280. </pre>
  281. <p>
  282. Data of a DataView can be filtered and formatted again, in exactly the
  283. same way as in a DataSet. See sections
  284. <a href="dataset.html#Data_Manipulation">Data Manipulation</a> and
  285. <a href="dataset.html#Data_Selection">Data Selection</a> for more
  286. information.
  287. </p>
  288. <pre class="prettyprint lang-js">
  289. var items = view.get({
  290. fields: ['id', 'score'],
  291. filter: function (item) {
  292. return (item.score > 50);
  293. }
  294. });
  295. </pre>
  296. <h2 id="Subscriptions">Subscriptions</h2>
  297. <p>
  298. One can subscribe on changes in the DataView. Subscription works exactly
  299. the same as for DataSets. See the documentation on
  300. <a href="dataset.html#Subscriptions">subscriptions in a DataSet</a>
  301. for more information.
  302. </p>
  303. <pre class="prettyprint lang-js">
  304. // create a DataSet and a view on the data set
  305. var data = new vis.DataSet();
  306. var view = new vis.DataView({
  307. filter: function (item) {
  308. return (item.group == 2);
  309. }
  310. });
  311. // subscribe to any change in the DataView
  312. view.on('*', function (event, properties, senderId) {
  313. console.log('event:', event, 'properties:', properties, 'senderId:', senderId);
  314. });
  315. // add, update, and remove data in the DataSet...
  316. </pre>
  317. </div>
  318. <!-- Bootstrap core JavaScript
  319. ================================================== -->
  320. <!-- Placed at the end of the document so the pages load faster -->
  321. <script src="../js/jquery.min.js"></script>
  322. <script src="../js/bootstrap.min.js"></script>
  323. <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  324. <script src="../js/ie10-viewport-bug-workaround.js"></script>
  325. <!-- controller -->
  326. <script src="../js/main.js"></script>