About Kent Cowgill
Articles filed under...
abs ab_ripper andylester arms back baggyshorts bestpractices bike blog bugs bus calculator cardio catalyst cgi chart chest chinups code cpan datamodel dbi doctor documentation exercise exhaustion fitness flattire flat_tire google gps heart_rate helmet history houston html humor journal kate kenpo_x kettlebell knees lazy legs lisa lisanne maps math matthew michaelmckenna mom montreal motivation movie mysql oops orm P90X pain park patellar_tendonitis patrick perl phb photos physical_therapy plyometrics poor_gait presentation procrastination pullups pushups pyramid rabbits racecondition rant refactor rest ribs ride route running shoulders situps slides sore spike sql statistics syntax test testing textile timex training triceps ups versioncontrol video vim vimrc walk warren work workouts yapc yapcna2007 yoga youtube

A R C H I V E S

(5)
(15)
(16)
(25)
(3)
(4)
(2)
(4)
(11)
(1)
(1)
(3)
(2)
(2)
(10)
(5)
(2)
(3)
(4)
(9)
(21)
(3)
(3)
(1)
(6)
(4)
(1)
(4)
(3)
(2)
(1)

    Is Kent Cowgill Online?
    View Kent Cowgill's profile on LinkedIn
    Add to Technorati Favorites

    Recent Entries...

    On track so far...

    The week is about half way over, and so far I haven't skippe...

    My pants are on fire!

    I lied. Friday evening I didn't do anything. Saturday ...

    Playing catch-up

    It's been a while since I've updated my P90X progress. A ...

    Re: Yoga kicks my butt

    You should try a different yoga mat. I highly recommend the...

    Yoga kicks my butt

    It's time for a confession. I haven't done an entire Yoga...

    Recovery week comes in the nick of time

    Since it's been a few days, and I know I've rearranged my in...

    Early to Bed, Early to Rise

    So I'm not exceptionally healthier, wealthier, or wiser. Bu...

    Chest and Back, week 3

    Sure, the date that's listed for this post is 10/28, but it'...

    No Procrastination, Some Pain, Some Gain

    After much cajoling and self-flagellation, I convinced mysel...

    Odd Math

    I must be counting something wrong. Granted, I haven't be...

    weblog | `web·lôg -läg |
    noun
    Another term for BLOG
    ORIGIN 1990s: from web in the sense [World Wide Web] and log in the sense [regular record of incidents.]
    blog | bläg |
    noun
    A web site on which an individual or group of users produces an ongoing narrative.
    ORIGIN a shortening of WEBLOG.

    Logarithmic tag cloud

    Kent Cowgill

    It's been a while since I've posted anything technical. Possibly, upon reflection, because it seems that there's very little at my current job that sticks in my craw enough to feel the need to vent about it on my blog.

    So instead I'll talk about an itch of mine I recently felt the need to scratch.

    My listing of tags to the right was pretty bad. I came up with an algorithm to show tags with higher post counts in a bigger font than tags with lower post counts. But it was pretty sophomoric. In fact, I poked fun of my algorithm in talk I gave about photo processing in perl at this years YAPC::NA where I made a comment about this algorithm as a "highly complicated and sophisticated formula derived by minutes and minutes of trial and error".

    I reproduce it here for your amusement:

    
    
    <span style="font-size: [% ( tag.num + 80 ) / 8 %]px">
    
    
    
    

    But I had a flaw.

    Every time I noticed some of the tag fonts get too large due to me posting more and more posts with tags, I had to manually tweak the formula to give the font sizes a gentler slope - so the larger fonts didn't get too large.

    Google to the rescue. I googled for tag cloud font size algorithm and found a font size variation algorithm that looked promising.

    All I needed to do was use min and max from List::Util, and add this code to the backend before sending my data off to Template:

    
    
    use List::Util qw/min max/;
    
    my $tag_min = min map { $_->{ num } } @tags;
    my $tag_max = max map { $_->{ num } } @tags;
    
    for my $tag_row( @tags ){
      $tag_row->{ fontsize } = get_fontsize(
                                              $tag_row->{ num },
                                              $tag_min,
                                              $tag_max,
                                              $MIN_FONTSIZE,
                                              $MAX_FONTSIZE,
                                           );
    }
    
    
    

    ... given that @tags is an array of hashrefs containing tags and their respective counts.

    What's the get_fontsize look like?

    
    
    {
      my %font_cache;
      sub get_fontsize {
        my( $count, $min_count, $max_count, $min_size, $max_size ) = @_;
    
        return $font_cache{ $count } if $font_cache{ $count };
    
        my $weight = ( log( $count ) - log( $min_count ) )
                   / ( log( $max_count ) - log( $min_count ) );
    
        $font_cache{ $count } = $min_size
                              + round( ( $max_size - $min_size )
                                       * $weight );
      }
    }
    
    
    
    

    Which leaves a much more sensible hunk of code in the template file:

    
    
    <span class="tags" style="font-size: [% tag.fontsize %]px">
    
    
    
    
    Related Photos: None

    Main Page | Login

    Do you want to buy me ? Find more gift ideas at my wishlist