summaryrefslogtreecommitdiff
path: root/www/lib/vis/examples/network/other/saveAndLoad.html
blob: 08165c85463c6cc048fd64d3e3b8bf4cbb909fe5 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!doctype html>
<html>
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <title>Network | Saving and loading networks</title>

        <style type="text/css">
            body {
              font: 10pt sans;
            }
            #network {
                float:left;
                width: 600px;
                height: 600px;
                margin:5px;
                border: 1px solid lightgray;
            }
            #config {
                float:left;
                width: 400px;
                height: 600px;
            }
            #input_output {
                height: 10%;
                width: 15%;
            }

            p {
                font-size:16px;
                max-width:700px;
            }
        </style>

        <script type="text/javascript" src="../exampleUtil.js"></script>
        <script type="text/javascript" src="../../../dist/vis.js"></script>
        <link href="../../../dist/vis.css" rel="stylesheet" type="text/css" />

        <script src="../../googleAnalytics.js"></script>
    </head>

    <body>
        <p>
            In this example, the network data can be exported to JSON and imported back into the network.

            Try this out by exporting the network to JSON, clearing the network and then importing it again. The nodes will all appear in the same position as they were before the network was destroyed.
        </p>

        <div id="network"></div>

        <div>
            <textarea id=input_output></textarea>
            <input type="button" id="import_button" onclick="importNetwork()" value="import"></input>
            <input type="button" id="export_button" onclick="exportNetwork()" value="export"></input>
            <input type="button" id="destroy_button" onclick="destroyNetwork()" value="destroy"></input>
        </div>

        <script type="text/javascript">
            var network;
            var container;
            var exportArea;
            var importButton;
            var exportButton;

            function init() {
                container = document.getElementById('network');
                exportArea = document.getElementById('input_output');
                importButton = document.getElementById('import_button');
                exportButton = document.getElementById('export_button');

                draw();
            }

            function addContextualInformation(elem, index, array) {
                addId(elem, index);
                addConnections(elem, index);
            }

            function addId(elem, index) {
                elem.id = index;
            }

            function addConnections(elem, index) {
                // need to replace this with a tree of the network, then get child direct children of the element
                elem.connections = network.getConnectedNodes(index);
            }

            function destroyNetwork() {
                network.destroy();
            }

            function clearOutputArea() {
                exportArea.value = "";
            }

            function draw() {
                // create a network of nodes
                var data = getScaleFreeNetwork(5);

                network = new vis.Network(container, data, {manipulation:{enabled:true}});

                clearOutputArea();
            }

            function exportNetwork() {
                clearOutputArea();

                var nodes = objectToArray(network.getPositions());

                nodes.forEach(addContextualInformation);

                // pretty print node data
                var exportValue = JSON.stringify(nodes, undefined, 2);

                exportArea.value = exportValue;

                resizeExportArea();
            }

            function importNetwork() {
                var inputValue = exportArea.value;
                var inputData = JSON.parse(inputValue);

                var data = {
                    nodes: getNodeData(inputData),
                    edges: getEdgeData(inputData)
                }

                network = new vis.Network(container, data, {});

                resizeExportArea();
            }

            function getNodeData(data) {
                var networkNodes = [];

                data.forEach(function(elem, index, array) {
                    networkNodes.push({id: elem.id, label: elem.id, x: elem.x, y: elem.y});
                });

                return new vis.DataSet(networkNodes);
            }

            function getEdgeData(data) {
                var networkEdges = [];

                data.forEach(function(node, index, array) {
                    // add the connection
                    node.connections.forEach(function(connId, cIndex, conns) {
                        networkEdges.push({from: node.id, to: connId});

                        var elementConnections = array[connId].connections;

                        // remove the connection from the other node to prevent duplicate connections
                        var duplicateIndex = elementConnections.findIndex(function(connection) {
                            connection === node.id;
                        });

                        elementConnections = elementConnections.splice(0, duplicateIndex - 1).concat(elementConnections.splice(duplicateIndex + 1, elementConnections.length))
                        });
                });

                return new vis.DataSet(networkEdges);
            }

            function objectToArray(obj) {
                return Object.keys(obj).map(function (key) { return obj[key]; });
            }

            function resizeExportArea() {
                exportArea.style.height = (1 + exportArea.scrollHeight) + "px";
            }

            init();
        </script>
    </body>
</html>