|
// Generated by CoffeeScript 1.4.0 |
|
(function() { |
|
var correct_x, correct_y, height, svg, treemap, vis, width, zoom, zoomable_layer, |
|
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; |
|
|
|
svg = d3.select('svg'); |
|
|
|
width = svg.node().getBoundingClientRect().width; |
|
|
|
height = svg.node().getBoundingClientRect().height; |
|
|
|
treemap = d3.layout.treemap().size([width, height]).value(function(node) { |
|
return node.count; |
|
}).sort(function(a, b) { |
|
return a.count - b.count; |
|
}).ratio(2).round(false); |
|
|
|
correct_x = d3.scale.linear().domain([0, width]).range([0, width * 1.05]); |
|
|
|
correct_y = d3.scale.linear().domain([0, height]).range([0, height * 3 / 4]); |
|
|
|
svg.attr({ |
|
viewBox: "" + (-width / 2) + " " + (-height / 2) + " " + width + " " + height |
|
}); |
|
|
|
zoomable_layer = svg.append('g'); |
|
|
|
zoom = d3.behavior.zoom().scaleExtent([1, 10]).on('zoom', function() { |
|
return zoomable_layer.attr({ |
|
transform: "translate(" + (zoom.translate()) + ")scale(" + (zoom.scale()) + ")" |
|
}); |
|
}); |
|
|
|
svg.call(zoom); |
|
|
|
vis = zoomable_layer.append('g').attr({ |
|
transform: "translate(" + (-width / 2) + "," + (-height / 2) + ")" |
|
}); |
|
|
|
d3.csv('kw_twitter.csv', function(keywords_data) { |
|
var color, keywords; |
|
keywords = keywords_data.map(function(d) { |
|
return d.keyword; |
|
}); |
|
color = function(txt, light) { |
|
var noise; |
|
Math.seedrandom(txt + 'abcdef'); |
|
noise = function(W) { |
|
return Math.random() * W - W / 2; |
|
}; |
|
return d3.hcl(0 + noise(360), 40, __indexOf.call(keywords, txt) >= 0 ? 10 : 70); |
|
}; |
|
return d3.csv('word_count_drugsforum.csv', function(data) { |
|
var enter_labels, labels, nodes_data, tree; |
|
data.forEach(function(d) { |
|
return d.count = +d.count; |
|
}); |
|
tree = { |
|
children: data, |
|
name: "cassandra" |
|
}; |
|
nodes_data = treemap.nodes(tree); |
|
labels = vis.selectAll('.label').data(nodes_data.filter(function(node) { |
|
return node.depth === 1; |
|
})); |
|
enter_labels = labels.enter().append('svg').attr({ |
|
"class": 'label' |
|
}); |
|
enter_labels.append('text').text(function(node) { |
|
return node.stem.toUpperCase(); |
|
}).attr({ |
|
dy: '0.35em', |
|
fill: function(node) { |
|
return color(node.stem, false); |
|
} |
|
}).each(function(node) { |
|
var bbox, bbox_aspect, node_bbox, node_bbox_aspect, rotate; |
|
bbox = this.getBBox(); |
|
bbox_aspect = bbox.width / bbox.height; |
|
node_bbox = { |
|
width: node.dx, |
|
height: node.dy |
|
}; |
|
node_bbox_aspect = node_bbox.width / node_bbox.height; |
|
rotate = bbox_aspect >= 1 && node_bbox_aspect < 1 || bbox_aspect < 1 && node_bbox_aspect >= 1; |
|
node.label_bbox = { |
|
x: bbox.x + (bbox.width - correct_x(bbox.width)) / 2, |
|
y: bbox.y + (bbox.height - correct_y(bbox.height)) / 2, |
|
width: correct_x(bbox.width), |
|
height: correct_y(bbox.height) |
|
}; |
|
if (rotate) { |
|
node.label_bbox = { |
|
x: node.label_bbox.y, |
|
y: node.label_bbox.x, |
|
width: node.label_bbox.height, |
|
height: node.label_bbox.width |
|
}; |
|
return d3.select(this).attr('transform', 'rotate(-90)'); |
|
} |
|
}); |
|
return enter_labels.attr({ |
|
x: function(node) { |
|
return node.x; |
|
}, |
|
y: function(node) { |
|
return node.y; |
|
}, |
|
width: function(node) { |
|
return node.dx; |
|
}, |
|
height: function(node) { |
|
return node.dy; |
|
}, |
|
viewBox: function(node) { |
|
return "" + node.label_bbox.x + " " + node.label_bbox.y + " " + node.label_bbox.width + " " + node.label_bbox.height; |
|
}, |
|
preserveAspectRatio: 'none' |
|
}); |
|
}); |
|
}); |
|
|
|
}).call(this); |