[ic] Graphics plot in interchange ?

Davor Ocelic docelic at spinlocksolutions.com
Wed Sep 3 11:16:07 UTC 2008


On Wed, 03 Sep 2008 08:58:35 +0200
Marco Mescoli <m.mescoli at omnib.it> wrote:

> Dear list
> 
> any suggestion for perl modules to use inside IC enviro to develop 
> simple bar graph and display in html pages ?

Hey what up,

I used bargraph script for this, which is an extended wrapper over
gnuplot.

Bargraph is on
http://www.burningcutlery.com/derek/bargraph/

So here's how it works here:

1) My IC page prepares the config file for bargraph (it contains both
plot config lines and actual plot data):


[perl tables=tbl1]

    # Graphing

   # set plot options
   my $data =<<__EOD__;
=cluster;$years
=table
title=Inhabitants structure for years $years
xlabel=Age group
ylabel=Nr. of inhabitants
extraops=set size 2.1,1.8
legendx=2
legendy=2
=gridx
colors=light_green,med_blue,red,yellow,black,cyan,dark_green,magenta,light_blue,dark_blue,grey,grey1,grey2,grey3,grey4,grey5
__EOD__

# Add data to plot
    for (my $i=0; $i<@age_groups; $i++) {
        $data .= "\n$age_groups[$i] ";
        for (my $j=0;$j<@years;$j++) {
            $data .= "$ydata[$j][$i+1] ";
        }
        $data .= "\n";
    }

	return;



2) I save this $data to tmp file in CATALOG/tmp/$session/gnuplot:

    $Tag->write_relative_file(
        'tmp/' . $Tag->data(qw/session id/) . '/gnuplot',
        $data );

    $Scratch->{years} = join ',', @years;

    $ret

[/perl]


3) I display graph with:

 <img src=/images/[gnp years="[scratch years]" session="[data session id]"] />


[gnp] is a global usertag which calls bargraph script and passes it the 
generated config file. Bargraph calls gnuplot which produces the plot and saves
it to the corresponding image file and returns the file name:

UserTag gnp AddAttr
UserTag gnp Routine <<EOR
use Chart::Graph::Gnuplot qw(gnuplot);
$Chart::Graph::debug++;
sub {

    my $opt = shift;
    ( my $filename = $opt->{years} ) =~ s/ /,/g;
    my $session = $opt->{session};

    $filename = '/PATH-TO/graph-' . $filename;
    ( my $relative = $filename ) =~ s#.*/##;

    unless ( -r "$filename.png" ) {
        system("/usr/local/bin/bargraph.pl /PATH-TO/cat/tmp/$session/gnuplot > $filename.eps")
            and return "$@";
        system("convert $filename.eps $filename.png") and return "$@";
        system("chmod 755 $filename.eps $filename.png") and return "$@";
    }

    $relative . '.png'
}
EOR


You notice that gnp tag calls bargraph.pl, which comes from
bargraph package.

Also, you see that bargraph outputs eps in my case, which I convert
to png (using 'convert' command from imagemagick package), but you
can pass -png to bargraph to have png right away.

Cya,
-doc




More information about the interchange-users mailing list