forked from norlab/paperbuzz_viz_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paperbuzzviz.js
executable file
·546 lines (449 loc) · 18.1 KB
/
paperbuzzviz.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
* paperbuzzViz
* See https://github.com/jalperin/paperbuzzviz for more details
* Distributed under the MIT License. For full terms see the file docs/COPYING.
*
* @brief Article level metrics visualization controller.
*/
function PaperbuzzViz(options) {
// allow jQuery object to be passed in
// in case a different version of jQuery is needed from the one globally defined
$ = options.jQuery || $;
// Init basic options
var minItems_ = options.minItemsToShowGraph;
var showTitle = options.showTitle;
var showMini = options.showMini;
var formatNumber_ = d3.format(",d");
var parseDate = d3.timeParse('%Y-%m-%d');
var graphheight = options.graphheight;
var graphwidth = options.graphwidth;
var data = options.paperbuzzStatsJson;
var sources = data.altmetrics_sources;
var published_date = options.published_date || false;
if (published_date) {
// accept published date provided
// Will choose pub date based on online pub, print pub, or issued. If no month or day defaults to 01
} else if (data.metadata['published-online']) {
published_date = data.metadata["published-online"]["date-parts"][0];
} else if (data.metadata['published-print']) {
published_date = data.metadata["published-print"]["date-parts"][0];
} else if (published_date = data.metadata["issued"]) {
published_date = data.metadata["issued"]["date-parts"][0];
} else {
published_date = [1969, 1, 1]; // TODO: Better handling if no date is found
}
// Fill in the month and day with 1 if they are empty
if (published_date.length == 1) {
published_date.push(1)
}
if (published_date.length == 2) {
published_date.push(1)
}
// extract publication date
var pub_date = parseDate(published_date.join('-'));
var vizDiv;
// Get the Div where the viz should go (default to one with ID "paperbuzz')
if (options.vizDiv) {
vizDiv = d3.select(options.vizDiv);
} else {
vizDiv = d3.select("#paperbuzz");
}
// look to make sure browser support SVG
var hasSVG_ = document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1");
// to track if any metrics have been found
var metricsFound_;
/**
* Initialize the visualization.
* NB: needs to be accessible from the outside for initialization
*/
this.initViz = function() {
vizDiv.select("#loading").remove();
if (showTitle) {
vizDiv.append("a")
.attr('href', 'http://dx.doi.org/' + data.doi)
.attr("class", "paperbuzz-title")
.text(data.metadata.title);
}
if (showMini) {
vizDiv.attr("style", "width: 200px;")
}
if (sources.length > 0) {
// TEMPORARY:
// Disabling miniViz for now, opting for just hiding graphs
// until miniViz is finalized
// if (showMini || !hasSVG_) {
// addMiniViz(vizDiv, sources);
// } else {
// loop through sources
sources.forEach(function(source) {
// metricsFound_ = true;
addSourceRow(vizDiv, source);
});
// }
metricsFound_ = true;
} else {
vizDiv.append("p")
.attr("class", "paperbuzz-muted")
.text("No metrics found.");
}
};
var addMiniViz = function(vizDiv, sources) {
miniDiv = vizDiv.append('div')
.attr('id', 'paperbuzz-mini');
miniDiv.append('span')
.attr('class', 'paperbuzz-mini-total')
var total = 0;
sources.forEach(function(source) {
if (source.events_count > 0) {
miniDiv.append('div')
.attr('class', 'paperbuzz-mini-source')
.append('i')
.attr('class', 'icon-' + source.source_id)
.append('span')
.attr('class', 'paperbuzz-mini-count')
.text(source.events_count);
total += source.events_count;
}
});
miniDiv.selectAll('.paperbuzz-mini-total')
.text(total);
}
/**
* Build each article level statistics source.
* @param {Object} canvas d3 element
* @param {Array} sources Information about the source.
* @param {Object} data Statistics.
* @return {JQueryObject|boolean}
*/
var addSourceRow = function(canvas, source) {
var sourceRow, sourceHeading, sourceTitle;
// Build category html objects.
sourceRow = canvas.append("div")
.attr("class", "paperbuzz-source-row" + (showMini ? " paperbuzz-compact" : ""))
.attr("id", "paperbuzz-source-" + source.source_id);
addSource(source, sourceRow)
return sourceRow;
};
/**
* Add source information to the passed category row element.
* @param {Object} source
* @param {Object} category
* @param {JQueryObject} $sourceRow
* @return {JQueryObject}
*/
var addSource = function(source, $row) {
var $heading, $countLabel, $count,
total = source.events_count;
$heading = $row.append("div")
.attr("class", "paperbuzz-source-heading")
.append("span")
.attr("class", "paperbuzz-source-name")
.attr("id", "paperbuzz-source-" + source.source_id)
.text(source.source.display_name)
.append("div")
.attr("class", "paperbuzz-count-label")
.attr("id", "paperbuzz-count-" + source.source_id)
.html('<i class="icon-' + source.source_id + '" aria-hidden="true"></i>' + ' ' + formatNumber_(source.events_count));
// Only add a chart if the browser supports SVG
// TEMPORARY: also use to hide miniViz
if (hasSVG_ && !showMini) {
var level = false;
// check what levels we can show
var showDaily = false;
var showMonthly = false;
var showYearly = false;
if (source.events_count_by_year) {
level_data = source.events_count_by_year;
var yearTotal = level_data.reduce(function(i, d) { return i + d.count; }, 0);
var numYears = d3.timeYear.range(pub_date, new Date()).length;
if (yearTotal >= minItems_.minEventsForYearly &&
numYears >= minItems_.minYearsForYearly) {
showYearly = true;
level = 'year';
};
}
if (source.events_count_by_month) {
level_data = source.events_count_by_month;
var monthTotal = level_data.reduce(function(i, d) { return i + d.count; }, 0);
var numMonths = d3.timeMonth.range(pub_date, new Date()).length;
if (monthTotal >= minItems_.minEventsForMonthly &&
numMonths >= minItems_.minMonthsForMonthly) {
showMonthly = true;
level = 'month';
};
}
if (source.events_count_by_day){
level_data = source.events_count_by_day;
//console.log(level_data);
//function needs to return i + d.count only for the first 30 days
var dayTotal = level_data
.filter(item => parseDate(item.date) < d3.timeDay.offset(pub_date, 29) && parseDate(item.date) >= pub_date)
.reduce(function(i, d) { return i + d.count; }, 0);
//console.log(dayTotal);
var numDays = d3.timeDay.range(pub_date, new Date()).length;
//console.log(numDays);
if (dayTotal >= minItems_.minEventsForDaily && numDays >= minItems_.minDaysForDaily) {
showDaily = true;
level = 'day';
};
}
// The level and level_data should be set to the finest level
// of granularity that we can show
timeInterval = getTimeInterval(level);
// check there is data for
if (showDaily || showMonthly || showYearly) {
var $chartDiv = $row.append("div")
// .attr("style", "width: 70%; float:left;")
.attr("class", "paperbuzz-chart-area");
var viz = getViz($chartDiv, source);
loadData(viz, level);
var update_controls = function(control) {
control.siblings('.paperbuzz-control').removeClass('active');
control.addClass('active');
};
var $levelControlsDiv = $chartDiv.append("div")
.attr("class", "paperbuzz-controls")
// .attr("style", "width: " + (viz.margin.left + viz.width) + "px;")
// .append("div")
// .attr("style", "float:right;");
if (showDaily) {
$levelControlsDiv.append("button")
.classed("paperbuzz-control", true)
// .classed("disabled", !showDaily)
.classed("active", (level == 'day'))
.text("daily (first 30)")
.on("click", function() {
if (showDaily && !$(this).hasClass('active')) {
loadData(viz, 'day');
update_controls($(this));
}
}
);
$levelControlsDiv.append("text").text(" | ");
}
if (showMonthly) {
$levelControlsDiv.append("button")
// .attr("href", "javascript:void(0)")
.classed("paperbuzz-control", true)
// .classed("disabled", !showMonthly || !showYearly)
.classed("active", (level == 'month'))
.text("monthly")
.on("click", function() { if (showMonthly && !$(this).hasClass('active')) {
loadData(viz, 'month');
update_controls($(this));
} });
if (showYearly) {
$levelControlsDiv.append("text")
.text(" | ");
}
}
if (showYearly) {
$levelControlsDiv.append("button")
// .attr("href", "javascript:void(0)")
.classed("paperbuzz-control", true)
// .classed("disabled", !showYearly || !showMonthly)
.classed("active", (level == 'year'))
.text("yearly")
.on("click", function() {
if (showYearly && !$(this).hasClass('active')) {
loadData(viz, 'year');
update_controls($(this));
}
}
);
}
};
};
return $row;
};
/**
* Extract the date from the source
* @param level (day|month|year)
* @param d the datum
* @return {Date}
*/
var getDate = function(level, d) {
var parseString = ''
if (level == 'year') {
parseString = '%Y';
} else if (level == 'month') {
parseString = '%Y-%m';
} else if (level == 'day') {
parseString = '%Y-%m-%d';
}
return d3.timeParse(parseString)(d.date);
};
/**
* Format the date for display
* @param level (day|month|year)
* @param d the datum
* @return {String}
*/
var getFormattedDate = function(level, d) {
switch (level) {
case 'year':
return d3.time.format("%Y")(getDate(level, d));
case 'month':
return d3.time.format("%b %y")(getDate(level, d));
case 'day':
return d3.time.format("%d %b %y")(getDate(level, d));
}
};
/**
* Extract the data from the source.
* @param {string} level (day|month|year)
* @param {Object} source
* @return {Array} Metrics
*/
var getData = function(level, source) {
switch (level) {
case 'year':
return source.events_count_by_year;
case 'month':
return source.events_count_by_month;
case 'day':
return source.events_count_by_day;
}
};
/**
* Returns a d3 timeInterval for date operations.
* @param {string} level (day|month|year
* @return {Object} d3 time Interval
*/
var getTimeInterval = function(level) {
switch (level) {
case 'year':
return d3.timeYear;
case 'month':
return d3.timeMonth;
case 'day':
return d3.timeDay;
}
};
/**
* The basic general set up of the graph itself
* @param {JQueryElement} chartDiv The div where the chart should go
* @param {Object} sources
* @return {Object}
*/
var getViz = function(chartDiv, sources) {
var viz = {};
// size parameters
viz.margin = {top: 20, right: 20, bottom: 50, left: 50};
viz.width = graphwidth - viz.margin.left - viz.margin.right;
viz.height = graphheight - viz.margin.top - viz.margin.bottom;
// div where everything goes
viz.chartDiv = chartDiv;
// sources data
viz.sources = sources;
// just for record keeping
viz.name = sources.source_id;
viz.x = d3.scaleTime()
.range([0,viz.width]);
//.nice(d3.timeMonth);
viz.y = d3.scaleLinear()
.rangeRound([viz.height,0]);
viz.z = d3.scaleOrdinal();
viz.z.range(['main', 'alt']);
// the chart
viz.svg = viz.chartDiv.append("svg")
.attr("width", viz.width + viz.margin.left + viz.margin.right)
.attr("height", viz.height + viz.margin.top + viz.margin.bottom)
.append("g")
.attr("transform", "translate(" + viz.margin.left + "," + viz.margin.top + ")");
// draw the bars g first so it ends up underneath the axes
viz.bars = viz.svg.append("g");
viz.svg.append("g")
.attr("class", "x paperbuzz-axis")
.attr("transform", "translate(0," + (viz.height - 1) + ")");
viz.svg.append("g")
.attr("class", "y paperbuzz-axis");
viz.tip = d3.tip()
.attr('class', 'paperbuzz-tooltip')
.html(function(d) { return 'Count: ' + d.count + "<br>" + 'Date: ' + d.date; });
viz.tip.offset([-10, 0]); // make room for the little triangle
viz.svg.call(viz.tip);
return viz;
};
/**
* Takes in the basic set up of a graph and loads the data itself
* @param {Object} viz AlmViz object
* @param {string} level (day|month|year)
*/
var loadData = function(viz, level) {
var level_data = getData(level, viz.sources);
var timeInterval = getTimeInterval(level);
var end_date = new Date();
// use only first 29 days if using day view
// close out the year otherwise
if (level == 'day') {
end_date = timeInterval.offset(pub_date, 29);
} else {
end_date = d3.timeYear.ceil(end_date);
}
//
// Domains for x and y
//
// a time x axis, between pub_date and end_date
viz.x.domain([timeInterval.floor(pub_date), end_date]);
// a linear axis from 0 to max value found
viz.y.domain([0, d3.max(level_data, function(d) { return d.count; })]);
//
// Axis
//
var yAxis = d3.axisLeft(viz.y)
.tickValues([d3.max(viz.y.domain())]);
var ticks;
if (level == 'day') {
ticks = d3.timeDay.every(3);
} else if (level == 'month') {
ticks = d3.timeMonth.every(6);
} else {
ticks = d3.timeYear.every(1)
}
var xAxis = d3.axisBottom(viz.x)
.ticks(ticks);
var xFormat;
if (level == 'day') {
xFormat = "%b %d '%y";
} else if (level == 'month') {
xFormat = "%b %Y";
} else {
xFormat = "%Y"
}
// The chart itself
//
// TODO: these transitions could use a little work
var barWidth = Math.max((viz.width/(timeInterval.range(pub_date, end_date).length + 1)) - 2, 1);
var bars = viz.bars.selectAll(".paperbuzz-bar")
.data(level_data, function(d) { return getDate(level, d); });
bars
.enter().append("rect")
.attr("class", function(d) {
var bartype = (level == 'day' ? d3.timeWeek(getDate(level, d)) : getDate(level, d).getFullYear());
return "paperbuzz-bar " + viz.z(bartype);
})
.attr("x", function(d) { return viz.x(getDate(level, d)) + 2; }) // padding of 2, 1 each
.attr("y", function(d) { return viz.y(d.count) } )
.attr("width", barWidth)
.attr("height", function(d) { return viz.height - viz.y(d.count); })
.on("mouseover", viz.tip.show)
.on("mouseout", viz.tip.hide);
bars
.exit()
.remove();
viz.svg
.select(".x.paperbuzz-axis")
.call((xAxis)
.tickFormat(d3.timeFormat(xFormat)))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-45)");
viz.svg
.transition().duration(1000)
.select(".y.paperbuzz-axis")
.call(yAxis);
}
};