-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_v2.js
141 lines (119 loc) · 4.12 KB
/
main_v2.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
var parseData = function(sampleData){
console.log("now we're parsing");
var parsedData = {};
var inputDataTypes = parsing.getTypesFromData(sampleData);
var requiredData = {
ordered:[
['string', 'date','number'],
['string', 'date','number'],
['string', 'date','number'],
['string', 'date','number'],
['number'],
['number']
]
};
if (parsing.typeChecker(inputDataTypes, requiredData)){
parsedData.data = sampleData;
} else {
parsedData.passedTypeChecks = false;
parsedData.inputDataTypes = inputDataTypes;
parsedData.requiredData = requiredData;
}
return parsedData;
}
var drawGraph = function(sampleData, parsedData, divLocation){
console.log("now we're graphing");
xf = crossfilter(parsedData);
var article = xf.dimension(function (d) {
return d.article_id;
});
var topic = xf.dimension(function (d) {
return d.topic_id;
});
var word = xf.dimension(function (d) {
return d.word_name;
});
var dimGroup = word.group().reduceSum(function(d,i){
return d.word_prob;
});
var fill = d3.scale.category20();
var width = $('#cloud').width();
var height = $('#cloud').height();
var uniqueArticles = article.group().top(Infinity);
appendArticles(uniqueArticles, '#articles');
var selectedArticle;
var selectedTopic;
var topics;
$('.articleChoice').on('click', function(){
selectedArticle = $(this).text();
uniqueTopics = topicsFromArticle(selectedArticle);
appendTopics(uniqueTopics, '#topics');
});
function appendArticles(uniqueArticles, location){
uniqueArticles.forEach(function(d){
$(location).append("<a href='#' class='articleChoice'>"+d.key+"<\/a><\/br>");
});
}
function appendTopics(uniqueTopics, location){
$('#topics').empty();
$('#cloud').empty();
uniqueTopics.forEach(function(d){
$(location).append("<a href='#' class='topicChoice'>"+d.key+"<\/a><\/br>");
});
$('.topicChoice').on('click', function(){
selectedTopic = $(this).text();
uniqueWords = wordsFromTopic(selectedTopic);
update(uniqueWords, '#cloud');
});
}
function topicsFromArticle(article_id){
article.filterAll();
article.filterExact(article_id);
return topic.group().top(Infinity);
}
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; });
}
};
}