Solved

Help make a chart-please

Posted on 2000-03-07
5
289 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now