@@ -226,8 +226,8 @@ function makePositionsString(nodeList){
226
226
posJSON = posJSON + '"' + nodeList [ i ] . data ( 'id' ) + '": ' ;
227
227
}
228
228
//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 ;
231
231
232
232
//Make sure the last entry doesn't have a comma
233
233
if ( i < nodeList . length - 1 ) {
@@ -258,6 +258,150 @@ function writePositions(){
258
258
} ) ;
259
259
} ) ;
260
260
}
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
+ }
261
405
262
406
//****************************************************************************
263
407
//OPEN BACKGROUND IMAGE
0 commit comments