Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Drawing graphs with perl

by Melanie (Acolyte)
on Apr 21, 2004 at 15:12 UTC ( #347028=perlquestion: print w/replies, xml ) Need Help??
Melanie has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone I need to draw some graphs from output I get from another program. I was hoping to do this in perl and investigated how the GD:Graphs module worked, but this doesnt seem to be able to create the graphs I want .. i.e. a scatter graph. I then came across something called DBD:graphs which is able to create scatter graphs ... but I guess you need to link to a database or something to use that? Does anyone know of anything that will do the job, without me having to go to all the trouble of setting up a database or something? Any help much appreciated Mel

Replies are listed 'Best First'.
Re: Drawing graphs with perl
by ChemBoy (Priest) on Apr 21, 2004 at 16:35 UTC

    I've never used DBD::Chart, but for scatterplots at my job we tend to use GnuPlot. We even use it through Perl, though the module we use (Chart::GnuPlot) doesn't seem to exist on CPAN. If you're working with the output from another application already, though, it's not clear that perl is doing more for you than just gluing things together, so GnuPlot might work on its own.

    The GnuPlot FAQ also points to PGPERL as an option, though I know nothing more about it than that, so I can't provide any more useful commentary about it.

    Good luck!

    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      Hi there Chemboy

      Thanks for your reply. I shall take a look :)

Re: Drawing graphs with perl
by blue_cowdawg (Monsignor) on Apr 21, 2004 at 15:44 UTC

        but I guess you need to link to a database or something to use that?

    DBD::Chart allows a DBD interface into creating charts with the GD libararies. It is dependant on:

    that taken directly from the DBD::Chart man page.

    As far as I know there no dependancy on a physical databse per se. The chart you are creating is treated as a database via the DBI and DBD:: interface.

      Hi, thanks for your reply. I think I understand that.
      To explain a little more about my problem .. although GD:GRAPH does plot linegraphs, it doesnt seem to be able to plot xy scatter graphs, which is why I hunted around for something else, but if anyone knows that GD:graph does actually do scattergraphs, it would be much appreciated.
      Anyway ... on the premise that I need to use DBD:Graph instead, I installed it, that seemed fine, and then tried one of the examples here ...
      Although I am not getting any error messages, I am not getting any graphical output either. Can anyone see where I might be going astray? Thanks again Mel

            Can anyone see where I might be going astray?

        I usually work better when I have code in front of me to poke holes at.... so I'll provide some. This is taken straight from the CPAN download's examples for DBD::Chart here is some sample code for a pie chart:

        #!/usr/bin/perl -w use DBI; use DBD::Chart; open(OUTF, ">pietest.html"); print OUTF "<html><body> <img src=simppie.png> <img src=updpie.png> <img src=delpie.png> </body></html>\n"; close OUTF; $dbh = DBI->connect('dbi:Chart:'); # # simple piechart # $dbh->do('CREATE TABLE pie (region CHAR(20), sales FLOAT)'); $sth = $dbh->prepare('INSERT INTO pie VALUES( ?, ?)'); $sth->execute('East', 2756.34); $sth->execute('Southeast', 3456.78); $sth->execute('Midwest', 1234.56); $sth->execute('Southwest', 4569.78); $sth->execute('Northwest', 33456.78); $rsth = $dbh->prepare( "SELECT PIECHART FROM pie WHERE WIDTH=400 AND HEIGHT=400 AND TITLE = \'Sales By Region\' AND COLOR=(red, green, blue, lyellow, lpurple) AND SIGNATURE=\'Copyright(C) 2001, GOWI Systems, Inc.\' AND BACKGROUND=lgray"); $rsth->execute; $rsth->bind_col(1, \$buf); $rsth->fetch; open(OUTF, '>simppie.png'); binmode OUTF; print OUTF $buf; close(OUTF); print "simppie.png OK\n"; $updsth = $dbh->prepare('UPDATE pie SET sales = ? WHERE region = \'Nor +thwest\' '); $updsth->bind_param(1, 12999.45); $updsth->execute; $rsth->execute; $rsth->bind_col(1, \$buf); $rsth->fetch; open(OUTF, '>updpie.png'); binmode OUTF; print OUTF $buf; close(OUTF); print "updpie.png OK\n"; $delsth = $dbh->prepare('delete from pie where region = ? '); $delsth->bind_param(1, 'Northwest'); $delsth->execute; $rsth->execute; $rsth->bind_col(1, \$buf); $rsth->fetch; open(OUTF, '>delpie.png'); binmode OUTF; print OUTF $buf; close(OUTF); print "delpie.png OK\n";

        I ran the code and did an "ls" in my CWD and lo and behold I found:

        [pberghol@cowdawg pietest]$ ls delpie.png pietest.html simppie.png updpie.png

        I bet if I open pietest.html I should see a pretty chart. I tried it and I did. If you look here you'll see what I mean.

Re: Drawing graphs with perl
by tachyon (Chancellor) on Apr 22, 2004 at 13:39 UTC

    Did you really read the docs -> GD::Graph::points is a scatter graph.



      Hi there
      Thanks to everyone that replied to this topic. I eventually got the DBD:Graphs module to work ... a little snippet of code for anyone hunting around for a way of creating scatterplots in the future will be included in my scatchpad (can anyone tell me how to do this? Thanks )
      By the way tachyon ... GD:Graph does plot line graphs certainly, but not xy scattergraphs, regardless of what the documentation said ;)
      Take care all

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://347028]
Approved by sgifford
[stevieb]: w00t! My Pi software(s) now have their own built-in SPI bus communication methods. About another week and I'll have everything completed!

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-01-20 22:42 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (179 votes). Check out past polls.