-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSigma.java
More file actions
172 lines (139 loc) · 3.73 KB
/
Sigma.java
File metadata and controls
172 lines (139 loc) · 3.73 KB
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
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Collectors;
import java.util.Random;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Sigma<T>{
int width, height;
double[][] points;
final Random rnd = new Random();
boolean randomCoords = false;
static final String jsFileName = "data";
final HashMap<T, List<T>> graph;
final List<T> nodes;
public Sigma(HashMap<T, List<T>> graph){
this.graph = graph;
this.nodes = new ArrayList<>(graph.keySet());
computeScreenSize();
computeCoords();
}
void computeScreenSize(){
points = new double[nodes.size()][2];
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
width = (int)screenSize.getWidth()/8;
height = (int)screenSize.getHeight()/8;
}
void computeCoords(){
int[] center = {width/2, height/2};
double x = center[0], y = height,
θ = 360 / (double)nodes.size(), r = height/2;
for(int i = 0; i < nodes.size(); ++i){
double scale = .8+
(((double)graph.get(nodes.get(i)).size()/(double)nodes.size()));
if(randomCoords){
points[i][0] = rnd.nextDouble();
points[i][1] = rnd.nextDouble();
}else{
points[i][0] = scale*center[0] + (r*Math.cos((i+1)*θ));
points[i][1] = scale*center[1] + (r*Math.sin((i+1)*θ));
}
x = points[i][0];
y = points[i][1];
}
}
String jsonGraph(){
return
"{\n "+
"\"nodes\": ["+
nodes
.stream()
.map(this::jsonNode)
.collect(Collectors.joining(","))+
"\n ],\n "+
"\"edges\": ["+
nodes
.stream()
.map(this::jsonEdges)
.filter(edges -> !edges.isEmpty())
.collect(Collectors.joining(","))+
"\n ]"+
"\n}";
}
String jsonNode(T u){
return
"\n {\n"
+ " \"id\": \""+ u +"\",\n"
+ " \"label\": \""+u+"("+ graph.get(u).size()+")"+"\",\n"
+ " \"x\": "+ point(u)[0] +",\n"
+ " \"y\": "+ point(u)[1] +",\n"
+ " \"color\": \""+ color(u) +"\",\n"
+ " \"size\": "+ pointSize(u) +"\n"
+ " }";
}
String jsonEdge(T u, T v){
return
"\n {\n"
+ " \"id\": \""+ u +"-"+ v +"\",\n"
+ " \"source\": \""+ u +"\",\n"
+ " \"target\": \""+ v +"\",\n"
+ " \"type\": \"arrow\"\n"
+ " }";
}
String jsonEdges(T u){
return graph
.get(u)
.stream()
.map(v -> jsonEdge(u,v))
.collect(Collectors.joining(","));
}
final String[] colors = {
"#8fbc8f","#7fffd4","#ffd700", "#d2691e","#6495ed",
"#00008b", "#006400","#483d8b","#2f4f4f","#8b0000"
};
String color(T node){
double tmp = ((double)graph.get(node).size()
/((double)nodes.size()-1))*100;
int color = (int) Math.floor(tmp/9);
color = color > colors.length-1 ? colors.length-1 : color;
return colors[color];
}
double[] point(T node){
int i = nodes.indexOf(node);
return points[i];
}
double pointSize(T node){
int maxSize = 20;
return ((graph.get(node).size()
/((double)nodes.size()))
*maxSize)
+(maxSize/2);
}
public void writeJSFile(String path, String filename){
String jsonG = jsonGraph();
if(path == null || path.isEmpty())
path = "./";
if(filename == null || filename.isEmpty())
filename = jsFileName;
try {
File file = new File(path + filename + ".js");
file.createNewFile();
BufferedWriter br = new BufferedWriter(new FileWriter(file));
br.write(filename + " =\n" + jsonG + ";\n");
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void randomCoords(boolean value){
randomCoords = value;
computeCoords();
}
}