d3-cloud关于点击事件是否可以在D3的标记云中使用点击事件,如果可以,如何使用
<!DOCTYPE html>
<meta charset="utf-8">
<script src="../lib/d3/d3.js"></script>
<script src="../d3.layout.cloud.js"></script>
<body>
<script>
var fill = d3.scale.category20();
var zz= ["Hello", "world", "normally", "you", "want", "more", "words", "than", "this"];
d3.layout.cloud().size([300, 300])
.words((zz).map(function(d) {
return {text: d, size: 10 + Math.random() * 90};
}))
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
function draw(words) {
d3.select("body").append("svg")
.attr("width", 300)
.attr("height", 300)
.append("g")
.attr("transform", "translate(150,150)")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "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.text; });
}
</script>(我希望"Hello“、"world”、"normally“、"you”、" want“、"more”、" words“、"than”、"this“这些词是可点击的)
发布于 2013-01-18 19:02:06
只需在脚本的末尾添加以下内容:
...
.text(function(d) { return d.text; })
.on("click", function(d) {
alert(d.text);
});发布于 2013-12-11 05:26:00
您可以使用.on绑定事件
我将向您展示如何在鼠标悬停时放大或减小标签大小,以及如何在单击标签时进行记录(我使用的是咖啡脚本)
.on("mouseover", ->
d3.select(this).style("font-size", (d) ->
d.size + 10 +"px"
)
.on("mouseout", ->
d3.select(this).style("font-size", (d) ->
d.size - 10 +"px"
)
.on("mousedown", ->
console.log(this)https://stackoverflow.com/questions/14376162
复制相似问题