Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

What would you guys recommend for generating graphic-based output?

by FrankRizzo (Acolyte)
on Jan 25, 2005 at 16:49 UTC ( #424934=perlquestion: print w/replies, xml ) Need Help??

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

I have been tasked with generating a png file containing a grid-like representation of data from a database. I have all the code to retrieve the data, but am lost when it comes to picking a package that will generate the grid for me. I saw TK:TixGrid, but it seems that the whole TK family is more tailored towards GUI, and user front-ends that for report output. As for requirements, I need to be able to put a title at the top of the grid, and I need the ability to "inverse" values that are "out of range". Other than that, it just needs to look like Excel output, without the windows platform. Any ideas?

Retitled by davido from 'What would you guys recommend?'.

  • Comment on What would you guys recommend for generating graphic-based output?

Replies are listed 'Best First'.
Re: What would you guys recommend for generating graphic-based output?
by davido (Cardinal) on Jan 25, 2005 at 16:59 UTC

    Well, there is the GD family of modules, which I have used. And there's the Chart family, which I haven't used. GD is farily complete, a little complicated at first, but pretty full-featured. You might like it.


    Dave

Re: What would you guys recommend for generating graphic-based output?
by g0n (Priest) on Jan 25, 2005 at 17:02 UTC
    perl-GD would be the first thing that occurred to me, especially since you want png.

    It supports png, and you can mix text & graphics to produce the grid.

    It wouldn't be just a case of sending your tabulated data to it and getting back what you want, it will require some coding, but nothing madly complicated.

    I don't know of anything else offhand that fulfils your criteria.

Re: What would you guys recommend for generating graphic-based output?
by borisz (Canon) on Jan 25, 2005 at 17:35 UTC
Re: What would you guys recommend for generating graphic-based output?
by sleepingsquirrel (Hermit) on Jan 25, 2005 at 17:18 UTC
    Not a pure perl solution, but you can do a lot of interesting graphics stuff (especially when you want to incorporate text) with the PostScript language and use ghostscript to output your PNG bitmap. For a good postscript tutorial, grab a copy of the Blue Book(PDF).


    -- All code is 100% tested and functional unless otherwise noted.
Re: What would you guys recommend for generating graphic-based output?
by zentara (Archbishop) on Jan 25, 2005 at 19:00 UTC
    If you wanted to reconsider Tk, maybe the Tk::Canvas would do the trick for you. You can easily setup an invisible grid on the canvas, then put whatever you want at each point, anchoring it to "north' or 'center', or whatever. You can mix Text and small graphics, and even draw outlines around them for emphasis. GD will do that, but it's alot trickier with GD, getting colors and fonts just right.

    Then you can take a png snaphot of the Canvas with Tk::WinPhoto, or export it to postscript.

    I think you will find Tk has more flexibility than GD, but it really depends on what your needs are.


    I'm not really a human, but I play one on earth. flash japh
      OK, the more important question, does anyone have any links to examples? I'm a "string in, string out" kinda guy, trying to get by with as little graphics work as I possibly can, as this is the LAST ITEM on the project plan, and I'm ready to be done! Thanks Guys! Frank
        Needs ghostscript installed, produces a file "tmp.png".
        #!/usr/bin/perl -w %stats = (Foo => 14, Bar => 10, Baz => 5, Box => 8); $title = "Widgets, Inc"; $ps_preamble = <<PS_END; %!PS-Adobe-2.0 /big /Helvetica findfont 20 scalefont def /tiny /Helvetica findfont 9 scalefont def /box { newpath moveto lineto lineto lineto closepath } def 0 setgray big setfont 612 ($title) stringwidth pop sub 2 div 648 moveto ($title) show 1 setlinewidth 72 72 72 720 540 720 540 72 box stroke tiny setfont PS_END open (GS, "|gs -dSAFER -dBATCH -dNOPAUSE -q -r80 -sDEVICE=png16m". " -sOutputFile=tmp.png - ") or die "Can't start Ghostscript" +; print GS $ps_preamble; my $n = keys %stats; my $w = 396/$n; my $i=0; for (keys %stats) { my $x = $i*$w+126; my $y = $stats{$_} * 30 + 108; print GS "$x 90 moveto\n($_) show\n"; print GS "0.5 0 0.8 setrgbcolor\n"; print GS "$x 108 ", "$x $y ", $x+$w/2, " $y ", $x+$w/2, " 108 box fill\n"; print GS "0 setgray\n"; $i++; } print GS "showpage\n"; close(GS);


        -- All code is 100% tested and functional unless otherwise noted.
        I'll post a little tk snippet later today, which exports a canvas full of text to png. (I'll have to make one up :-) ) Also I just noticed a new version of "pngwriter" was just announced on freshmeat.

        About: The PNGwriter library, which requires libpng, allows you to plot to a 48-bit PNG file, saving it directly to disk. Plotting is as easy as specifying the red, green, and blue values and the x, y coordinates of the pixel. It includes functions for plotting simple geometric shapes (circle, rect, line), plotting text (UTF-8 support for Asian languages), reading the colour of a pixel, reading in a whole PNG file (great for image analysis), plotting and reading in HSV colourspace, and many others that might come in handy.

        pngwriter


        I'm not really a human, but I play one on earth. flash japh
