in reply to Seeking GD::Graph like goal-thermometer graph

I hope I get a merit badge for this. :-) This will take the current amount on the commandline and generate the thermometer. It could use some enhancements, but it should get you started.
#!/usr/bin/perl use warnings; use strict; use GD; use GD::Text::Align; my $current = shift; $current ||= 500; my $goal = 10000; my $image = new GD::Image(250,500); my $black = $image->colorAllocate(0,0,0); my $white = $image->colorAllocate(255,255,255); my $red = $image->colorAllocate(255,0,0); my $top = 20; my $bottom = 400; my $dif = $bottom - $top; my $left_text_margin = 65; #background, set tranparent if you want $image->filledRectangle( 0, 0, 250, 500, $white ); #$image->rectangle($x1,$y1,$x2,$y2,$color) $image->rectangle(40,20,60,425,$black); #$image->filledEllipse($cx,$cy,$width,$height,$color) $image->filledEllipse(50,450,75,75,$red); $image->filledRectangle(40,400,60,425,$red); my $text = new GD::Text::Align( $image, font => gdLargeFont, # font => './arial.ttf', text => $goal.' Our Goal', color => $black, valign => "center", halign => "left", ); #draw goal at top $text->draw($left_text_margin,$top); #draw start at bottom $text->set_text('0 Start'); $text->draw($left_text_margin,$bottom); show_current(); open( IMAGE, ">GD-thermometer.png") || die "Couldn't open file: $!\n"; binmode( IMAGE ); print IMAGE $image->png(); close IMAGE; sub show_current{ my $curpix = 400 - ($dif/$goal)* $current; #draw text level $text->set_text("$current Current"); $text->draw($left_text_margin,$curpix); $image->filledRectangle(40,$curpix ,60,425,$red); }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: Seeking GD::Graph like goal-thermometer graph
by hesco (Deacon) on Dec 18, 2006 at 19:57 UTC
    Zentara:

    Thanks, mate!

    That is certainly moving me in the right direction. Sorry I have no merit badges to offer you, but perhaps the last three XP I had to share today might help. I also opened a list of your posts and if the XP genies come back around before my browser runs my system out of memory again, I'd be happy to share a few more your way.

    My next step will be to make all the image sizes a function of the image's overall deminsions. As it is, when I tried to scale the image down to a usuable size, it cut off most of the content as outside it bounds. This is something I hope to make use of for years to come. Perhaps your code might serve as the basis for a new method / module as GD::Graph::thermometer, if the GD::Graph maintainer will have it. Otherwise I intend to maintain a personal version.

    Thanks again. This moves a data cruncher so close to delivering on a graphical requirement that I see light at the end of this tunnel.

    -- Hugh

    if( $lal && $lol ) { $life++; }
      Yeah, I was thinking the same thing about it being a module. I was tempted to make the dimensions all variables based on graphic dimension, but I was in a hurry. Have fun, I'll bet you could make a very useful module out of it, with lots of cool little features, like color selection, markers for various levels, transparencies, etc.

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum