S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find out how perlmonks.com is generating their horz bars in the poll results page: My primary PC CPU is a...

Key here is the fact that the bars are proportional to each other, are seperate (not one giant graphic), and there is no white space after the bars (as is appearent with the displayed stats at the end of each bar).

I run a number of poll databases at my site and I like this format as it allows for ease of page formatting.

How'd they do that?

TIA

======================
Sean Shrum
http://www.shrum.net

  • Comment on How'd they do that...proportional horz. bars in poll results...

Replies are listed 'Best First'.
Re: How'd they do that...proportional horz. bars in poll results...
by Caillte (Friar) on Apr 04, 2002 at 10:44 UTC

    This is more an HTML question but it works like this:

    my $results = { 'Category A' => 14, 'Category B' => 21 }; # Start your HTML here..... Then.... print qq(<table border="0">\n); foreach my $key (keys(%$results)) { print qq(<tr><td>$key</td><td><img src="bar.gif" width="$results-> +{$key}"></td></tr>); } print qq(</table>\n);

    bar.gif serves to give the colour to the result and the width is set by the number of entries for that result.

    Of course this is a rather crude example but it should sho the basics ;)

    Update: Added the offending semi-colon so it now compiles (Thanks tachyon). Moral, don't write something in a rush while preparing for a meeting ;)

    This page is intentionally left justified.

      This is of course the gist but if you want it to compile :o) and look right (ie height of bars), perhaps you might:

      my $results = { 'Category A' => 140, 'Category B' => 210 }; # Start your HTML here..... Then.... print qq(<table border="0">\n); foreach my $key (keys(%$results)) { print <<" HTML"; <br> <tr> <td> $key </td> <td> <img src="http://perlmonks.org/images/bluebox.gif" height=15 wid +th="$results->{$key}"> </td> <td> $results->{$key} width units </td> </tr> HTML } print "</table>\n";

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Perfect...I know how to sort on keys but how can I sort them according to the values (hi-lo)?

        TIA

        ======================
        Sean Shrum
        http://www.shrum.net

Re: How'd they do that...proportional horz. bars in poll results...
by Juerd (Abbot) on Apr 04, 2002 at 12:14 UTC

    Using no images speeds up :)

    Step one: gather results.

    use DBIx::Simple; my @items = DBIx::Simple -> connect(...) -> query('SELECT label, votes FROM polls WHERE poll_id = ?', ...) -> list;
    Step two: calculate total and percentages.
    my $total = 0; # Total $total += $_->[1] for @items; # Parts (e.g. 1/10) $_->[2] = $_->[1] / $total for @items; # Greatest (for width) my $highest = 0; $_->[2] > $highest and $highest = $_->[2] for @items;
    Step three: generate html.
    my $width = 300; # Width for 100% my $html = '<table>'; for (@items) { my $part = $_->[2] / $highest; my $pixels = int(.5 + $part * $width); my $percentage = int(l5 + $_->[2] * 100); $html .= "<tr><td>$_->[0]</td><td>" . "<div style='width: ${pixels}px; " . 'height: 30px; ' . 'background-color: silver; ' . 'text-align: right;\'>' . "$percentage%&nbsp;($_->[1])" . '</div></td></tr>'; } $html .= '</table>';
    Step four: display the generated html.
    template_foo($html);

    This will have the numbers in the bars, but changing that is a simple HTML change, which I leave up to you.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk