-
Notifications
You must be signed in to change notification settings - Fork 22
/
data.js
241 lines (209 loc) · 7.19 KB
/
data.js
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
;(function (exports) {
exports.gg.sampleData = {};
// Random upward trending data
gg.sampleData.upward = (function () {
var x = 0;
var y = 0;
return _.map(_.range(20), function () {
x += Math.random() * 30;
y += 20 - Math.random() * 30;
return { d: x, r: y };
});
}());
// Random upward trending paired data
gg.sampleData.upwardPairs = (function () {
var x = 0;
var y0 = 0;
var y1 = 0;
return _.map(_.range(20), function () {
x += Math.random() * 30;
y0 += 20 - Math.random() * 30;
y1 = y0 + Math.random() * 10;
return { a: x, top: y0, bottom: y1 };
});
}());
// Data above and below zero.
gg.sampleData.toBeCentered = (function () {
var x = 0;
var y = 0;
return _.map(_.range(20), function () {
x += Math.round(Math.random() * 30);
if (Math.random() > .5) {
y = (Math.round(Math.random() * 30));
} else {
y = -1 * (Math.round(Math.random() * 20))
}
return { d: x, r: y };
});
}());
// Random upward trending data for four subjects
gg.sampleData.upwardSubjects = (function () {
var subjects = ['a', 'b', 'c', 'd'];
var x = 0;
var y = 0;
return _.flatten(_.map(_.range(20), function (i) {
x += Math.round(Math.random() * 30);
y += Math.round(Math.abs(20 - Math.random() * 30));
return _.map(subjects, function(subject, i) {
var skew = i + 1;
return { d: x, r: y * (Math.random() * skew), subject: subject };
})
}));
}());
// Random purchases by four different groups
gg.sampleData.purchases = (function () {
var names = [ 'foo', 'bar', 'baz', 'quux' ];
var randomMeans = d3.random.normal(200, 10);
var groups = _.map(names, function (n) {
var mean = randomMeans();
return {
name: n,
rng: d3.random.normal(mean, mean/3)
};
});
return _.map(_.range(20), function () {
var g = groups[Math.floor(Math.random() * groups.length)];
return {
who: g.name,
purchases: Math.round(Math.max(0, g.rng()))
};
});
}());
// Random data for plotting semi-log.
gg.sampleData.semiLogData = (function () {
var data = [];
var x = 0;
var y = 1;
_.times(20, function () {
x += Math.random() * 30;
y *= Math.random() * 5;
data.push({
d: x,
r: y
});
});
return data;
}());
// Random height weight data for binned histogram and scatter plot
gg.sampleData.heightWeight = (function () {
// Some random variables.
var randomHeight = d3.random.normal(66, 18);
var randomBMI = d3.random.normal(21.75, 3);
var standardNormal = d3.random.normal();
return _.map(_.range(20000), function () {
var inches = randomHeight();
var lbs = randomBMI() * inches * inches / 703.06958;
return {
height: inches,
weight: lbs
};
});
}());
// Random data for box plot graphic
gg.sampleData.forBoxPlots = (function () {
var names = ['a', 'b', 'c', 'd' ];
var randomMeans = d3.random.normal(500, 100);
var randomStddevs = d3.random.normal(150, 20);
var outlierRates = d3.random.normal(0.01, 0.001);
function makeRNG (mean, stddev, outlierRate) {
var baseRNG = d3.random.normal(mean, stddev);
return function () {
var r = baseRNG();
var sign = Math.abs(r - mean) / (r - mean);
return (Math.random() < outlierRate)
? r + (sign * stddev * (3 + Math.random() * 2)) : r;
};
}
var groups = _.map(names, function (n) {
return {
name: n,
rng: makeRNG(randomMeans(), Math.abs(randomStddevs()), Math.abs(outlierRates()))
};
});
return _.map(_.range(2000), function () {
var g = groups[Math.floor(Math.random() * groups.length)];
return { grade: g.name, score: g.rng() };
});
}());
gg.sampleData.twoPopulations = (function () {
var groups = [
{
name: 'zorks',
rng1: d3.random.normal(100, 15),
rng2: d3.random.normal(0, 0.1)
},
{
name: 'florgs',
rng1: d3.random.normal(90, 20),
rng2: d3.random.normal(0, 0.2)
}
];
return _.map(_.range(2000), function (i) {
var g = groups[i % groups.length];
var intelligence = g.rng1();
var wisdom = (1 + g.rng2()) * intelligence;
return {
group: g.name,
intelligence: intelligence,
wisdom: wisdom
};
});
}());
gg.sampleData.quadrants = (function () {
var randomX = d3.random.normal(500, 100);
var randomY = d3.random.normal(200, 50);
var randomSize = d3.random.normal(10, 2);
var randomId = d3.random.normal(1000, 100);
return _.map(_.range(50), function () {
return {
x: randomX(),
y: randomY(),
size: Math.max(0, randomSize()) * 100,
name: "Patient #" + Math.round(randomId())
};
});
}());
gg.sampleData.facets = (function () {
function choice (list) {
return function () { return list[Math.floor(Math.random() * list.length)]; }
}
var gender = choice(['Male', 'Female']);
var job = choice(['Programmer', 'Manager', 'Designer']);
var pay = {
Male: {
Programmer: d3.random.normal(30, 5),
Manager: d3.random.normal(50, 10),
Designer: d3.random.normal(20, 5)
},
Female: {
Programmer: d3.random.normal(25, 4),
Manager: d3.random.normal(40, 12),
Designer: d3.random.normal(20, 6)
}
}
var experience = {
Male: {
Programmer: d3.random.normal(10, 3),
Manager: d3.random.normal(15, 5),
Designer: d3.random.normal(5, 2)
},
Female: {
Programmer: d3.random.normal(7, 4),
Manager: d3.random.normal(10, 4),
Designer: d3.random.normal(6, 3)
}
}
return _.map(_.range(1000), function () {
var g = gender();
var j = job();
var e = Math.max(1, experience[g][j]());
var p = Math.max(5, pay[g][j]()) * e;
return {
gender: g,
job: j,
pay: p,
experience: e
}
});
}());
})(window);