From 95266176402fd448a78064196a97254976204294 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 21 May 2014 18:45:12 +0200 Subject: Add helper to refresh spam markings --- js/spam.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/js/spam.js b/js/spam.js index c37426e..e007b8c 100644 --- a/js/spam.js +++ b/js/spam.js @@ -168,9 +168,7 @@ function processData(data) { var node = contents.append('g').selectAll('circle') .data(force.nodes()) .enter().append('circle') - .attr('class', function (d) { - return 'node ' + (d.isSpam ? 'spam' : 'ham'); - }) + .attr('class', getNodeClasses) .attr('r', function (d) { return d.radius; }) @@ -332,22 +330,27 @@ function processData(data) { } }()); +// initializes a request for a users.csv file. +function requestUsersCsv(filename) { + // userid,name,tweetCount + return d3.csv(filename) + .row(function (d) { + return { + name: d.name, + group: +d.userid, + tweetCount: d.tweetcount, + radius: Math.sqrt(d.tweetcount), + isSpam: +d.isspam, + // indices of nodes that link to this one + relatedTo: [], relatedFrom: [], + }; + }); +} + function run() { /* fetch CSV files and render the result */ getEdgesNodes( - // userid,name,tweetCount - d3.csv('users.csv') - .row(function (d) { - return { - name: d.name, - group: +d.userid, - tweetCount: d.tweetcount, - radius: Math.sqrt(d.tweetcount), - isSpam: +d.isspam, - // indices of nodes that link to this one - relatedTo: [], relatedFrom: [], - }; - }), + requestUsersCsv('users.csv'), // source,target,value d3.csv('links.csv') .row(function (d) { @@ -361,6 +364,50 @@ function run() { ); } +// refresh the spam labels +function refreshSpam() { + // never loaded before? Let's do that first then... + if (!force) { + run(); + return; + } + + var cacheBump = '?cache-sucks=' + (new Date()).getTime() + Math.random(); + requestUsersCsv('users.csv' + cacheBump) + .get(function (errors, rows) { + if (errors) { + console.log('refreshSpam request error:', errors); + return; + } + // store spam levels for the new user + var map_userSpam = {}; + rows.forEach(function (user) { + map_userSpam[user.group] = user.isSpam; + }); + + // update the spam levels in the dataset + var nodes = force.nodes(); + nodes.forEach(function (node) { + if (node.group in map_userSpam) { + node.isSpam = map_userSpam[node.group]; + } else { + console.log('New data is missing user', node.group); + } + }); + + // change the appearance of displayed elements + d3.select('#map').selectAll('.node') + .data(nodes, function (d) { + return d.group; + }) + .attr('class', getNodeClasses); + }); +} + +function getNodeClasses(d) { + return 'node ' + (d.isSpam ? 'spam' : 'ham'); +} + // initialize SVG element and force initSvg(); -- cgit v1.2.1