d3 词云 重叠问题
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="js/libs/d3/d3.v3.min.js"></script>
<script src="js/libs/d3/d3.layout.cloud.1.js"></script>
<script>
//Simple animated example of d3-cloud - https://github.com/jasondavies/d3-cloud
//Based on https://github.com/jasondavies/d3-cloud/blob/master/examples/simple.html
// Encapsulate the word cloud functionality
function wordCloud(selector) {
var fill = d3.scale.category20();
//Construct the word cloud's SVG element
var svg = d3.select(selector).append("svg")
.attr("width", 500)
.attr("height", 500)
.append("g")
.attr("transform", "translate(250,250)");
//Draw the word cloud
function draw(words) {
var cloud = svg.selectAll("g text")
.data(words, function(d) { return d.text; })
//Entering words
cloud.enter()
.append("text")
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr('font-size', 1)
.text(function(d) { return d.text; });
//Entering and existing words
cloud
.transition()
.duration(600)
.style("font-size", function(d) { return d.size + "px"; })
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.style("fill-opacity", 1);
//Exiting words
cloud.exit()
.transition()
.duration(200)
.style('fill-opacity', 1e-6)
.attr('font-size', 1)
.remove();
}
//Use the module pattern to encapsulate the visualisation code. We'll
// expose only the parts that need to be public.
return {
//Recompute the word cloud for a new set of words. This method will
// asycnhronously call draw when the layout has been computed.
//The outside world will need to call this function, so make it part
// of the wordCloud return value.
update: function(words) {
d3.layout.cloud().size([500, 500])
.words(words)
.padding(5)
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
}
}
}
//Some sample data - http://en.wikiquote.org/wiki/Opening_lines
var words = [
"You don't know about me without you have read a book called The Adventures of Tom Sawyer but that ain't no matter.",
"The boy with fair hair lowered himself down the last few feet of rock and began to pick his way toward the lagoon.",
"When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating his eleventy-first birthday with a party of special magnificence, there was much talk and excitement in Hobbiton.",
"It was inevitable: the scent of bitter almonds always reminded him of the fate of unrequited love."
]
//Prepare one of the sample sentences by removing punctuation,
// creating an array of words and computing a random size attribute.
function getWords(i) {
return words[i]
.replace(/[!\.,:;\?]/g, '')
.split(' ')
.map(function(d) {
return {text: d, size: 10 + Math.random() * 60};
})
}
//This method tells the word cloud to redraw with a new set of words.
//In reality the new words would probably come from a server request,
// user input or some other source.
function showNewWords(vis, i) {
i = i || 0;
vis.update(getWords(i ++ % words.length))
//setTimeout(function() { showNewWords(vis, i + 1)}, 2000)
}
//Create a new instance of the word cloud visualisation.
var myWordCloud = wordCloud('body');
//Start cycling through the demo data
showNewWords(myWordCloud);
</script>
- 解决viewPager fragment有时候内容重叠的问题的方法
- 使用RadioGroup 的时候出现onCheckChanged() 执行了三次的问题和App重启页面重叠现象
- SeedCoder2014热身题目8:圆的重叠问题
- LeetCode 435. Non-overlapping Intervals 无重叠区间(贪心问题,两种解法)
- UICollectionView与UITableView在复用时,数据重叠问题
- Ubuntu14.04设置terminator,解决字迹重叠问题
- 用Fragment制作的Tab页面产生的UI重叠问题
- 解决APP中fragment重叠问题
- 【矩形覆盖问题分析】 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
- z-index顺序重叠问题
- memmove 和 memcpy的区别以及处理内存重叠问题
- hihoCoder 1407 后缀数组二·重复旋律2 && POJ 1743 Musical Theme(最长不可重叠重复子串问题)
- 网页 多个div层 重叠居中 问题
- UICollectionViewCell重用注意要点(解决视图重叠问题)
- fragment重叠问题解决方案
- 彻底解决Fragment重叠的问题
- 图片为背景的Jpanel 重叠问题
- POJ-1743 Musical Theme 字符串问题 不重叠最长重复子串
- Activity被回收后,重建时Fragment重叠问题
- smartform循环打印后将多页重叠打印到一页上问题解决