Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
XP is just a number
 
PerlMonks  

Re: How'd they do that...proportional horz. bars in poll results...

by Caillte (Friar)
on Apr 04, 2002 at 10:44 UTC ( #156618=note: print w/ replies, xml ) Need Help??


in reply to How'd they do that...proportional horz. bars in poll results...

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.


Comment on Re: How'd they do that...proportional horz. bars in poll results...
Download Code
Re: Re: How'd they do that...proportional horz. bars in poll results...
by tachyon (Chancellor) on Apr 04, 2002 at 11:32 UTC

    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

        You can sort by value by changing
        foreach my $key (keys(%$results)) {
        in the above code to:
        foreach my $key (sort {$results->{$b} <=> $results{$a}} keys %$results +) {
        If you have a ton of results, this may be inefficient, and may want to use a Schwartzian transform instead. But it sounds like maybe you don't have to worry about that.

        Typos, typos.... foreach my $key ( sort {$results->{$b} <=> $results->{$a}} keys %$results ) {

        Note that we access our hash $results->{'some_key'} as $results is a reference to the hash. So too the %$results notation to dereference $results and give us the hash. If you just have a plain hash %results your code will look like:

        foreach my $key ( sort {$results{$b} <=> $results{$a}} keys %results ) + {

        You will need to change the other -> derefereces too if that is the case.

        cheers

        tachyon

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://156618]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-04-20 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls