Skip to content

Commit 2488995

Browse files
committed
Added basic graph opening functionality.
1 parent ff1ee1b commit 2488995

File tree

4 files changed

+159
-6
lines changed

4 files changed

+159
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graph-creator",
3-
"version": "0.6.4",
3+
"version": "0.7.4",
44
"main": "index.html",
55
"window": {
66
"height": 1024,

src/creator.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ function addEdge(evt){
109109
group: 'edges',
110110
data: {
111111
source: selected[0].id(),
112-
target: selected[1].id(),
113-
weight: 1
112+
target: selected[1].id()
114113
}
115114
});
116115

src/fileWrite.js

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ function makePositionsString(nodeList){
226226
posJSON = posJSON + '"' + nodeList[i].data('id') + '": ';
227227
}
228228
//Scale the coordinates
229-
var relX = nodeList[i].position().x / map.width,
230-
relY = nodeList[i].position().y / map.height;
229+
var relX = nodeList[i].position().x / 1080,
230+
relY = nodeList[i].position().y / 720;
231231

232232
//Make sure the last entry doesn't have a comma
233233
if(i < nodeList.length - 1){
@@ -258,6 +258,150 @@ function writePositions(){
258258
});
259259
});
260260
}
261+
//**************************************************************************
262+
//OPEN POSITIONS
263+
//**************************************************************************
264+
function parsePositions(posData){
265+
/*
266+
Given a json positions file, reads it and applies the positions to
267+
the nodes in the graph.
268+
269+
INPUT
270+
posData: The positions of the nodes in the graph
271+
*/
272+
273+
var positions = JSON.parse(posData);
274+
275+
//Go through each nodes position and assign it
276+
for(key in positions){
277+
cy.getElementById(key).position('x', 1080 * positions[key][0]);
278+
cy.getElementById(key).position('y', 720 * positions[key][1]);
279+
}
280+
}
281+
282+
283+
function openPositions(){
284+
/*
285+
Opens a JSON form positions file and loads it into cytoscape.
286+
*/
287+
288+
dialog.setContext(document);
289+
290+
//Open a multiline adjlist and add the nodes
291+
dialog.openFileDialog(function(filePath){
292+
fs.readFile(filePath, function (err, data){
293+
if(err) throw err;
294+
295+
parsePositions(data.toString());
296+
});
297+
});
298+
}
299+
300+
//***************************************************************************
301+
//OPEN GRAPHS
302+
//***************************************************************************
303+
304+
function parseGraph(graph){
305+
/*
306+
Given input of a multiline adjlist as a string,
307+
parses it and creates the cytoscape graph.
308+
309+
INPUT
310+
graph: A string of a multiline adjlist
311+
*/
312+
313+
//Remove any existing graph
314+
cy.elements().remove();
315+
316+
var lines = graph.split('\r\n');
317+
if(lines.length === 0) throw new Error('Could not open graph: no data');
318+
319+
var currentLine = 0;
320+
321+
//Read each of the adjacencies for a source node and add them
322+
while(currentLine < lines.length - 1){
323+
324+
//Read the first node and add it
325+
var sourceNode = lines[currentLine].split(' ')[0];
326+
var degree = parseInt(lines[currentLine].split(' ')[1]);
327+
328+
//If the source node already exists this will make an error
329+
try{
330+
cy.add({
331+
group: 'nodes',
332+
data: {
333+
id: sourceNode
334+
}
335+
});
336+
}catch(err){}
337+
338+
currentLine++;
339+
340+
edges = lines.slice(currentLine, currentLine + degree);
341+
342+
//Add each edge if it doesn't exist already
343+
for(var i=0; i<edges.length; i++){
344+
//Split up the edge destination and properties
345+
var sepString = edges[i].split('{');
346+
347+
//Get the target node and remove the extra space if there was a bracket
348+
if(sepString[0].length === 2){
349+
var targetNode = sepString[0].slice(0, -1);
350+
}
351+
else{
352+
var targetNode = sepString[0];
353+
}
354+
355+
356+
var weight = '';
357+
//Get the properties properties
358+
if(sepString.length === 2){
359+
weight = JSON.parse('{' + sepString[1]).weight.toString();
360+
}
361+
362+
//Add the target node in case it doesn't exist
363+
try{
364+
cy.add({
365+
group: 'nodes',
366+
data: {
367+
id: targetNode
368+
}
369+
});
370+
}catch(err){}
371+
372+
//Add the edge
373+
cy.add({
374+
group: 'edges',
375+
data: {
376+
source: sourceNode,
377+
target: targetNode
378+
},
379+
style: {
380+
label: weight
381+
}
382+
});
383+
}
384+
385+
currentLine+= degree;
386+
}
387+
}
388+
389+
function openGraph(){
390+
/*
391+
Opens a multiline adjlist form graph file and loads it into cytoscape.
392+
*/
393+
394+
dialog.setContext(document);
395+
396+
//Open a multiline adjlist and add the nodes
397+
dialog.openFileDialog(function(filePath){
398+
fs.readFile(filePath, function (err, data){
399+
if(err) throw err;
400+
401+
parseGraph(data.toString());
402+
});
403+
});
404+
}
261405

262406
//****************************************************************************
263407
//OPEN BACKGROUND IMAGE

src/menu.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ var menu = new nw.Menu({type: 'menubar'});
33

44
var fileSubmenu = new nw.Menu();
55
fileSubmenu.append(new nw.MenuItem({
6-
label: 'Open',
6+
label: 'Load Background',
77
click: openImage
88
}));
99

10+
fileSubmenu.append(new nw.MenuItem({
11+
label: 'Load Graph',
12+
click: openGraph
13+
}));
14+
15+
fileSubmenu.append(new nw.MenuItem({
16+
label: 'Load Positions',
17+
click: openPositions
18+
}));
19+
1020
menu.append(new nw.MenuItem({
1121
label: 'File',
1222
submenu: fileSubmenu

0 commit comments

Comments
 (0)