Skip to content

Commit ff1ee1b

Browse files
committed
Added weighting and writing weighted multiline adjlists.
1 parent e8286d6 commit ff1ee1b

File tree

4 files changed

+101
-9
lines changed

4 files changed

+101
-9
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.4.3",
3+
"version": "0.6.4",
44
"main": "index.html",
55
"window": {
66
"height": 1024,

src/creator.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
1+
//********************************************************************
2+
//Adding Weights and Labels
3+
//********************************************************************
4+
var selectedEdge,
5+
writingEdge = false;
6+
7+
function writeWeight(evt){
8+
//Enable writing weights for edges
9+
selectedEdge = evt.target;
10+
writingEdge = true;
11+
}
12+
13+
14+
function stopWriteWeight(evt){
15+
//Disable writing weights after the user takes his mouse off an edge
16+
writingEdge = false;
17+
}
18+
19+
$(document).on('keypress', function(key){
20+
/*
21+
Listen for when we press a key and change a edge's weight if
22+
we are hovering over it.
23+
24+
INPUT
25+
key: A jquery keyup event
26+
*/
27+
28+
if(!writingEdge) return;
29+
30+
//Enter only numbers
31+
if(key.keyCode >= 48 && key.keyCode <= 57){
32+
selectedEdge.style('label',
33+
selectedEdge.style('label') + String.fromCharCode(key.keyCode).toLowerCase());
34+
}
35+
});
36+
37+
$(document).on('keyup', function(key){
38+
if(!writingEdge) return;
39+
40+
if(key.keyCode === 8){
41+
//Check if our weight is empty
42+
if(selectedEdge.style('label').length === 0) return;
43+
44+
//Remove the last character
45+
selectedEdge.style('label', selectedEdge.style('label').slice(0, -1));
46+
}
47+
});
48+
149
var selectedNode,
250
writing = false;
351

@@ -61,7 +109,8 @@ function addEdge(evt){
61109
group: 'edges',
62110
data: {
63111
source: selected[0].id(),
64-
target: selected[1].id()
112+
target: selected[1].id(),
113+
weight: 1
65114
}
66115
});
67116

src/cytoInit.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function renderGraph(img){
1212
'line-color': '#3399ff',
1313
'opacity': 0.7,
1414
'curve-style': 'bezier',
15-
'control-point-step-size': '50px'
15+
'control-point-step-size': '50px',
16+
'label': ''
1617
}
1718
},
1819
{
@@ -45,5 +46,8 @@ function renderGraph(img){
4546

4647
cy.on('mouseover', 'node', writeLabel);
4748
cy.on('mouseout', 'node', stopWrite);
49+
50+
cy.on('mouseover', 'edge', writeWeight);
51+
cy.on('mouseout', 'edge', stopWriteWeight);
4852
}
4953

src/fileWrite.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ var fs = require('fs'),
77
//**************************************************************************
88
//WRITE GRAPHS
99
//**************************************************************************
10+
function checkWeighted(){
11+
/*
12+
Goes through each edge in the graph and checks if weight is defined.
13+
14+
OUTPUT
15+
weighted: Boolean value for whether any edges are weighted
16+
*/
17+
18+
var edges = cy.edges(),
19+
weighted = false;
20+
21+
//Check each edge for a weight
22+
for(var i=0; i<edges.length; i++){
23+
if(edges[i].style('label') !== ''){
24+
weighted = true;
25+
}
26+
}
27+
28+
return weighted;
29+
}
30+
1031
function makeAdjlist(nodeList){
1132
/*
1233
Writes a multiline adjacency list from a list of nodes, as used by networkX.
@@ -17,7 +38,7 @@ function makeAdjlist(nodeList){
1738
OUTPUT
1839
adjlist: A string of the multiline adjacency list
1940
*/
20-
41+
var weighted = checkWeighted();
2142
var adjlist = '';
2243

2344
//Go through each vertex and add it's edges to the adjlist
@@ -37,27 +58,44 @@ function makeAdjlist(nodeList){
3758
//Add each edge
3859
for(var k=0; k<numEdges; k++){
3960

61+
4062
//Check if the target is the node itself and use the source instead
4163
if(edges[k].data('target') === nodeList[i].data('id')){
4264
var targetLabel = cy.getElementById(edges[k].data('source')).style('label');
4365

4466
if(targetLabel !== ''){
45-
adjlist = adjlist + targetLabel + '\r\n';
67+
adjlist = adjlist + targetLabel;
4668
}
4769
else{
48-
adjlist = adjlist + edges[k].data('source') + '\r\n';
70+
adjlist = adjlist + edges[k].data('source');
4971
}
5072
}
5173
else{
5274
var targetLabel = cy.getElementById(edges[k].data('target')).style('label');
5375

5476
if(targetLabel !== ''){
55-
adjlist = adjlist + targetLabel + '\r\n';
77+
adjlist = adjlist + targetLabel;
5678
}
5779
else{
58-
adjlist = adjlist + edges[k].data('target') + '\r\n';
80+
adjlist = adjlist + edges[k].data('target');
5981
}
6082
}
83+
84+
//If we are weighted get a weight and write it
85+
if(weighted){
86+
var label = edges[k].style('label');
87+
88+
if(label !== ''){
89+
weight = label;
90+
}
91+
else{
92+
weight = '1';
93+
}
94+
95+
adjlist = adjlist + " {'weight': " + weight + '}';
96+
}
97+
98+
adjlist = adjlist + '\r\n';
6199
}
62100
}
63101

@@ -111,7 +149,8 @@ function makeMatrix(nodeList){
111149
matrix: A string with the first line being the dimensions of a matrix,
112150
and then a jagged array to represent the matrix.
113151
*/
114-
152+
153+
//Check if the user has specified any weights in the graph
115154
var matrix = nodeList.length.toString() + ',' + nodeList.length.toString()
116155
+ '\r\n';
117156
matrix = matrix + '[';

0 commit comments

Comments
 (0)