K-means clustering of the tweets extracted using search API.
xxxxxxxxxx
<meta charset="utf-8">
<style>
circle {
fill: rgb(31, 119, 180);
fill-opacity: .25;
stroke: rgb(31, 119, 180);
stroke-width: 1px;
}
.leaf circle {
fill: #ff7f0e;
fill-opacity: 1;
}
text {
font: 10px sans-serif;
}
.tooltip{
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var diameter = 960,
format = d3.format(",d");
var pack = d3.layout.pack()
.size([diameter - 4, diameter - 4])
.value(function(d) { return d.size; });
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(2,2)");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.tsv("kmeans10_big.txt", function(error, root) {
var clusters = {
"name": "Root",
"children": []
};
for (var j = 0; j < 10; j++) {
clusters.children.push({"name": j, "children": []});
}
for (var i = 0; i < root.length; i++) {
console.log(i);
if (root[i].cluster === undefined) {
root[i + 1].text = root[i].text + root[i + 1].text;
continue;
}
clusters.children[root[i].cluster].children.push({"name": root[i].text, "children": null, "size": 20});
}
var node = svg.datum(clusters).selectAll(".node")
.data(pack.nodes)
.enter().append("g")
.attr("class", function(d) { return d.children ? "node" : "leaf node"; })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("title")
.text(function(d) { return d.name; });
node.append("circle")
.attr("r", function(d) { return d.r; })
.on("mouseover", function(d){
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.name)
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
});
d3.select(self.frameElement).style("height", diameter + "px");
</script>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js