This repository was archived by the owner on Oct 15, 2020. It is now read-only.
forked from leebyron/streamgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiaryDataSource.java
More file actions
126 lines (96 loc) · 3.62 KB
/
DiaryDataSource.java
File metadata and controls
126 lines (96 loc) · 3.62 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
import java.util.*;
import java.lang.String;
/**
* BelievableDataSource
* Create test data for layout engine.
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public class DiaryDataSource implements DataSource {
public Random rnd;
public String rawData[];
public DiaryDataSource(String lines[]) {
rnd = new Random(2);
rawData = lines;
}
public Layer[] make() {
// count the number of lines ( -1 for the legend on the first line)
int numLayers = rawData.length - 1;
// count the items in each line
String [] chars = rawData[0].split(",");
// -2 for the timestamp and name tuimes the number of hours in the day
int sizeArrayLength = (chars.length - 2) * 24;
Layer[] layers = new Layer[numLayers];
for (int i = 0; i < numLayers; i++) {
String currentLine = rawData[i + 1];
// Find the name, skipping the timestamp
int nameStart = currentLine.indexOf(',');
int nameEnd = currentLine.indexOf(',', nameStart + 1);
String name = currentLine.subSequence(nameStart + 1, nameEnd).toString();
// Parse the entries for each day
String entries = currentLine.subSequence(nameEnd + 1, currentLine.length()).toString();
float[] data = processEntries(entries, sizeArrayLength);
// Create the layer
layers[i] = new Layer(name, data);
}
return layers;
}
protected float[] processEntries(String rawDays, int n) {
float[] x = new float[n];
// zero out all the hours to start with
for (int i=0; i<n; i++) {
x[i] = 0;
}
// see which hours we need to set
final int hoursInADay = 24;
final int days = n / hoursInADay;
final int initialHeight = 60;
final float downScale = 2.45f;
final int noiseIterations = 10;
String dayData = rawDays;
for (int i=0; i < days; i++) {
final int dayStart = dayData.indexOf('"') + 1;
final int dayEnd = dayData.indexOf('"', dayStart + 1);
if (dayStart < dayEnd) {
String hourData = dayData.subSequence(dayStart, dayEnd).toString();
while (hourData != "")
{
// Find the current hour
final int hoursEnd = hourData.indexOf(':');
String currentData = hourData.subSequence(0, hoursEnd).toString();
final int hourToSet = Integer.parseInt(currentData);
final int arrayPosition = (hoursInADay * i) + hourToSet;
x[arrayPosition] = initialHeight;
System.out.printf("%d\n", arrayPosition);
// Move to the next hour
final int endOfString = hourData.indexOf(',');
if (endOfString == -1) {
hourData = "";
}
else {
hourData = hourData.subSequence(endOfString + 2, hourData.length()).toString();
}
}
}
// Skip to the next day
dayData = dayData.subSequence(dayEnd + 1, dayData.length()).toString();
}
// Now ramp the data up to make it more interesting
float scaleHeight = initialHeight / downScale;
for (int i = 0; i < noiseIterations; i++) {
addNoise(x, scaleHeight);
scaleHeight = scaleHeight / downScale;
}
return x;
}
protected void addNoise(float[] hours, float height) {
int count = (int)(rnd.nextFloat() * (hours.length));
for (int i = 0; i < count; i++) {
int index = (int)(rnd.nextFloat() * hours.length);
if (hours[index] != 0) {
hours[index] += (rnd.nextFloat() * height * 2) - height;
}
}
}
}