簡體   English   中英

在d3.cloud中從-60旋轉到60

[英]Create rotations from -60 to 60 in d3.cloud

我試圖將https://www.jasondavies.com/wordcloud/上的cloud一詞顯示在我的網站上。

我設法將示例中的一些代碼(以及另一個SO答案)放在一起,並得出以下代碼:

var fill = d3.scale.category20();

var layout = d3.layout.cloud()
    .size([900, 500])
    .words([
        "Hello", "world", "normally", "you","Hello", "Hello", "normally", "you", "want", "more", "words",
        "987654321", "123456789"].map(function(d) {
        return {text: d, size: 10 + Math.random() * 90};
    }))
    .padding(5)
    .rotate(function() { return ~~(Math.random() * 2) * 60; })
    .font("Impact")
    .fontSize(function(d) { return d.size; })
    .on("end", draw);

layout.start();

function draw(words) {
    d3.select(".wordcloud").append("svg")
        .attr("width", layout.size()[0])
        .attr("height", layout.size()[1])
        .append("g")
        .attr("transform", "translate(" + layout.size()[0] / 2 + "," + layout.size()[1] / 2 + ")")
        .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; });
}

但這只會使單詞僅旋轉0度或60度https://imgur.com/a/fNyFH ,兩者之間沒有任何變化。 如何使它與示例鏈接中的一樣?

定義應用於單詞的旋轉方式的部分是:

.rotate(function() { return ~~(Math.random() * 2) * 60; })

在這里,您可以隨機定義0或60的旋轉角度。

要重現的示例中 ,單詞可以得到以下旋轉:[-60,-30、0、30、60],可以使用以下方式獲得旋轉:

.rotate(function() { return ~~(Math.random() * 5) * 30 - 60; })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM