How can I calculate the font size of tags in a tag cloud?

4

I have a tag cloud and want to change the font size of each tag as per its usage.

I need to have a minimum size and a maximum size.

What would be the formula for calculating the size of the tags?

    
asked by anonymous 24.12.2013 / 19:00

1 answer

6

I have created some examples to clarify the Delan response .

Basically the formula is:

(tagAtual.Usos / maiorNumeroDeUsos) * (fonteMaxima - fonteMinima) + fonteMinima

Example in Javascript

var tags =
[
    { Name: "c#", Uses: 100 },
    { Name: ".net", Uses: 75 },
    { Name: "typescript", Uses: 50 },
    { Name: "lua", Uses: 50 },
    { Name: "javascript", Uses: 25 },
    { Name: "jquery", Uses: 1 },
    { Name: "c++", Uses: 0 },
];

var max = 100; // Deve ser computado
var min = 0;   // Deve ser computado

var fontMin = 10;
var fontMax = 20;

for (var i in tags)
{
    var tag = tags[i];

    var size = tag.Uses == min ? fontMin
        : (tag.Uses / max) * (fontMax - fontMin) + fontMin;
}

Output:

c#             font-size: 20
.net           font-size: 17.5
typescript     font-size: 15
lua            font-size: 15
javascript     font-size: 12.5
jquery         font-size: 10
c++            font-size: 10

C # example

var tags = new List<Tag>
{
    new Tag { Name = "c#", Uses = 100 },
    new Tag { Name = ".net", Uses = 75 },
    new Tag { Name = "typescript", Uses = 50 },
    new Tag { Name = "lua", Uses = 50 },
    new Tag { Name = "javascript", Uses = 25 },
    new Tag { Name = "jquery", Uses = 5 },
    new Tag { Name = "c++", Uses = 5 },
};

int max = tags.Max(o => o.Uses);
int min = tags.Min(o => o.Uses);

double fontMax = 20;
double fontMin = 10;

foreach (var tag in tags)
{
    double size = tag.Uses == min ? fontMin
        : (tag.Uses / (double)max) * (fontMax - fontMin) + fontMin;
}

Output:

c#             font-size: 20
.net           font-size: 17.5
typescript     font-size: 15
lua            font-size: 15
javascript     font-size: 12.5
jquery         font-size: 10
c++            font-size: 10
    
25.12.2013 / 18:37