Skip to content

Commit 217a952

Browse files
committed
All file output functions now integrated with cytoscape.
1 parent 348bb5e commit 217a952

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/fileWrite.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ function writeGraphAdjlist(){
7979
});
8080
}
8181

82+
function getIndexOf(id){
83+
/*
84+
Given the id of a node, finds it's index in cy.nodes().
85+
86+
INPUT
87+
id: The id of the node in question.
88+
89+
OUTPUT
90+
index: The index of the node in question
91+
*/
92+
var nodes = cy.nodes();
93+
94+
for(var i=0; i < nodes.length; i++){
95+
if(nodes[i].id() === id){
96+
return i;
97+
}
98+
}
99+
}
100+
82101
function makeMatrix(nodeList){
83102
/*
84103
Given a list of nodes in a graph, creates an adjacency matrix.
@@ -98,16 +117,20 @@ function makeMatrix(nodeList){
98117
matrix = matrix + '[';
99118

100119
for(var i=0; i<nodeList.length; i++){
101-
console.log('here');
102-
var edges = nodes[i].adjacencies;
120+
var edges = nodeList[i].connectedEdges();
103121

104122
//Define our row and fill it with zeros
105123
var matrixRow = new Array(nodeList.length);
106124
matrixRow.fill(0);
107125

108126
//Increment every index which has an edge by one
109127
for(var j=0; j<edges.length; j++){
110-
matrixRow[edges[j].index]++;
128+
if(edges[j].data('target') === nodeList[i].id()){
129+
matrixRow[getIndexOf(edges[j].data('source'))]++;
130+
}
131+
else{
132+
matrixRow[getIndexOf(edges[j].data('target'))]++;
133+
}
111134
}
112135

113136
matrix = matrix + '[' + matrixRow.toString() + ']' + ',\r\n';
@@ -130,7 +153,7 @@ function writeGraphMatrix(){
130153

131154
dialog.setContext(document);
132155
dialog.saveFileDialog(function(fileName){
133-
var matrixFile = makeMatrix(nodes);
156+
var matrixFile = makeMatrix(cy.nodes());
134157

135158
fs.writeFile(fileName, matrixFile, function(err){
136159
if(err) throw err;
@@ -157,11 +180,15 @@ function makePositionsString(nodeList){
157180
var posJSON = '{';
158181

159182
for(var i=0; i<nodeList.length; i++){
160-
posJSON = posJSON + '"' + i.toString() + '": ';
161-
183+
if(nodeList[i].style('label') !== ''){
184+
posJSON = posJSON + '"' + nodeList[i].style('label') + '": ';
185+
}
186+
else{
187+
posJSON = posJSON + '"' + nodeList[i].data('id') + '": ';
188+
}
162189
//Scale the coordinates
163-
var relX = nodeList[i].xPos / map.width,
164-
relY = nodeList[i].yPos / map.height;
190+
var relX = nodeList[i].position().x / map.width,
191+
relY = nodeList[i].position().y / map.height;
165192

166193
//Make sure the last entry doesn't have a comma
167194
if(i < nodeList.length - 1){
@@ -185,7 +212,7 @@ function writePositions(){
185212

186213
dialog.setContext(document);
187214
dialog.saveFileDialog(function(fileName){
188-
var positions = makePositionsString(nodes);
215+
var positions = makePositionsString(cy.nodes());
189216

190217
fs.writeFile(fileName, positions, function(err){
191218
if(err) throw err;

0 commit comments

Comments
 (0)