Solved

Help make a chart-please

Posted on 2000-03-07
5
308 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:windfall
  • 4
5 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2593481
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;


0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2593538
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;

0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2598867
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;

0
 
LVL 2

Author Comment

by:windfall
ID: 2601764
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
0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 300 total points
ID: 2602592
You can find the module at http://www.perl.com/CPAN-local/modules/by-module/GD/GDGraph-1.24.tar.gz . My understanding was that browsers assemble page form componets such a gif files, so I am not sure if it is possible to create the image dynamically.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cpan issue 1 63
How to strip .csv from file name 9 81
Having a problem with ActiveState Perl on 64-bit Windows 7 3 72
Perl script to delete older files 6 86
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question