Here is how you might do it in Tk. Check out the Tk::Graph module for something better. I did a quick hack on the axis and you need to figure out a way to convert your variance number to a hex string between 0 and 65536. Also the circles I make are not on-center. (Hey, I have to leave some work for you. :-) ) But a nice save-to-postscript sub is shown.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $margin = 50;
my $offset = 20; #axis offset
my $x_max = 400;
my $y_max = 400;
my $mw = tkinit;
my $scanvas = $mw->Scrolled('Canvas',
-width => 500,
-height => 500,
-scrollregion => [-$margin,-$margin,
$x_max + $margin,
$y_max + $margin + $offset ],
-scrollbars => 'osoe',
-bg => 'black')->pack(-expand=>1, -fill=>'both');
&build_axis();
my $canvas = $scanvas->Subwidget('scrolled');
$canvas->Tk::bind("<Button-1>", [ \&print_xy, Ev('x'), Ev('y') ]);
sub print_xy {
my ($canv, $x, $y) = @_;
print "(x,y) = ", $canv->canvasx($x), ", ", $y_max - $canv->canvasy(
+$y) + $offset, "\n";
}
my $subframe = $mw->Frame(-background =>'gray50')->pack(-fill => 'x');
$subframe->Button(
-text => "Save",
-command => [sub {
$canvas->update;
my @capture=();
my ($x0,$y0,$x1,$y1)=$canvas->bbox('all');
@capture=('-x'=>$x0,'-y'=>$y0,-height=>$y1-$y0,-width=>$x1-$x
+0);
$canvas -> postscript(-colormode=>'color',
-file=>$0.'.ps',
-rotate=>90,
-width=>800,
-height=>500,
@capture);
}
] )->pack;
$subframe->Button(-text =>'Exit',
-background => 'hotpink',
-activebackground => 'red',
-command => sub{ exit }
)->pack(-side=>'left',-padx=>40);
while( <DATA> ){
chomp;
my ($x,$y,$var) = split / /,$_;
my $hexcolor = sprintf "%x", $var + 2000;
print $hexcolor,"\n";
my $circle=$canvas->createOval($x, $y, $x + 10, $y + 10,
-fill => '#'.$hexcolor.$hexcolor.$hexcolor,
);
}
MainLoop;
######################################################################
+#
sub build_axis{
# axis
my $xaxis = $scanvas->createLine( 0, $y_max + $offset, $x_max, $y_max
++ $offset,
-width => 1,
-fill => 'lightblue');
my $yaxis = $scanvas->createLine( 0, $y_max + $offset ,0,0,
-width => 1,
-fill => 'lightgreen');
# x axis ticks
my $tflag;
my $labflag;
my $min =0 ;
my $hour = 0;
my $minflag = 0;
my $hourflag = 0;
my $tlength;
my $color;
for(1..$x_max){
$tflag = 0;
$tlength = 5;
$color = 'white';
$labflag = 0;
if( ($_ % 10) == 0 ){ $tflag = 1 } #minutes are broken into 10 se
+c intervals
if( ($_ % 50) == 0 ){ $tlength = 15 ;
$color = 'yellow';
$min++;
$minflag = 1;
$labflag = 1;
}
if( ($_ % 100) == 0 ){ $tlength = 25;
$color = 'hotpink';
$hour++;
$hourflag = 1;
$labflag = 1;
$minflag = 0;
}
if( $tflag ){
$scanvas->createLine( $_, $y_max + $offset, $_, $y_max + $offset
+ + $tlength,
-width => 1,
-fill => $color);
if($labflag){
my $label;
if($minflag){ $label = 100*($hour + .5); }
else { if($hourflag){ $label = $hour * 100; } }
$scanvas->createText( $_, $y_max + $offset + 1.2*$tlength,
-text => $label,
-fill => $color,
-anchor => 'n',
);
}
}
}
# y axis ticks
my $uflag;
my $midflag;
my @array = reverse(0..$y_max );
for(@array){
my $num = $y_max - $_; #reverse normal axis
$tflag = 0;
$tlength = 5;
$color = 'white';
$uflag = 0;
$midflag = 0;
$labflag = 0;
if( ($num % 10) == 0 ){ $tflag = 1 }
if( ($num % 50) == 0 ){ $tlength = 10 ;
$color = 'yellow';
$midflag = 1;
$labflag = 1;
}
if( ($num % 100) == 0 ){ $tlength = 20;
$color = 'hotpink';
$uflag = 1;
$labflag = 1;
$midflag = 0;
}
if( $tflag ){
$scanvas->createLine( 0 - $tlength, $num + $offset, 0, $num + $o
+ffset,
-width => 1,
-fill => $color);
if($labflag){
my $label;
if($midflag){ $label = $num; }
if($uflag){ $label = $num; }
$scanvas->createText( -20 , $y_max + $offset - $num ,
-text => $label,
-fill => $color,
-anchor => 'e',
);
}
}
}
$scanvas->xview('moveto',0);
$scanvas->yview('moveto',1);
}
__DATA__
10 100 45
10 200 50
30 300 60
400 400 500
|