Re: What would you guys recommend for generating graphic-based output?
by zentara (Archbishop) on Jan 26, 2005 at 13:10 UTC
    Here is a simple Tk example. I've just put the row-col numbers in the cells, but you could load $cells{$row}{$col}{'data'} with your spreadsheet output and use that for the text. It is also possible to change fonts, cell colors, text color, etc. When you click the png-output button, be patient, it may take a second or 2.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::WinPhoto; use Tk::PNG; my $mw = tkinit; my $width = 500; my $height = 700; my $canv = $mw->Canvas(width => $width, height =>$height)->pack(); #set cell size adjustment my $x = 50; my $y = 20; my $rows = int( $height / $y + 1 ) - 1; #make it 0 based my $cols = int( $width / $x + 1 ) - 1; print "rows->$rows cols->$cols\n"; my %cells; foreach my $row ( 0 .. $rows ) { foreach my $col ( 0 .. $cols ) { $cells{$row}{$col}{'rect'} = $canv->createRectangle( $col * $x, $row * $y, $col * $x + $x, $row * $y + $y, #-width => 1, -outline => 'white', ); $cells{$row}{$col}{'text'} = $canv->createText( $col * $x + $x/2 , $row * $y + $y/2, -text => "$row - $col", -anchor => 'center', -fill => 'black', ); } } my $canvbutton = $mw->Button(-text=>'Canvas Capture', -command => \&canv_capture, )->pack; MainLoop; ########################################################## sub canv_capture{ my $image = $mw->Photo(-format => 'Window', -data => oct($canv->id) ); my $pathname = './canvas.'.time.'.png'; $image->write($pathname, -format => 'PNG'); } ##############################################################

    I'm not really a human, but I play one on earth. flash japh
Re: What would you guys recommend for generating graphic-based output?
by individual61 (Initiate) on Jan 26, 2005 at 22:44 UTC
    Hi guys,

    I'm the author of PNGwriter, mentioned above (site). PNGwriter is well-suited for precisely the task you're interested in. However, when you say you need to represent data on a grid, do you mean alter a pixel's colour given each data entry? Or place text (your data) in a grid of lines? In any case, PNGwriter can help you do both, as it lets you alter individual pixel's RGB values (as in BASIC) and also allows you to plot any TrueType font. All you need is libpng, Freetype2 for the text and away you go. If you're interested, I'd be more than willing to answer any questions you may have.

    As a side note, a good friend of mine and Perl developer was helping me create a Perl interface for PNGwriter, so all the good Perl folks could access this C++ library straight from Perl code, should they want to. Unfortunately he's been swamped by work (and he's a self-employed programmer/admin, so what he codes is what he eats :-) ) and has been unable to advance this project beyond what I estimate to be the 50-70% mark. We have CPAN namespace approval (did I say that right?) .

    Might anyone here be interested in cooperating?

    Cheers,

    Paul Blackburn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://424934]
Approved by holli
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-10-05 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (25 votes). Check out past polls.

    Notices?