Skip to content

Commit

Permalink
implement HTML version of miniViz
Browse files Browse the repository at this point in the history
  • Loading branch information
jalperin committed Jul 20, 2018
1 parent 81dbdf0 commit b34ce82
Showing 1 changed file with 60 additions and 120 deletions.
180 changes: 60 additions & 120 deletions paperbuzzviz.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,36 @@ function PaperbuzzViz(options) {
var graphheight = options.graphheight;
var graphwidth = options.graphwidth;

var published_date = options.published_date || false;

var data = options.paperbuzzStatsJson;
//console.log(data);



if (published_date) {
// accept the published date passed in, if there was one

// Will choose pub date based on online pub, print pub, or issued. If no month or day defaults to 01
if (data.metadata['published-online']) {
var year = data.metadata["published-online"]["date-parts"][0][0];
if (data.metadata["published-online"]["date-parts"][0][1]) {
var month = data.metadata["published-online"]["date-parts"][0][1];
} else {var month = 01;
}
if (data.metadata["published-online"]["date-parts"][0][2]) {
var day = data.metadata["published-online"]["date-parts"][0][2];
} else {var day = 01;
}
var published_date = year+"-"+month+"-"+day;
} else if (data.metadata['published-online']) {
published_date = data.metadata["published-online"]["date-parts"][0];
} else if (data.metadata['published-print']) {
var year = data.metadata["published-print"]["date-parts"][0][0];
if (data.metadata["published-print"]["date-parts"][0][1]) {
var month = data.metadata["published-print"]["date-parts"][0][1];
} else {var month = 01;
}
if (data.metadata["published-print"]["date-parts"][0][2]) {
var day = data.metadata["published-print"]["date-parts"][0][2];
} else {var day = 01;
}
var published_date = year+"-"+month+"-"+day;
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 {
var year = data.metadata["issued"]["date-parts"][0][0];
if (data.metadata["issued"]["date-parts"][0][1]) {
var month = data.metadata["issued"]["date-parts"][0][1];
} else {var month = 01;
}
if (data.metadata["issued"]["date-parts"][0][2]) {
var day = data.metadata["issued"]["date-parts"][0][2];
} else {var day = 01;
}
var published_date = year+"-"+month+"-"+day;
published_date = [1969, 1, 1]; // TODO: Better handling if no date is found
}

// extract publication date
var pub_date = parseDate(published_date);

//console.log(pub_date);
// 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')
Expand All @@ -85,86 +72,12 @@ function PaperbuzzViz(options) {

sources = data.altmetrics_sources

if (showMini) {

//vizDiv.select("#loading").remove();

var miniViz = d3.select("body").append("svg")
.attr('height', "100%")
.attr("width", "100%");
//console.log(sources);
miniViz.selectAll("rect")
.data(sources)
.enter().append("rect")
.attr("fill", "#CECCCC")
.attr("height", "90")
.attr("width", "100%")
.attr("x", "0")
.attr("y", "0");

miniViz.append("text")
.attr("x", "10")
.attr("y", "20")
.attr("class", "miniViz-title")
.html('<a href="http://dx.doi.org/' + data.doi + '">' + data.metadata.title + '</a>');

var total = 0;
for (i = 0; i < data.altmetrics_sources.length; i++) {
total += data.altmetrics_sources[i].events_count;
}

function calculateYears(pub_date) {
var years = (new Date()).getFullYear() - pub_date.getFullYear();
return Math.ceil(years);
}

miniViz.append("text")
.attr("x", "10")
.attr("y", "70")
.attr("class", "miniViz-total")
.text(total);

miniViz.append("text")
.attr("x", "100")
.attr("y", "50")
.attr("class", "miniViz-text")
.text('Online mentions over');

miniViz.append("text")
.attr("x", "100")
.attr("y", "70")
.attr("class", "miniViz-text")
.text(calculateYears(pub_date) + ' year(s)');


var total = 0;
for (i = 0; i < data.altmetrics_sources.length; i++) {
console.log(i);
var x = 230 + (i*40);
var y = 33;

miniViz.append("foreignObject")
.attr("class", "miniViz-count")
.attr("id", "miniViz-count-" + data.altmetrics_sources[i].source_id)
.attr("x", x)
.attr("y", y)
.html('<i class="icon-' + data.altmetrics_sources[i].source_id + '"></i>' + " ");

miniViz.append("text")
.attr("x", x)
.attr("y", 70)
.attr("class", "miniViz-text")
.text(data.altmetrics_sources[i].events_count);
}


}

/**
* Initialize the visualization.
* NB: needs to be accessible from the outside for initialization
*/
this.initViz = function() {

vizDiv.select("#loading").remove();

if (showTitle) {
Expand All @@ -176,20 +89,47 @@ function PaperbuzzViz(options) {

vizDiv.append("br");


// loop through sources
sources.forEach(function(source) {
metricsFound_ = true;
addSourceRow(vizDiv, source);
});

if (!metricsFound_) {
if (sources.length > 0) {
if (showMini) {
addMiniViz(vizDiv, sources);
} else {
// loop through sources
sources.forEach(function(source) {
// metricsFound_ = true;
addSourceRow(vizDiv, source);
});
}
metricsFound_ = true;
} else {
vizDiv.append("p")
.attr("class", "muted")
.text("No metrics found.");
}
};

var addMiniViz = function(vizDiv, sources) {

miniDiv = vizDiv.append('div')
.attr('id', 'paperbuzz-mini');

miniDiv.append('span')
.attr('class', 'miniViz-total')

var total = 0;
sources.forEach(function(source) {
miniDiv.append('span')
.append('i')
.attr('class', 'icon-' + source.source_id)
.append('span')
.attr('class', 'miniViz-count')
.text(source.events_count);
total += source.events_count
});

miniDiv.selectAll('.miniViz-total')
.text(total);
}

/**
* Build each article level statistics source.
* @param {Object} canvas d3 element
Expand Down

0 comments on commit b34ce82

Please sign in to comment.