Quicksilver has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use DBI; my $dbh = DBI->connect('dbi:mysql:milton:localhost', 'user', 'pword'); my $sth = $dbh->prepare ('SELECT word, occurrences FROM statistic ORDE +R BY occurrences DESC LIMIT 200'); my %tag; my ($word, $count); #load in tag file - do this from db $sth->execute; while (my($word, $count) = $sth->fetchrow_array) { $tag{$word} = $count; } $sth->finish; $dbh->disconnect(); my $useLogCurve = 1; my $minFontSize = 10; my $maxFontSize = 36; my $fontRange = $maxFontSize - $minFontSize; #determining the count my $maxTagCnt = 0; my $minTagCnt = 1000000; foreach $word (%tag) { $maxTagCnt = $tag{word} if $tag{word} lt $maxTagCnt; $minTagCnt = $tag{word} if $tag{word} gt $minTagCnt; } my $minLog = log($minTagCnt); my $maxLog = log($maxTagCnt); my $logRange = $maxLog - $minLog; $logRange = 1 if ($maxLog == $minLog); sub DetermineFontSize ($) { my ($tagCnt) = @_; my $cntRatio; if ($useLogCurve) { $cntRatio = (log($tagCnt) - $minLog)/$logRange; } my $fsize = $minFontSize + $fontRange * $cntRatio; return $fsize; } #output tag cloud print "Content-type: text/html\n"; print<<EOT; <html> <head> <link href="../../css/tagcloud.css" rel="stylesheet" type="text/css" +> </head> <body> <div class=\"cdiv\"> <p class=\"cbox\"> EOT #output the keys #still needs to have on onclick event to get the bigrams or to search foreach $word (sort keys %tag) { my $fsize = DetermineFontSize($tag{$word}); printf "<div name=\"$word\" style=\"font-size:%dpx;\">%s", $fsize, $ +word, "</div>"; } #output end of tag file print <<EOT; </p> </div> </body> </html> EOT __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Setting up a log to determine font size
by moritz (Cardinal) on May 22, 2008 at 09:22 UTC | |
by Anonymous Monk on May 22, 2008 at 10:33 UTC | |
by moritz (Cardinal) on May 22, 2008 at 10:55 UTC |