Fix word cloud.

This commit is contained in:
BoHung Chiu 2023-03-26 10:54:23 +08:00
parent 74b5257bc4
commit 431e3f9f4c
1 changed files with 39 additions and 26 deletions

View File

@ -1,33 +1,46 @@
<div class="w-video_data video_data-widget-2">
<%= javascript_include_tag 'video_pro/wordcloud2.js' %>
<div style="width: 100%; height: 60vw" class="my_favorite_latin_words jqcloud"><div>
<div style="width: 100%; height: 40vw" class="my_favorite_latin_words jqcloud wordcloud2"><div>
<script>
var container_{{subpart-id}} = $('[data-subpart-id="{{subpart-id}}"] .my_favorite_latin_words');
var width_{{subpart-id}} = container_{{subpart-id}}.width();
container_{{subpart-id}}.attr('width', width_{{subpart-id}})
var jqcloud_words_{{subpart-id}} = {{jqcloud_words}};
var text_map_{{subpart-id}} = {};
var max_value_{{subpart-id}} = 1;
$.each(jqcloud_words_{{subpart-id}}, function(i, v){
if (max_value_{{subpart-id}} < v.weight){
max_value_{{subpart-id}} = v.weight;
}
});
var list_{{subpart-id}} = $.map(jqcloud_words_{{subpart-id}}, function(v){
text_map_{{subpart-id}}[v.text] = v.link;
return [[v.text, Math.ceil(v.weight/max_value_{{subpart-id}}*3), v.link]];
});
container_{{subpart-id}}.on('wordcloudstop', function (e) {
$(this).children('span').each(function (i, v) {
var text = $(this).html();
$(this).html('<a title="'+ list_{{subpart-id}}[i][0] + '" style="color: inherit" href="' + list_{{subpart-id}}[i][2] +'">' + text + '</a>');
});
});
$(document).ready(function(){
var container = $('[data-subpart-id="{{subpart-id}}"] .my_favorite_latin_words');
var width = container.width();
container.attr('width', width)
var jqcloud_words = {{jqcloud_words}};
var text_map = {};
var max_value = 1;
$.each(jqcloud_words, function(i, v){
if (max_value < v.weight){
max_value = v.weight;
}
});
var list = $.map(jqcloud_words, function(v){
text_map[v.text] = v.link;
return [[v.text, Math.ceil(v.weight/max_value*3), v.link]];
});
container.on('wordcloudstop', function (e) {
$(this).children('span').each(function (i, v) {
var text = $(this).html();
$(this).html('<a title="'+ list[i][0] + '" style="color: inherit" href="' + list[i][2] +'">' + text + '</a>');
});
});
WordCloud(container[0], {
list: list,
WordCloud(container_{{subpart-id}}[0], {
list: list_{{subpart-id}},
gridSize: 3,
weightFactor: jqcloud_words.length * width / 550,
weightFactor: jqcloud_words_{{subpart-id}}.length * width_{{subpart-id}} / 550,
rotateRatio: 0,
shuffle: false,
drawOutOfBound: false,
shrinkToFit: true,
shape: 'square'
});
})
$(window).resize(function(){
width_{{subpart-id}} = container_{{subpart-id}}.width();
WordCloud(container_{{subpart-id}}[0], {
list: list_{{subpart-id}},
gridSize: 3,
weightFactor: jqcloud_words_{{subpart-id}}.length * width_{{subpart-id}} / 550,
rotateRatio: 0,
shuffle: false,
drawOutOfBound: false,
@ -36,4 +49,4 @@
});
})
</script>
</div>
</div>