Commit 82209850 by Valera Rozuvan Committed by Vasyl Nakvasiuk

Small refactor.

parent 2bcc0065
...@@ -14,7 +14,6 @@ WordCloudMain.prototype = { ...@@ -14,7 +14,6 @@ WordCloudMain.prototype = {
_this = this; _this = this;
console.log('submit answer');
this.wordCloudEl.find('input.input-cloud').each(function(index, value){ this.wordCloudEl.find('input.input-cloud').each(function(index, value){
data.student_words.push($(value).val()); data.student_words.push($(value).val());
}); });
...@@ -31,7 +30,7 @@ WordCloudMain.prototype = { ...@@ -31,7 +30,7 @@ WordCloudMain.prototype = {
(response.hasOwnProperty('status') !== true) || (response.hasOwnProperty('status') !== true) ||
(typeof response.status !== 'string') || (typeof response.status !== 'string') ||
(response.status.toLowerCase() !== 'success')) { (response.status.toLowerCase() !== 'success')) {
console.log('Bad response!');
return; return;
} }
_this.showWordCloud(response); _this.showWordCloud(response);
...@@ -48,18 +47,24 @@ WordCloudMain.prototype = { ...@@ -48,18 +47,24 @@ WordCloudMain.prototype = {
'showWordCloud': function(response){ 'showWordCloud': function(response){
var words, var words,
_this = this, _this = this,
fill = d3.scale.category20(); fill = d3.scale.category20(),
maxSize, minSize;
console.log('Show word cloud with next:'); this.wordCloudEl.find('#input-cloud-section').hide();
console.log(response);
inputSection = this.wordCloudEl.find('#input-cloud-section'); words = response.top_words;
resultSection = this.wordCloudEl.find('#result-cloud-section');
inputSection.hide(); maxSize = 0;
resultSection.show(); minSize = 10000;
words = response.top_words; $.each(words, function (index, word) {
if (word.size > maxSize) {
maxSize = word.size;
}
if (word.size < minSize) {
minSize = word.size;
}
});
d3.layout.cloud().size([500, 500]) d3.layout.cloud().size([500, 500])
.words(words) .words(words)
...@@ -68,7 +73,15 @@ WordCloudMain.prototype = { ...@@ -68,7 +73,15 @@ WordCloudMain.prototype = {
}) })
.font('Impact') .font('Impact')
.fontSize(function (d) { .fontSize(function (d) {
return d.size; var size;
size = (d.size / maxSize) * 140;
if (size < 20) {
return 0;
}
return size;
}) })
.on('end', draw) .on('end', draw)
.start(); .start();
...@@ -77,11 +90,24 @@ WordCloudMain.prototype = { ...@@ -77,11 +90,24 @@ WordCloudMain.prototype = {
return; return;
function draw(words) { function draw(words) {
d3.select('.word_cloud_d3_' + _this.hash).append('svg') var el;
$('#word_cloud_d3_' + _this.hash).remove();
el = $(
'<div ' +
'id="' + 'word_cloud_d3_' + _this.hash + '" ' +
'style="display: block; width: 500px; height: 500px; margin-left: auto; margin-right: auto;" ' +
'></div>'
);
_this.wordCloudEl.append(el);
d3.select('#word_cloud_d3_' + _this.hash).append('svg')
.attr('width', 500) .attr('width', 500)
.attr('height', 500) .attr('height', 500)
.attr('id', 'word_cloud_d3_' + _this.hash)
.append('g') .append('g')
.attr('transform', 'translate(125,125)') .attr('transform', 'translate(190,250)')
.selectAll('text') .selectAll('text')
.data(words) .data(words)
.enter().append('text') .enter().append('text')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment