【问题标题】:d3 wordcloud fontsize doesn't adaptd3 wordcloud fontsize 不适应
【发布时间】:2016-05-10 20:45:46
【问题描述】:

我的 d3.wordcloud 文档的字体大小不受 wordlist.tsv 文件中指定的计数的影响。但是根据我对代码的理解,它应该是。

<!DOCTYPE html>
<html>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="build/d3.layout.cloud.js"></script>
<head>
    <title>Word Cloud Example</title>
</head>

<body>
</body>

<script>
 var width = 500; heigth = 500;



var wordscale = d3.scale.linear().range([10,60]);
 var fill = d3.scale.category20();
        d3.tsv("wordlist.tsv",function(data){
        var wordlist2 = data
                .filter(function(d){ return +d.count > 10;})
                .map(function(d){ return{text: d.word +" ("+ d.count+")", fsize: +d.count};})
                .sort(function(a,b){return d3.descending(a.fsize,b.fsize);})
                .slice(0,100);



        wordscale.domain([0,d3.max(wordlist2, function(d) {return d.fsize;})]);
            d3.layout.cloud()
                .size([width, heigth]) 
                .padding(1)
                .words(wordlist2) 
                .rotate(0)
                .fontSize(function(d) { return wordscale(+d.fsize); })
                .on("end", draw) //afblijven
                .start(); //start algoritme
});


function draw(words) {
   d3.select("body")
          .append("svg")
                .attr("width", width)
                .attr("height", heigth)
                .attr("class", "wordcloud")
              .append("g")
                .attr("transform", "translate("+width/2+","+heigth/2+")")
                .selectAll("text")
                .data(words)
              .enter().append("text")
                .style("font-size", function(d) { d.fsize + "px"; })
                .style("fill", function(d, i) { return fill(i); })
                .attr("transform", function(d) {return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";})
                .text(function(d) { return d.text; });
    }

</script>
</html>

根据我对以下代码行的理解:

.fontSize(function(d) { return wordscale(+d.fsize); })

应该使代码适应代码前面定义的比例,但可惜它没有,并且所有单词的大小都相同。尽管 .tsv 文件中每个单词的计数参数不同

有什么建议吗?

【问题讨论】:

    标签: d3.js font-size word-cloud


    【解决方案1】:

    已解决,我忘记了以下代码行中的return语句

    .style("font-size", function(d) { d.fsize + "px"; })
    

    这应该是:

    .style("font-size", function(d) {return d.fsize + "px"; })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2013-09-16
      • 2013-03-14
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 2021-06-03
      相关资源
      最近更新 更多