diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-05-21 18:45:12 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-05-21 18:45:12 +0200 |
commit | 95266176402fd448a78064196a97254976204294 (patch) | |
tree | 43f777e888a7a7c31a4022d8e6641e863d539445 /js | |
parent | 28641ec5e1a5e819f92c715ba297d2cf2305941f (diff) | |
download | d3viz-95266176402fd448a78064196a97254976204294.tar.gz |
Add helper to refresh spam markings
Diffstat (limited to 'js')
-rw-r--r-- | js/spam.js | 79 |
1 files changed, 63 insertions, 16 deletions
@@ -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(); |