-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
154 lines (128 loc) · 4.61 KB
/
main.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var parseData = function(sampleData){
console.log("now we're parsing");
var parsedData = {};
parsedData.data = sampleData;
return parsedData;
}
var drawGraph = function(sampleData, parsedData, divLocation){
console.log("now we're graphing");
xf = crossfilter(parsedData);
var articleGroup = xf.dimension(function (d) {
return d.GroupID;
});
var article = xf.dimension(function (d) {
return d.DocumentID;
});
var topic = xf.dimension(function (d) {
return d.TopicID;
});
var word = xf.dimension(function (d) {
return d.word;
});
var dimGroup = word.group().reduceSum(function(d){
return d.topicWordProb;
});
var fill = d3.scale.category20();
var width = $('#cloud').width();{}
var height = $('#cloud').height();
var uniqueArticleGroups = articleGroup.group().top(Infinity);
appendArticleGroups(uniqueArticleGroups, '#articleGroups');
var selectedArticleGroup;
var selectedArticle;
var selectedTopic;
var topics;
$('.articleGroupChoice').on('click', function(){
selectedArticleGroup = $(this).text();
uniqueArticles = articlesFromArticleGroup(selectedArticleGroup);
appendArticles(uniqueArticles, '#articles');
});
function appendArticleGroups(uniqueArticleGroups, location){
uniqueArticleGroups.forEach(function(d){
$(location).append("<a href='#' class='articleGroupChoice' data-id="+d.key+">"+d.key+"<\/a><\/br>");
});
}
function appendArticles(uniqueArticles, location){
$('#articles').empty();
$('#topics').empty();
$('#cloud').empty();
uniqueArticles.forEach(function(d){
$(location).append("<a href='#' class='articleChoice' data-id="+d.key+">"+d.key+"<\/a><\/br>");
});
$('.articleChoice').on('click', function(){
selectedArticle = $(this).text();
uniqueTopics = topicsFromArticle(selectedArticle);
appendTopics(uniqueTopics, '#topics');
});
}
function appendTopics(uniqueTopics, location){
$('#topics').empty();
$('#cloud').empty();
uniqueTopics.forEach(function(d){
$(location).append("<a href='#' class='topicChoice' data-id="+d.key+">"+d.key+"<\/a><\/br>");
});
$('.topicChoice').on('click', function(){
selectedTopic = $(this).text();
uniqueWords = wordsFromTopic(selectedTopic);
update(uniqueWords, '#cloud');
});
}
function articlesFromArticleGroup(article_group_id){
articleGroup.filterAll();
articleGroup.filterExact(article_group_id);
return article.group().top(Infinity).filter(function(d){
return d.value > 0;
});
}
function topicsFromArticle(article_id){
article.filterAll();
article.filterExact(article_id);
return topic.group().top(Infinity).filter(function(d){
return d.value > 0;
});
}
function wordsFromTopic(topic_id){
topic.filterAll();
topic.filterExact(topic_id);
return dimGroup.top(200).filter(function(d){
return d.value > 0;
});
}
function update(data, location){
d3.layout.cloud().stop();
$(location).empty();
var data = JSON.parse( JSON.stringify( data ) );
var xScale = d3.scale.linear()
.domain([d3.min(data, function(d) { return d.value; }), d3.max(data, function(d) { return d.value; })])
.range([10,100]);
d3.layout.cloud().size([width, height])
.timeInterval(2)
.words(data)
.fontSize(function(d) { return xScale(+d.value); })
.text(function(d) { return d.key; })
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.on("end", draw)
.start();
function draw(words) {
d3.select(location).append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + [width >> 1, height >> 1] + ")")
.selectAll("text")
.data(words)
.enter().append("text")
.transition()
.duration(1000)
.attr("transform", function(d) { return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; })
.style("font-size", function(d) { return xScale(d.value) + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.key; });
}
};
}