// Generated by CoffeeScript 1.4.0 (function() { var SIZE, height, matrix, svg, width, zoom, zoomable_layer; svg = d3.select('svg'); width = svg.node().getBoundingClientRect().width; height = svg.node().getBoundingClientRect().height; zoomable_layer = svg.append('g'); zoom = d3.behavior.zoom().scaleExtent([0.5, 8]).on('zoom', function() { return zoomable_layer.attr({ transform: "translate(" + (zoom.translate()) + ")scale(" + (zoom.scale()) + ")" }); }); svg.call(zoom); matrix = zoomable_layer.append('g').attr('transform', "translate(" + (width / 2) + "," + (height / 2) + ")"); SIZE = 2; d3.csv('social_graph_keywords_top1k.csv', function(data) { var back, cell_gs, clusters, color, enter_cell_gs, graph, link_index, max, node_index, tree; graph = { nodes: [], links: [] }; node_index = {}; link_index = {}; data.forEach(function(d) { var l, link_id, n; if (!(d.user_id_1 in node_index)) { n = { id: d.user_id_1, name: d.username_1 }; node_index[+d.user_id_1] = n; graph.nodes.push(n); } if (!(d.user_id_2 in node_index)) { n = { id: d.user_id_2, name: d.username_2 }; node_index[+d.user_id_2] = n; graph.nodes.push(n); } link_id = '(' + Math.min(+d.user_id_1, +d.user_id_2) + ')--(' + Math.max(+d.user_id_1, +d.user_id_2) + ')'; if (!(link_id in link_index)) { l = { source: node_index[Math.min(+d.user_id_1, +d.user_id_2)], target: node_index[Math.max(+d.user_id_1, +d.user_id_2)], tags: [ { tag: d.tag, weight: d.coposting } ] }; link_index[link_id] = l; return graph.links.push(l); } else { return link_index[link_id].tags.push({ tag: d.tag, weight: d.coposting }); } }); graph.links.forEach(function(d) { return d.avg_weight = d3.mean(d.tags, function(t) { return t.weight; }); }); max = d3.max(graph.links, function(l) { return l.avg_weight; }); color = function(d) { return d3.hcl(340 - 480 * d / max, 80 - Math.pow(0.5 - d / max, 2) / 400, 100 * d / max); }; console.debug('Computing hierarchical clustering...'); clusters = clusterfck.hcluster(graph.nodes, (function(a, b) { var link_id; link_id = '(' + Math.min(a.id, b.id) + ')--(' + Math.max(a.id, b.id) + ')'; if (!(link_id in link_index)) { return 1; } else { return 1 - link_index[link_id].avg_weight / max; } }), clusterfck.SINGLE_LINKAGE); tree = tree_utils.binary_to_std(clusters); graph.nodes = tree_utils.get_leaves(tree).map(function(d) { return d.value; }); graph.nodes.forEach(function(d, i) { return d.i = i; }); back = matrix.append('rect').attr({ "class": 'back', x: -graph.nodes.length / 2 * SIZE, y: -graph.nodes.length / 2 * SIZE, width: graph.nodes.length * SIZE, height: graph.nodes.length * SIZE }); cell_gs = matrix.selectAll('.cell').data(graph.links); enter_cell_gs = cell_gs.enter().append('g').attr({ fill: function(l) { return color(l.avg_weight); } }); enter_cell_gs.append('rect').attr({ "class": 'cell', x: function(d) { return (-graph.nodes.length / 2 + d.target.i) * SIZE; }, y: function(d) { return (-graph.nodes.length / 2 + d.source.i) * SIZE; }, width: SIZE, height: SIZE }); enter_cell_gs.append('rect').attr({ "class": 'cell', x: function(d) { return (-graph.nodes.length / 2 + d.source.i) * SIZE; }, y: function(d) { return (-graph.nodes.length / 2 + d.target.i) * SIZE; }, width: SIZE, height: SIZE }); return enter_cell_gs.append('title').text(function(l) { return 'user1: ' + l.source.name + '\nuser2: ' + l.target.name + '\naverage weight: ' + d3.format(',.2f')(l.avg_weight) + '\ntags: ' + l.tags.map(function(t) { return t.tag; }).join(', '); }); }); d3.select(self.frameElement).style('height', height + 'px'); }).call(this);