Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Generating images from a text file

by Anonymous Monk
on Feb 16, 2005 at 08:42 UTC ( #431469=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a text file on my domain that I would like to read in and create a bar chart showing all items listed with graphical bars. Reading in the file I should be able to do (I made the script to hashin and hashout the file), however, the only method I know to generate images is with the GD lib's and the provider will not install it, nor any other lib's. All I can assume is, is they have the standard perl lib's installed (I can't get them to tell me what's actually there for my use), and I don't even know what version they have there (make me wonder why I even stay with them)....

The format of the file looks like this:
thisone 3
thatone 1
something 7
else 4

What I'm wondering is, is it possible to generate graphical bars (or any graphics for that matter) using the standard perl distro, and if so, how (also, no SSI ability, so its a one shot CGI page)?


Replies are listed 'Best First'.
Re: Generating images from a text file
by borisz (Canon) on Feb 16, 2005 at 08:56 UTC
    Try to install the modules by yourself.
    Have a look at Installing modules without a rootshell. Otherwise draw simpler graphs ;-)
    my ( $m, %h ) = 0; while (<DATA>) { my ( $k, $v ) = split / /, $_, 2; $m = length $k if $m < length $k; $h{$k} = $v; } for ( sort keys %h ) { printf "%${m}s %s\n", $_, 'x' x $h{$_}; } __DATA__ thisone 3 thatone 1 something 7 else 4 __OUTPUT__ else xxxx something xxxxxxx thatone x thisone xxx
      Thanks, not really the reply I was hoping for (was hoping perl carried a method of making web images internally and that the other libs were just addins to create other types of images or make creation simpler).

      In any event, I learned something....
Re: Generating images from a text file
by Taulmarill (Deacon) on Feb 16, 2005 at 09:29 UTC
    if you don't want to use modules, just don't use real graphics. imho there is no perl core module which produces images. but you can do simple bar diagrams by using css blocks or html tables.
      ditto ... for simple bar charts, ... you can do a lot with <div>, playing with background colors, images, nesting them, etc.
Re: Generating images from a text file
by g0n (Priest) on Feb 16, 2005 at 09:31 UTC
    if you're really stuck, you could do something primitive like this:(updated html version below)

    use strict; open (IFILE,"testfile.txt") or die $!; my %values; my $maxvalue; while (<IFILE>) { my ($tag,$value) = split /\s/,$_; $values{$tag} = $value; if ($value > $maxvalue){$maxvalue = $value} } for (my $counter=$maxvalue;$counter>0;$counter--) { foreach (keys %values) { if ($values{$_} >= $counter) { print "#"; } print "\t\t"; } print "\n"; } foreach (keys %values) { print "$_\t"; if (length($_) <8){print "\t"} } print "\n";

    Note: tabbing of the column labels may be flakey

    It just prints a very simple bar chart with # symbols

    Update: After the excellent suggestion from TaulMarill of using html tags, you might like to try this alternative version:

    use strict; open (IFILE,"testfile.txt") or die $!; my %values; my $maxvalue; while (<IFILE>) { my ($tag,$value) = split /\s/,$_; $values{$tag} = $value; if ($value > $maxvalue){$maxvalue = $value} } print "<table>"; for (my $counter=$maxvalue;$counter>0;$counter--) { print "<tr>"; foreach (keys %values) { if ($values{$_} >= $counter) { print "<td bgcolor=\"red\" fontcolor=\"red\">< +pre> </pre></td>"; } else { print "<td></td>"; } } print "</tr>"; } print "<tr>"; foreach (keys %values) { print "<td>$_</td>"; } print "</tr></table>";

    I think the results look quite nice :-)

Re: Generating images from a text file
by neniro (Priest) on Feb 16, 2005 at 10:42 UTC
Re: Generating images from a text file
by Anonymous Monk on Feb 16, 2005 at 11:51 UTC
    Guess I should have refreshed before replying....

    borisz, thanks, see the reply above.
    Taulmarill and g0n, interesting. I hadn't thought of that. That is really a unique way of acheiving the goal. I'll have to look deaper into this. The effective way to make it work even better (for me atleast) would be to generate based on the average hits before actually making the table and cells, to ensure that the actual display stays on the page, and in the desired display area.

    I tried your 2nd script, g0n, the results are very interesting, it does cause allot of generation though. In the test case I ran, the file had this:
    Highest: 42
    Lowest: 1
    Total counted: 31

    The result ended up being rather large, and took several seconds to generate. But in the end, does offer a way to generate something that represents a bar chart.

    I have to play around with the html/css methods and see what I come up with. Thanks allot for the ideas!
Re: Generating images from a text file
by Anonymous Monk on Feb 16, 2005 at 16:26 UTC
    Thanks again guys,
    I tried and tried the table variant, however, tables ended up being very unpredictable in different browsers. I tried setting the 'graph' to 50 1px cells, then looping though the hash and sending the cell with color until the 'bar graph' should end. The results were less then pritty....

    I finally broke down, and made a 1x10px image, then, rather then sending table cells, send img html code for the bar 'length'. The result, looks considerably better, although the code is rather scrappy (IMHO). I doubt anyone here would be interested in it, but if someone really wants it..... The graph generated is for a site hit counter, and generates a bar graph that is flexable enough to make the output look better then the code.

    Before asking, take a look at the code in action, at (click the 1234 icon at the bottom), if you still want it, then use the email links there, and I'll send it over. Don't know if pasting my sloppy code here with you pros is a good idea. :-P

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://431469]
Approved by grinder
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2017-12-18 09:25 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (475 votes). Check out past polls.