-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc_Graph.java
More file actions
284 lines (241 loc) · 7.2 KB
/
src_Graph.java
File metadata and controls
284 lines (241 loc) · 7.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.util.HashMap;
import java.util.Set;
import processing.core.PApplet;
import processing.core.PFont;
public class Graph {
private PApplet graphicsWindow;
private float xmin, xmax, ymin, ymax;
private float dataxmin, dataxmax, dataymin, dataymax;
private float dataxrange, datayrange;
private float yshiftval, yscaleval, xshiftval, xscaleval;
private boolean CONNECTED = true;
private boolean wrap = true;
private boolean cleardata = false;
private HashMap<Object, ScaledDataSet> dataSets;
private HashMap<Object, Integer> colorMap;
private static int[] defaultColors;
private int nextColor = 0;
public boolean drawLines = false;
public boolean drawPoints = true;
public int pointsize = 3;
public String xlabel;
public String ylabel;
public String title;
public int titlePointSize = 20;
public int labelPointSize = 16;
public int numXIncrements = 5;
public int numYIncrements = 5;
public int HSPACING = 20;
PFont font;
public Graph(PApplet p, float xmin, float ymin, float xmax, float ymax,
float dataxmin, float dataymin, float dataxmax, float dataymax) {
this.xmin = xmin;
this.ymin = ymin;
this.xmax = xmax;
this.ymax = ymax;
setDataRanges(dataxmin, dataxmax, dataymin, dataymax);
this.graphicsWindow = p;
this.dataSets = new HashMap<Object, ScaledDataSet>();
this.colorMap = new HashMap<Object, Integer>();
this.defaultColors = new int[3];
this.defaultColors[0] = p.color(255, 100, 100);
this.defaultColors[1] = p.color(100, 100, 255);
this.defaultColors[2] = p.color(100, 255, 100);
font = p.createFont("SansSerif", 20);
p.textFont(font);
}
public void setDataRanges(float xmin, float xmax, float ymin, float ymax) {
this.dataxmin = xmin;
this.dataxmax = xmax;
this.dataymin = ymin;
this.dataymax = ymax;
this.dataxrange = xmax - xmin;
this.datayrange = ymax - ymin;
this.xscaleval = (float) (xmax - xmin) / (float) (dataxmax - dataxmin);
this.xshiftval = xmin - xscaleval * dataxmin;
this.yscaleval = (ymax - ymin) / (dataymax - dataymin);
this.yshiftval = ymin - yscaleval * dataymin;
// this.xscaleval = (float) (dataxmax - dataxmin) / (float) (xmax - xmin);
// this.xshiftval = dataxmin - xscaleval * xmin;
// this.yscaleval = (dataymax - dataymin) / (ymax - ymin);
// this.yshiftval = dataymin - yscaleval * ymin;
}
public void setPosition(float xmin, float xmax, float ymin, float ymax) {
this.xmin = xmin;
this.xmax = xmax;
this.ymin = ymin;
this.ymax = ymax;
}
public void draw() {
int c;
ScaledDataSet d;
this.drawAxes();
drawTitle();
drawXAxisLabel();
drawYAxisLabel();
drawXIncrements();
drawYIncrements();
drawXIncrementLabels();
drawYIncrementLabels();
for (Object f : dataSets.keySet()) {
d = dataSets.get(f);
c = this.colorMap.get(f);
graphicsWindow.fill(c); // change the color
graphicsWindow.stroke(c); // change the color
for (int i = 1; i < d.getSize(); i++) {
if (drawLines) {
graphicsWindow.line(d.getx(i - 1), d.gety(i - 1), d.getx(i), d.gety(i));
}
if (drawPoints) {
graphicsWindow.ellipse(d.getx(i), d.gety(i), pointsize,pointsize);
}
}
}
graphicsWindow.stroke(0);
}
private void drawAxes() {
graphicsWindow.stroke(graphicsWindow.color(0));
graphicsWindow.line(xmin, ymin, xmin, ymax);
graphicsWindow.line(xmin, ymin, xmax, ymin);
}
private void drawTitle() {
if ((title != null) && (!title.equals(""))) {
graphicsWindow.fill(0);
graphicsWindow.textSize(titlePointSize);
graphicsWindow.textAlign(graphicsWindow.LEFT);
graphicsWindow.text(title, xmin, ymax - 10);
}
}
private void drawYAxisLabel() {
if ((ylabel != null) && (!ylabel.equals(""))) {
graphicsWindow.fill(0);
graphicsWindow.textSize(labelPointSize);
graphicsWindow.textAlign(graphicsWindow.RIGHT);
graphicsWindow.text(ylabel, xmin - HSPACING, (ymin + ymax) / 2);
}
}
private void drawXAxisLabel() {
if ((xlabel != null) && (!xlabel.equals(""))) {
graphicsWindow.fill(0);
graphicsWindow.textSize(labelPointSize);
graphicsWindow.textAlign(graphicsWindow.CENTER);
graphicsWindow.text(xlabel, (xmin + xmax) / 2, ymin + 22);
}
}
private void drawXIncrements() {
float dxinc = (xmax - xmin) / numXIncrements;
for (float i = xmin; i < xmax; i += dxinc) {
graphicsWindow.stroke(100);
graphicsWindow.line(i, ymin, i, ymin + 4);
}
}
private void drawXIncrementLabels() {
float dxinc = (xmax - xmin) / numXIncrements;
float dataxinc = (dataxmax - dataxmin) / numXIncrements;
String text;
float c = dataxmin;
for (float i = xmin; i < xmax; i += dxinc) {
graphicsWindow.fill(0);
graphicsWindow.textSize(10);
graphicsWindow.textAlign(graphicsWindow.CENTER);
text = Float.toString(c);
graphicsWindow.text(text, i, ymin + 10);
c += dataxinc;
}
}
private void drawYIncrementLabels() {
float dyinc = (ymin - ymax) / numYIncrements;
float datayinc = (dataymax - dataymin) / numYIncrements;
String text;
float c = dataymax;
for (float i = ymax; i < ymin; i += dyinc) {
graphicsWindow.fill(0);
graphicsWindow.textSize(10);
graphicsWindow.textAlign(graphicsWindow.CENTER);
text = Float.toString(c);
graphicsWindow.text(text, xmin - HSPACING, i);
c -= datayinc;
}
}
private void drawYIncrements() {
float dyinc = (ymin - ymax) / numYIncrements;
for (float i = ymax; i < ymin; i += dyinc) {
graphicsWindow.stroke(100);
graphicsWindow.line(xmin, i, xmin - HSPACING / 2, i);
}
}
public void plotPoint(float x, float y, Object key) {
if ((x > dataxmax) && (!wrap)) {
return;
}
if (x < dataxmin) {
return;
}
if (y < dataymin) {
return;
}
if (y > dataymax) {
return;
}
if ((x > dataxmax) && (wrap)) {
// if we're wrapping, re-adjust data range
cleardata = true;
float datarange = dataxmax - dataxmin;
while (x > dataxmax) {
dataxmin += datarange;
dataxmax += datarange;
}
}
if (cleardata) {
clearData();
}
if (dataSets.containsKey(key)) {
ScaledDataSet d = dataSets.get(key);
d.addPoint(x, y);
} else {
ScaledDataSet d = new ScaledDataSet();
d.setxScaling(dataxmin, xmin, dataxmax, xmax);
d.setyScaling(dataymin, ymin, dataymax, ymax);
d.addPoint(x, y);
dataSets.put(key, d);
if (!colorMap.containsKey(key)) {
colorMap.put(key, this.getNextColor());
}
}
}
private void clearData() {
ScaledDataSet d;
Set s = dataSets.keySet();
dataSets.clear();
for (Object f : s) {
d = new ScaledDataSet();
d.setxScaling(dataxmin, xmin, dataxmax, xmax);
d.setyScaling(dataymin, ymin, dataymax, ymax);
dataSets.put(f, d);
}
cleardata = false;
}
private int getNextColor() {
int i = defaultColors[nextColor];
nextColor = (nextColor + 1) % defaultColors.length;
return i;
}
public float xDataToXCoordinate(float xdata) {
return xdata * xscaleval + this.xshiftval;
}
public float xCoordinateToXData(float xcoord) {
return (xcoord - this.xshiftval) / this.xscaleval;
}
public float yDataToYCoordinate(float ydata) {
return ydata * yscaleval + this.yshiftval;
}
public float yCoordinateToYData(float ycoord) {
return (ycoord - this.yshiftval) / this.yscaleval;
}
public void setColor(Class organismClass, Integer color) {
colorMap.put(organismClass, color);
}
public void clear() {
clearData();
}
}