Reputation: 7664
i dont know if this is the right place to ask this question but here it goes I am primarily working in php but an ans in any language will do.
i am making a tag cloud for a website. Something like this
lets say i have an array of numbers
array(54, 67, 68, 72, 98, 103, 112, 136, 169, 200, 201)
this numbers represent how many times a word has been searched for in the website.
in the tag cloud i want to display popular keywords in a bigger font size. So i want to use this numbers as a link to the font size
So my question is, how can i reduce these numbers relatively to each other so that all of them occur with the rage on 10 to 20. Where 10 and 20 will be the font size of the tags
I hope i was clear enough.
Upvotes: 0
Views: 402
Reputation: 10781
Here is the fiddle.
<?php
$values = array(54, 67, 68, 72, 98, 103, 112, 136, 169, 200, 201);
$max = max($values);
$min = min($values);
$fontMin = 10;
$fontMax = 20;
$valueDiff = $max - $min;
$fontDiff = $fontMax - $fontMin;
$incrementEvery = round($valueDiff / $fontDiff);
foreach ($values as $value) {
$actualFont = round(($value - $min) / $incrementEvery) + $fontMin;
echo "$value: $actualFont \n";
}
Results in:
54: 10
67: 11
68: 11
72: 11
98: 13
103: 13
112: 14
136: 15
169: 18
200: 20
201: 20
The one-liner is:
round(($value - $min) / round(($max - $min) / ($fontMax - $fontMin))) + $fontMin;
Upvotes: 3
Reputation: 1376
You can sort the array using the php function sort() [http://php.net/manual/en/function.sort.php] to get the difference between the highest and lowest numbers. For example, in your list 201 is the highest and 54 is the lowest giving you a difference of 147. Since you want between 10 and 20 inclusively, you have 10 different font sizes to use (or more if you use decimals) so you can divide 147 by 10 to determine how many items over the minimum you will need to increase the font-size by 1 point.
So, 54 would be 10 point font and 68 would be 11 point font (54 + 14.7 = 68.7). If you want to use whole numbers, simply use PHP floor().
Hope this gives you a good idea of how to solve the problem.
Upvotes: 1