2016-05-10 7 views
-1

私のd3.wordcloud文書のフォントサイズは、wordlist.tsvファイルで指定されたカウントの影響を受けません。しかし、私はそれがすべきコードの理解から。以前のコードで定義されているようd3 wordcloud fontsizeが適合しない

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

スケールにコードを適応させる必要がありますが、そうでないと、悲しいかなすべての単語は、次のとおりです。私は次のコード行を理解して何から

<!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> 

同じサイズ。 countパラメータは.tsvファイルの各単語ごとに異なりますが、

アドバイスはありますか?

解決

答えて

1

、私は次のコード行で

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

をreturn文を忘れてしまった、これは次のようになります。

.style("font-size", function(d) {return d.fsize + "px"; }) 
関連する問題