From a6fb7ce01144236b68666a5f3d4e4293deb66acd Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 21 May 2014 13:48:57 +0200 Subject: Split From and To in user info box --- bubble.js | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'bubble.js') diff --git a/bubble.js b/bubble.js index 8d957b6..773e481 100644 --- a/bubble.js +++ b/bubble.js @@ -207,7 +207,8 @@ function processData(data) { // ... and also update neighboring nodes contents.selectAll('.node') .classed('neighbor', function (node) { - return node.related.indexOf(d.index) >= 0; + return node.relatedTo.indexOf(d.index) >= 0 || + node.relatedFrom.indexOf(d.index) >= 0; }); }) .on('mouseout', function (d) { @@ -234,35 +235,33 @@ function processData(data) { userInfo.select('.spam-status') .text(d.isSpam ? 'SPAM' : 'ham'); - var selfId = d.index; - var links = []; - force.links().forEach(function (edge) { - // insert related elements, assuming no self-references - if (edge.source.index === selfId) { - links.push({ - direction: 'to', - node: edge.target + var nodes = { + 'from': force.nodes().filter(function (edge) { + return edge.relatedFrom.indexOf(d.index) >= 0; + }), + 'to': force.nodes().filter(function (edge) { + return edge.relatedTo.indexOf(d.index) >= 0; + }) + }; + Object.keys(nodes).forEach(function (dir) { + var related = nodes[dir]; + related.sort(function sort_by_name(a, b) { + return a.name.localeCompare(b.name); + }); + userInfo.select('.relations-' + dir + '-count') + .text(related.length); + var relations = userInfo.select('.relations-' + dir) + .selectAll('li') + .data(related, function key_func_links(d) { + // unique keys to group by node (index) + return d.index; }); - } else if (edge.target.index === selfId) { - links.push({ - direction: 'from', - node: edge.source + relations.enter().append('li') + .text(function (d) { + return dir + ' ' + d.name; }); - } + relations.exit().remove(); }); - userInfo.select('.relations-count') - .text(links.length); - var relations = userInfo.select('.relations') - .selectAll('li') - .data(links, function (d) { - // unique keys to group by direction and node (index) - return d.direction + ' ' + d.node.index; - }); - relations.enter().append('li') - .text(function (d) { - return d.direction + ' ' + d.node.name; - }); - relations.exit().remove(); } force.on('tick', function() { @@ -338,7 +337,8 @@ function run() { tweetCount: d.tweetcount, radius: Math.sqrt(d.tweetcount), isSpam: +d.isspam, - related: [] // nodes that link to this + // indices of nodes that link to this one + relatedTo: [], relatedFrom: [], }; }), // source,target,value -- cgit v1.2.1