Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D3 word cloud cannot show all words #166

Open
wudayu940214 opened this issue Jun 26, 2020 · 4 comments
Open

D3 word cloud cannot show all words #166

wudayu940214 opened this issue Jun 26, 2020 · 4 comments

Comments

@wudayu940214
Copy link

Hi

I am doing the word cloud by using d3. In the document, I have many words about one thousand but it seems that only show part of them. I would like to know if there is a limitation of the number to show in the word cloud and how to set the maximum number to show in the layout? Thanks

@isaraovin
Copy link

I am facing the same, have you figure out a way?

@sciamp
Copy link

sciamp commented Dec 3, 2020

Same here.

Could this be related to the size of the chart container? I'm using fixed width and height and it seems that shrinking the container prevents some words to show

@pmi123
Copy link

pmi123 commented Dec 6, 2020

I am also seeing missing words from my word clouds. In the examples images, the most frequent word is 'story'. If I use .rotate(function() { return 0; }) in the layout, the word 'story' appears. If I remove the rotate function in the layout, the word 'story' does not appear. There are 257 words in the word cloud.
Screenshot from 2020-12-06 11-44-48
Screenshot from 2020-12-06 11-43-48
The same thing happens in a smaller Spanish word cloud (88 words). The most common word is 'roja', and it only appears in the word cloud with rotate=0.
Screenshot from 2020-12-06 11-54-29
Screenshot from 2020-12-06 11-53-49
Any ideas on how to fix this?

@pmi123
Copy link

pmi123 commented Dec 6, 2020

After some more investigation, I see the problem is the size of the font in relation to the size of the drawing space for the word cloud. What I don't understand is how this .append("g") works in the svg. With the following code, the actual drawing area is reduced to 590 x 290 from the original 1200 x 600.

function prep_drawing(data) {

  // set the dimensions and margins of the graph
  var margin = {top: 10, right: 10, bottom: 10, left: 10},
      width = 1200 - margin.left - margin.right,
      height = 600 - margin.top - margin.bottom;
  
  // append the svg object to the body of the page
  var svg = d3.select("#wordly_wordcloud").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
    .append("g")
      .attr("transform",
            "translate(" + margin.left + "," + margin.top + ")");
  
  // Constructs a new cloud layout instance. It run an algorithm to find the position of words that suits your requirements
  // Wordcloud features that are different from one word to the other must be here
  var layout = d3.layout.cloud()
    .size([width, height])
    .words(data.map(function(d) { return {text: d.word, size:d.size, color:d.color, url:d.url}; }))
    .padding(5)        //space between words
    .rotate(function() { return 0; })
    .fontSize(function(d) { return parseFloat(d.size) * 150; })      // font size of words
    .on("end", draw);
  layout.start();
  
  function draw(words) {
      svg
        .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; })
            .style("fill", function(d) { return d.color; }) 
            .attr("text-anchor", "middle")
            .style("font-family", "Impact")
            .attr("transform", function(d) {
              return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
            })
            .append("a")
            .attr("xlink:href", function(d) { return d.url; })
            .attr("style", function(d) { return "fill:"+d.color+";"; })
            .text(function(d) { return d.text; });
    }
}

Screenshots from Chrome browser developer tools.
![Screenshot from 2020-12-06 12-18-05](https://user-images.githubusercontent.com/3002127/101290212-001ac380-37be-11eb-87a7-4824fc4d8991.png)
![Screenshot from 2020-12-06 12-17-42](https://user-images.githubusercontent.com/3002127/101290214-00b35a00-37be-11eb-96b9-b8e10ad30f75.png)




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants