in reply to How'd they do that...proportional horz. bars in poll results...
Using no images speeds up :)
Step one: gather results.
Step two: calculate total and percentages.use DBIx::Simple; my @items = DBIx::Simple -> connect(...) -> query('SELECT label, votes FROM polls WHERE poll_id = ?', ...) -> list;
Step three: generate html.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 four: display the generated 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% ($_->[1])" . '</div></td></tr>'; } $html .= '</table>';
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
|
|---|