1

I am creating a word cloud that looks like this:

enter image description here

I have the English word "Tickets" fixed at 160px. How can I set a minimum size for the non-English words (32 for example)? I am using d3.js and Math.random().

var languages = [{"text":"Tickets"},{"text":"Fahrkarten"},{"text":"Entradas"},{"text":"Billets"},{"text":"Biglietti"},{"text":"チケット"},{"text":"티켓"},{"text":"Bilhetes"},{"text":"门票"},{"text":"門票"}];

var eases = [d3.easeExp,d3.easeSin,d3.easeLinear,d3.easeCubic];

var easesItem = eases[Math.floor(Math.random()*eases.length)];

console.log(easesItem);

var color = d3.scaleLinear()
        .domain([0,1,2,3,4,5,6,10,15,20,100])
        .range(["#000000", "#2F4F4F", "#708090", "#778899", "#808080", "#696969", "#A9A9A9", "#C0C0C0", "#D3D3D3", "#DCDCDC"]);

var layout = d3.layout.cloud()
    .size([1020, 1980])
    .words(languages)
    .padding(5)
    .rotate(function() { return ~~(Math.random() * 2) * 90; })
    .font("MoMA Sans Office")
    .fontSize(function(d) { if(d.text === 'Tickets') { return 160; } else { return 10 + Math.random() * 160; } })
    .on("end", draw);

layout.start();

NOTE: The screen changes every 5 seconds and the words appear in random order, color, and sizes, except for the English word "Ticket" which has a random color and order, but is always 160px. I am looking to avoid very small words.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
smoore4
  • 4,520
  • 3
  • 36
  • 55
  • Good comment. The screen changes every 5 seconds and the words appear in random order and sizes. I am looking to not have very small words basically. – smoore4 Sep 15 '18 at 16:19
  • [Math.random() | Getting a random number between two values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_number_between_two_values) – SamVK Sep 15 '18 at 16:24

1 Answers1

0

I assume this line sets the fontsize:

.fontSize(function(d) { if(d.text === 'Tickets') { return 160; } else { return 10 + Math.random() * 160; } })

and you want the minimum size to be 32:

.fontSize(function(d) { if(d.text === 'Tickets') { return 160; } else { return 32 + Math.random() * (160 - 32); } })

This should set the minimum fontsize to 32 and the maximum fontsize to 160.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57