Link to home
Start Free TrialLog in
Avatar of windfall
windfall

asked on

Help make a chart-please

I am using ActiveState Perl 5.005. IIS server ... NT 4.0 service pack 4
We wish to create "an on the fly" graph or chart....
I can generate an associated list (from a DBI - call) such as:

use DBI etc....

%Daily {
      00 => "4",
      01 => "3",
      02 => "0",
      03 => "1",
      04 => "6",
      05 => "3",
      06 => "12",
      07 => "14",
      08 => "16",
      09 => "20",
      10 => "21",
      11 => "23",
      12 => "24",
      13 => "25",
      14 => "26",
      15 => "27",
      16 => "25",
      17 => "22",
      18 => "10",
      19 => "9",
      20 => "4",
      21 => "4",
      22 => "8",
      23 => "1",
      };

How would I create a bar chart where the key values would be hours of the day (on the x axis) and the values would be vertical bars ?
such as:

      $query = new CGI;
      print $query->header("-nph=>1");
      print $query->start_html;

      print "I_WANT_THE_CHART_HERE.gif(or png)";
      print $query->end_html;



I have GD.pm and Image Majick installed but have had no success creating an image. I am also very confused as to ".PNG" format.
All of my intranet clients are Netscape 4.++.


A sample code snippet would be very appreciated.

Thanks
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Therei s a CPAN module calleed GD::Graph which you will need to install. It also requires GD 1.19 or above (1.23 recommended) and GD::Text::Align, parto of the GDTextUtils package. The following code is adaptedfrom the sample files in GD::Graph

use GD::Graph::bars;
use GD::Graph::colour;

print STDERR "Processing sample 1-1\n";

@daily =
(
    ["00","01","02","03"],  # .. up to 23
    [   4,  3,   0,   1],
);

$my_graph = new GD::Graph::bars();

$my_graph->set(
   x_label => 'Hours',
   y_label => 'Y label',
   title => 'Your Bar Chart',
   y_max_value => 30,
   y_tick_number => 2,
   y_label_skip => 1,
   
   # shadows
   bar_spacing => 8,
   shadow_depth => 4,
   shadowclr => 'dred',
)
or warn $my_graph->error;

# plot the graph
my $gd = $my_graph->plot(\@data);

# create a GIF file

open(IMG, '>file.gif') or die $!;
binmode IMG;
print IMG $gd->gif;
close IMG;

# create a PNG version

open(IMG, '>file.png') or die $!;
binmode IMG;
print IMG $gd->png;


To find put more about browser support for PNG format, take a look at http://www.cdrom.com/pub/png/pngapbr.html under the Netscape section.

Also, I forgot the final line in my code listing
close IMG;

use GD::Graph::bars;
use GD::Graph::colour;

print STDERR "Processing sample 1-1\n";

@daily =
(
    ["00","01","02","03"],  # .. up to 23
    [   4,  3,   0,   1],
);

$my_graph = new GD::Graph::bars();

$my_graph->set(
   x_label => 'Hours',
   y_label => 'Y label',
   title => 'Your Bar Chart',
   y_max_value => 30,
   y_tick_number => 2,
   y_label_skip => 1,
   
   # shadows
   bar_spacing => 8,
   shadow_depth => 4,
   shadowclr => 'dred',
)
or warn $my_graph->error;

# plot the graph
my $gd = $my_graph->plot(\@data);

# create a GIF file

open(IMG, '>file.gif') or die $!;
binmode IMG;
print IMG $gd->gif;
close IMG;

Avatar of windfall
windfall

ASKER

Thanks....
This code produces the following error:
Can't locate GD/Graph/bars.pm in @INC (@INC contains: C:\Perl\lib C:\Perl\site\5.00502\lib/MSWin32-x86-object C:\Perl\site\5.00502\lib C:\Perl\site\lib .) at d:\Resource\www\cgi-bin\Z_MAKECHART_SAMPLE.pl line 29.
BEGIN failed--compilation aborted at d:\Resource\www\cgi-bin\Z_MAKECHART_SAMPLE.pl line 29.
Would this have something to do with GD.pm only working with png format now?

I have looked for GD/Graph/bars  both locally and at activestate and have not had success.
Would you know where I went wrong looking ?


However, I did find  "Chart::Bars" and was able to make the following work:
It makes a .png image but my clients do not  have the appropriate plugin apparently because the image would not appear. I changed the format to .gif writing to the file, and then it worked (!?)

I am unsure how making the information in .png format but saving in .gif format works, but it does.

*****************************
#c:\perl\bin\perl
#she-bang line for cross-platform ease
#This code by:
#************
#Webmaster - Department Emergency Medicine
#************
#elvis911 currently operates on win-32 -- IIS 4.0
#Place explanatory info for particular script below
########################################
#This script is called:Z_MAKECHART_SAMPLE.pl
#This script purpose:make a bar chart
#This script reads from tables:
#This script writes to tables:
########################################
#it uses DBI
#it uses CGI
########################################
#These can be uncommented during development to assist in de-bugging
#use strict;
#use diagnostics;
########################################
#Standard DBI initial variable calls
use vars qw($query,$dbhndl,$sql,$sth,@row);
use DBI;
#call to escape unescape funny characters
use CGI qw/escape unescape/;
use Date::Manip;

use GD;
use Chart::Bars;

@daily = (['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23' ],
[26, 24, 20, 27, 20, 24, 12, 14, 16, 20, 21, 23, 24, 25, 26, 27, 25, 22, 10, 9, 4, 4, 8, 1 ] );


 $obj = Chart::Bars->new (600,400);
 
 $obj->set ('title' => 'Foo Bar');
 $obj->set ('x_label' => 'Hours');
 $obj->set ('y_label' => 'Patients');
 $obj->set ('legend' => 'bottom');
 $obj->set ('skip_x_ticks' => '2');
 $obj->set ('grid_lines' => 'true');
 
 
 
 #Here is an unusual line png to gif????...But it works
 $obj->png (">D:/Resource/www/images/daily.gif", \@daily);

$query = new CGI;
#send MIME and HTML headers
print $query->header("-nph=>1");
print $query->start_html(-title => 'CHART SAMPLE',-style=>{src=>'/style/overlib.css'}, -script=>{-language=>'javascript', -src=>'/javascript/staff_func.js'});
print"<DIV ID=\"overDiv\" STYLE=\"position\:absolute\; visibility\:hide\;\"></DIV>";
print"<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"/javascript/overlib.js\"></SCRIPT>";

print <<HTML;

<img SRC="../images/daily.gif" ALT="Daily patient population">


HTML
print $query->end_html;

***********************************

Could you tell me how I could display this image dynamically  rather than save to disk and then call it back to the generated page?

Thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial