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.

148 lines
3.5 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Data serialization</title>
  5. <style>
  6. body, html {
  7. font-family: arial, sans-serif;
  8. font-size: 11pt;
  9. }
  10. textarea {
  11. width: 800px;
  12. height: 200px;
  13. }
  14. .buttons {
  15. margin: 20px 0;
  16. }
  17. .buttons input {
  18. padding: 10px;
  19. }
  20. </style>
  21. <script src="../../../dist/vis.js"></script>
  22. <link href="../../../dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  23. </head>
  24. <body>
  25. <h1>Serialization and deserialization</h1>
  26. <p>This example shows how to serialize and deserialize JSON data, and load this in the Timeline via a DataSet. Serialization and deserialization is needed when loading or saving data from a server.</p>
  27. <textarea id="data">
  28. [
  29. {
  30. "id": 1,
  31. "content": "item 1",
  32. "start": "2014-01-01T01:00:00"
  33. },
  34. {
  35. "id": 2,
  36. "content": "item 2",
  37. "start": "2014-01-01T02:00:00"
  38. },
  39. {
  40. "id": 3,
  41. "content": "item 3",
  42. "start": "2014-01-01T03:00:00"
  43. },
  44. {
  45. "id": 4,
  46. "content": "item 4",
  47. "start": "2014-01-01T04:00:00",
  48. "end": "2014-01-01T04:30:00"
  49. },
  50. {
  51. "id": 5,
  52. "content": "item 5",
  53. "start": "2014-01-01T05:00:00",
  54. "type": "point"
  55. },
  56. {
  57. "id": 6,
  58. "content": "item 6",
  59. "start": "2014-01-01T06:00:00"
  60. }
  61. ]
  62. </textarea>
  63. <div class="buttons">
  64. <input type="button" id="load" value="&darr; Load" title="Load data from textarea into the Timeline">
  65. <input type="button" id="save" value="&uarr; Save" title="Save data from the Timeline into the textarea">
  66. </div>
  67. <div id="visualization"></div>
  68. <script>
  69. var txtData = document.getElementById('data');
  70. var btnLoad = document.getElementById('load');
  71. var btnSave = document.getElementById('save');
  72. // Create an empty DataSet.
  73. // This DataSet is used for two way data binding with the Timeline.
  74. var items = new vis.DataSet();
  75. // create a timeline
  76. var container = document.getElementById('visualization');
  77. var options = {
  78. editable: true
  79. };
  80. var timeline = new vis.Timeline(container, items, options);
  81. function loadData () {
  82. // get and deserialize the data
  83. var data = JSON.parse(txtData.value);
  84. // update the data in the DataSet
  85. //
  86. // Note: when retrieving updated data from a server instead of a complete
  87. // new set of data, one can simply update the existing data like:
  88. //
  89. // items.update(data);
  90. //
  91. // Existing items will then be updated, and new items will be added.
  92. items.clear();
  93. items.add(data);
  94. // adjust the timeline window such that we see the loaded data
  95. timeline.fit();
  96. }
  97. btnLoad.onclick = loadData;
  98. function saveData() {
  99. // get the data from the DataSet
  100. //
  101. // Note that we specify the output type of the fields start and end
  102. // as "ISODate", which is safely serializable. Other serializable types
  103. // are "Number" (unix timestamp), "ASPDate" or "String" (without timezone!).
  104. //
  105. // Alternatively, it is possible to configure the DataSet to convert
  106. // the output automatically to ISODates like:
  107. //
  108. // var options = {
  109. // type: {start: 'ISODate', end: 'ISODate'}
  110. // };
  111. // var items = new vis.DataSet(options);
  112. // // now items.get() will automatically convert start and end to ISO dates.
  113. //
  114. var data = items.get({
  115. type: {
  116. start: 'ISODate',
  117. end: 'ISODate'
  118. }
  119. });
  120. // serialize the data and put it in the textarea
  121. txtData.value = JSON.stringify(data, null, 2);
  122. }
  123. btnSave.onclick = saveData;
  124. // load the initial data
  125. loadData();
  126. </script>
  127. </body>
  128. </html>