forked from cmgerber/PLOS_Cloud_Explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_example.html
77 lines (64 loc) · 1.99 KB
/
cloud_example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<!-- used https://github.com/jasondavies/d3-cloud/blob/master/examples/simple.html -->
<style type="text/css">
g {
}
</style>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="js/d3.layout.cloud.js"></script>
<script src="js/colorbrewer.js"></script>
<script>
// var fill = d3.scale.category20();
var fill = d3.scale.ordinal()
.range(colorbrewer.Blues[10]);
var counter = 0; //for keeping track of color change
d3.csv("wordcloud2.csv", function(error, wordlist) {
data = wordlist.map(function(d)
{
word = d.word;
weight = parseFloat(d.count);
return {"word": word, "weight": weight};
});
console.log(data);
// for(var i=0;i<data.length;i++){
// var obj = data[i];
// var word = [];
// for(var key in obj) {
// wordArray.push(obj[key]);
// }
// }
d3.layout.cloud().size([400, 400])
.words(data.map(function(d) {
return {text: d.word, size: 10 + d.weight * 2};
}))
.rotate(function() { return 0; }) //~~(Math.random() * 2) * 90 (for different orientations)
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
function draw(words) {
d3.select("body").append("svg")
.attr("width", 450)
.attr("height", 450)
.append("g")
.attr("transform", "translate(200,200)")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
//loops through colors the first 10 words are the darkest blue ect...
.style("fill", function(d, i) {
if(i%10 === 0){counter++;}
return fill(counter); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
});
</script>
</body>