summaryrefslogtreecommitdiff
path: root/www/lib/vis/examples/timeline/dataHandling/dataSerialization.html
blob: 79b6aebf400d108203e92ff5df33981b964f8213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE HTML>
<html>
<head>
  <title>Timeline | Data serialization</title>

  <style>
    body, html {
      font-family: arial, sans-serif;
      font-size: 11pt;
    }

    textarea {
      width: 800px;
      height: 200px;
    }

    .buttons {
      margin: 20px 0;
    }

    .buttons input {
      padding: 10px;
    }
  </style>

  <script src="../../../dist/vis.js"></script>
  <link href="../../../dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  <script src="../../googleAnalytics.js"></script>
</head>
<body>

<h1>Serialization and deserialization</h1>

<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>

<textarea id="data">
[
  {"id": 1, "content": "item 1", "start": "2014-01-01 01:00:00"},
  {"id": 2, "content": "item 2", "start": "2014-01-01 02:00:00"},
  {"id": 3, "content": "item 3", "start": "2014-01-01 03:00:00"},
  {"id": 4, "content": "item 4", "start": "2014-01-01 04:00:00", "end": "2014-01-01 04:30:00"},
  {"id": 5, "content": "item 5", "start": "2014-01-01 05:00:00", "type": "point"},
  {"id": 6, "content": "item 6", "start": "2014-01-01 06:00:00"}
]
</textarea>

<div class="buttons">
  <input type="button" id="load" value="&darr; Load" title="Load data from textarea into the Timeline">
  <input type="button" id="save" value="&uarr; Save" title="Save data from the Timeline into the textarea">
</div>

<div id="visualization"></div>

<script>
  var txtData = document.getElementById('data');
  var btnLoad = document.getElementById('load');
  var btnSave = document.getElementById('save');

  // Create an empty DataSet.
  // This DataSet is used for two way data binding with the Timeline.
  var items = new vis.DataSet();

  // create a timeline
  var container = document.getElementById('visualization');
  var options = {
    editable: true
  };
  var timeline = new vis.Timeline(container, items, options);

  function loadData () {
    // get and deserialize the data
    var data = JSON.parse(txtData.value);

    // update the data in the DataSet
    //
    // Note: when retrieving updated data from a server instead of a complete
    // new set of data, one can simply update the existing data like:
    //
    //   items.update(data);
    //
    // Existing items will then be updated, and new items will be added.
    items.clear();
    items.add(data);

    // adjust the timeline window such that we see the loaded data
    timeline.fit();
  }
  btnLoad.onclick = loadData;

  function saveData() {
    // get the data from the DataSet
    //
    // Note that we specify the output type of the fields start and end
    // as "ISODate", which is safely serializable. Other serializable types
    // are "Number" (unix timestamp), "ASPDate" or "String" (without timezone!).
    //
    // Alternatively, it is possible to configure the DataSet to convert
    // the output automatically to ISODates like:
    //
    //   var options = {
    //     type: {start: 'ISODate', end: 'ISODate'}
    //   };
    //   var items = new vis.DataSet(options);
    //   // now items.get() will automatically convert start and end to ISO dates.
    //
    var data = items.get({
      type: {
        start: 'ISODate',
        end: 'ISODate'
      }
    });

    // serialize the data and put it in the textarea
    txtData.value = JSON.stringify(data, null, 2);
  }
  btnSave.onclick = saveData;

  // load the initial data
  loadData();
</script>
</body>
</html>