This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
charts.js
441 lines (401 loc) · 13.2 KB
/
charts.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
'use strict';
(function (window) {
var microp = window.microp;
var Charts = function (auctions) {
this.auctions = auctions;
this.textRotation = 60;
this.generate();
}
Charts.prototype = {
scale: d3.scale.sqrt(),
linear: d3.scale.linear(),
load: {
chart2: function chart2 (settings) {
c3.generate({
bindto: '#chart2',
data: {
xs: {
bids: 'bids_dates',
means: 'means_dates'
},
columns: settings.cols,
types: {
bids: 'scatter'
},
names: {
bids: 'Winning Bid',
means: 'Mean Winning Bid By Date'
}
},
axis: {
x: {
type: 'timeseries',
tick: {
multiline: false,
fit: true,
rotate: Charts.textRotation
},
label: {
text: 'Auction date',
position: 'outer-center'
}
},
y: {
label: {
text: 'Bid amount',
position: 'outer-middle'
},
tick: {
format: function(d) {
return microp.format.commaSeparatedDollars(d);
}
}
}
},
point: {
r: 7
},
tooltip: {
show: true,
format: {
value: function (value, ratio, id, index) {
return value === null ? 'no bid' : microp.format.commaSeparatedDollars(value);
}
}
}
});
},
chart4: function chart4 (settings) {
var today = new Date();
var twoWeeksAgo = new Date(today - (1000 * 60 * 60 * 24 * 14)); // 14 days ago
var dateExtent = [twoWeeksAgo,today];
c3.generate({
bindto: '#chart4',
axis: {
x: {
type: 'timeseries',
label: {
text: 'Date',
position: 'outer-center'
},
tick: {
rotate: Charts.textRotation,
multiline: false
},
'extent': dateExtent
},
y: {
tick: {
count: 4,
format: function (d) { return Math.round(d); }
},
label: {
text: 'Count',
position: 'outer-middle'
}
}
},
data: {
x: settings.x,
columns: settings.cols,
type: 'bar'
},
bar: {
width: {
ratio: 0.3 // this makes bar width 50% of length between ticks
}
},
color: {
pattern: ['#046B99','#B3EFFF','#1C304A','#00CFFF']
},
zoom: {
enabled: false
},
subchart: {
show: true,
size: {
height: 30
}
}
});
},
chart5: function chart5 (settings) {
c3.generate({
bindto: '#chart5',
axis: {
x: {
type: 'timeseries',
label: {
text: 'Auction date',
position: 'outer-center'
},
tick: {
rotate: Charts.textRotation,
multiline: false
}
},
y: {
label: {
text: '# Bids',
position: 'outer-middle'
}
}
},
data: {
xs: settings.xs,
columns: settings.cols,
type: 'scatter'
},
tooltip: {
contents: function(d) {
return '<table class="c3-tooltip">' +
'<tbody>' +
'<tr>' +
'<th colspan="1">' + microp.format.date(d[0].x, '/') + '</th>' +
'<th class="name" colspan="1"><span style="background-color:#1C304A"></span>' +
d[0].id + '</th>' +
'</tr>' +
'<tr class="c3-tooltip-name--'+ d[0].id + '">' +
'<td class="name">Bids</td>' +
'<td class="value">'+ d[0].value + '</td>' +
'</tr>' +
'<tr class="c3-tooltip-name--' + d[0].id + '">' +
'<td class="name">Winning bid</td>' +
'<td class="value">$' + settings.z[d[0].id] + '</td>' +
'</tr>' +
'</tbody>' +
'</table>';
}
},
legend: {
show: false
},
color: {
pattern: ['#1C304A']
},
point: {
r: function(d) {
if ( _.isUndefined(settings.z[d.id]) ) { return; }
var scaledValue = Charts.prototype.linear(Charts.prototype.scale(settings.z[d.id]));
return scaledValue <= 3 ? 3 : scaledValue;
}
}
});
},
donut1: function donut1 (settings) {
c3.generate({
bindto: "#donut-by-repo",
data: {
columns: settings.cols ? settings.cols : [
['software', 0],
['non-software', 120]
],
type : 'donut'
},
donut: {
title: 'Projects by repo'
},
color: {
pattern: ['#1C304A','#00CFFF','#046B99','#B3EFFF']
}
});
},
donut2: function donut2 (settings) {
c3.generate({
bindto: "#donut-by-language",
data: {
columns: settings.cols ? settings.cols : [
['software', 0],
['non-software', 120]
],
type : 'donut'
},
donut: {
title: 'Projects by language'
},
color: {
pattern: ['#046B99','#B3EFFF','#1C304A','#00CFFF']
}
});
}
},
create: {
// Creates a scatterplot with a connecting sparkline
// Outputs 4 columns: [['bids_dates'], ['bids'], ['means_dates'],['means']]
// bids_dates: a list of dates that an auction occurred. There may be multiple
// of the same date if multiple auctions occurred on that day.
// bids: a list of corresponding winning bids. The index corresponds to
// the date with the same index in `bids_dates`.
// means_dates: a list of dates for the mean winning bid. There should only be
// one of each date.
// means: a list of mean winning bids. Matches a date of the same index
// in `means_dates`.
chart2: function chart2 (auctions) {
var settings = {};
settings.cols = [['bids_dates'], ['bids'], ['means_dates'],['means']];
_.each(auctions, function(auction){
var auction_bids = _.sortBy(auction.bids, 'created_at');
if (auction.winning_bid && auction.bids.length > 0) {
settings.cols[0].push(microp.format.date(auction.ended_at));
settings.cols[1].push(+auction.winning_bid.amount);
}
});
var pairedDates = _.zip(settings.cols[0],settings.cols[1]);
pairedDates = _.groupBy(pairedDates, function(num){
return num[0];
});
var dateList = _.keys(pairedDates);
var bidList = _.values(pairedDates);
settings.cols[2] = dateList;
settings.cols[2][0] = 'means_dates';
_.forEach(bidList, function(bidGrouping, i) {
if (i === 0) {
return;
}
var winningBidGroup = _.map(bidGrouping, function(num) {
return num[1];
});
settings.cols[3].push(d3.mean(winningBidGroup));
});
return { cols: settings.cols };
},
chart4: function chart4 (auctions) {
var settings = {};
settings.cols = [
['date_community'],
['Bidders'],
['Open-source projects'],
['Auctions']
];
settings.x = 'date_community';
_.each(auctions, function(auction){
auction.github_repo = microp.format.stardardizeUrl(auction.github_repo);
auction.github_repo = microp.format.removeGitPrefix(auction.github_repo);
})
var byGithub = d3.nest()
.key(function(d){return microp.format.date(d.ended_at)})
.key(function(d){return d.github_repo})
.entries(auctions);
var byAuction = d3.nest()
.key(function(d){return microp.format.date(d.ended_at)})
.entries(auctions);
_.each(byGithub, function (date, key) {
// Dates
settings.cols[0].push(date.key);
// Auctions
settings.cols[2].push(date.values.length);
});
_.each(byAuction, function (date, key) {
var bidders = _.map(date.values, function (auction, key) {
var bidder_ids = _.map(auction.bids, function (bid, key) {
return bid.bidder_id;
});
return bidder_ids;
});
var uniqueBidders = _.uniq(_.flatten(bidders)).length;
// Projects
settings.cols[3].push(date.values.length);
// Bidders
settings.cols[1].push(uniqueBidders);
});
return settings;
},
/**
* @function
* @returns {Object} - to be loaded as a c3 scatterplot.
**/
chart5: function chart5 (auctions) {
var settings = {};
// set of columns for the scatterplot
// @format [['date_#{ auction number }', '#{ date }'],['#{ auction number }', #{ number of bids }]]
settings.cols = [];
// object with corresponding date strings
// @format { #{ auction number }, '#{ corresponding date string }'}
settings.xs = {};
// object with winning bid by auction
// @format { #{ auction number }, #{ corresponding winning bid }}
settings.z = {};
// only include auctions that have a winning_bid
auctions = _.filter(auctions, function (auction) {
return auction.winning_bid;
})
var auctionsByEndtime = d3.nest()
.key(function(d){
return microp.format.date(d.ended_at);
})
.key(function(d){ return d.id; })
.entries(auctions);
_.each(auctionsByEndtime, function(dateObj, i, list) {
var dateString = 'date_'+ i;
settings.cols.push([dateString, dateObj.key]);
_.each(dateObj.values, function(auction, j, list) {
var winningBid = auction.values[0].winning_bid.amount
? auction.values[0].winning_bid.amount
: undefined;
settings.cols.push([auction.key, auction.values[0].bids.length]);
settings.xs[auction.key] = dateString;
settings.z[auction.key] = winningBid; // $ amount
});
});
return settings;
},
donut1: function donut1 (auctions) {
var settings = {};
settings.cols = [];
_.each(auctions, function(auction){
auction.github_repo = microp.format.stardardizeUrl(auction.github_repo);
auction.github_repo = microp.format.removeGitPrefix(auction.github_repo);
})
var repos = d3.nest()
.key(function(d){ return d.github_repo;})
.rollup(function(d){ return d.length; })
.map(auctions);
settings.cols = _.map(repos, function(value, key) {
return [key,value];
});
return settings;
},
donut2: function donut2(auctions) {
var settings = {};
settings.cols = [];
var repos = [
{'name': '18F/fedramp-micropurchase', 'language': 'google sheets'},
{'name': '18F/micropurchase', 'language': 'ruby'},
{'name': '18F/openopps-platform', 'language': 'ruby'},
{'name': '18F/playbook-in-action', 'language': 'python'},
{'name': '18F/procurement-glossary', 'language': 'yml'},
{'name': '18F/tock', 'language': 'python'},
{'name': '18F/travel-form', 'language':undefined},
{'name': '18F/deduplicate-tock-float', 'language': undefined},
{'name': '18F/aws-broker', 'language': 'go'}
];
repos = d3.nest()
.key(function(d){ return d.language; })
.rollup(function(d) {return d.length;})
.map(repos);
settings.cols = _.map(repos, function(value, key) {
if ( key !== 'undefined' && key !== undefined) {
return [key,value];
} else { return []; }
});
return settings;
}
},
settings: function () {
var settings = {},
auctions = this.auctions,
that = this;
_.each(this.create, function(fn) {
that.settings[fn.name] = settings[fn.name] = fn(auctions);
});
return settings;
},
generate: function () {
var settings = this.settings();
_.each(this.load, function(fn) {
fn(settings[fn.name]);
});
}
}
window.Charts = Charts;
}(this));