-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plot.pm
1052 lines (832 loc) · 36.5 KB
/
Plot.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#=====================================================================#
# Plot -- Front end to GD.pm for plotting two dimensional data #
# by Sanford Morton <[email protected]> #
#=====================================================================#
# Changes:
# v 0.0 - 08 March 1998
# first version
# v 0.01 - 09 March 1998;
# - _getOM reports >= max instead of >;
# - fixed bug in setData() data check
# v 0.02 - 10 March 1998;
# - changed error handling in setData() (public method) which
# now returns undef on success and sets $self->error ;
# - changed legend to title (public method)
# - adjusted horizontal tick labels up a bit
# v 0.03 - 15 March 1998
# - added colors and dashed line options to dataset graph style
# - added option to pass dataset as two arrays (@xdata, @ydata)
# - added hack for case om == max
# v 0.04 - 15 March 1998
# - added general purpose setGraphOptions()
# v 0.05 - 18 March 1998
# _ added synopsis to pod
# - added getBounds()
# - Hor axis label is set below and right centered or justified.
# - additional vertical offset if title is present; larger font
# v 0.06 - 22 March 1998
# - removed title, offset and axis label methods in favor of
# setGraphOptions()
# - added getBounds()
# v 0.07 - 29 May 1998
# - finally put into standard h2xs form
# - added check for tick step too small
# - changed data validity check to permit scientific notation
# but this invites awful looking tick labels
# v 0.08 - 15 Dec 1998
# - added access to GD object: getGDobject() and data2pxl()
# v 0.09 - 26 July 1999
# - added custom tick labels: xTickLabels, yTickLabels
# - added binmode() to install test and demo script
# v 0.10 - 22 May 2000
# - added @_image_types and image_type() to use gif, jpeg or png
# according to local version of GD; modified draw() and _init()
# v 0.11 - 04 April 2001
# - fixed bug in draw() to enable jpeg's
package Plot;
$Plot::VERSION = '0.11';
use GD;
use strict;
#==================#
# class variables #
#==================#
# list of image types supported by GD, currently jpeg, png or gif,
# depending on GD version; initialized in _init()
my @_image_types = ();
#==================#
# public methods #
#==================#
# usage: $plot = new Plot(); # default 400 by 300 pixels or
# $plot = new Plot(640, 480);
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->_init (@_);
return $self;
}
sub setData {
my $self = shift;
my ($arrayref, $style) = @_;
my ($i);
# check whether array is unbalanced
if ($#$arrayref % 2 == 0) {
$self->{'_errorMessage'} = "The dataset does not contain an equal number of x and y values.";
return 0;
}
# check whether data are numeric
for ($i=0; $i<=$#$arrayref; $i++) {
if ($$arrayref[$i] !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) {
# if ($$arrayref1[$i] =~ /[^\d\.-]/) {
$self->{'_errorMessage'} = "The data element $$arrayref[$i] is non-numeric.";
return 0;
}
}
# record the dataset
my $label = ++$self->{'_numDataSets'};
$self->{'_data'}->{$label} = $arrayref;
$self->{'_dataStyle'}->{$label} = ($style ? $style : 'linespoints');
$self->{'_validMinMax'} = 0; # invalidate any prior min-max calculations
return $label;
}
sub error {
my $self = shift;
return $self->{'_errorMessage'};
}
sub setGraphOptions {
my $self = shift;
my %hash = @_;
for (keys (%hash)) {
$self->{"_$_"} = $hash{$_};
# check tick labels for non-numeric positions
if (/^(x|y)TickLabels$/) {
my $xory = $1;
foreach ( keys %{$hash{$_}} ) {
unless (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) {
$self->{'_errorMessage'} =
"The $xory axis tick label position $_ is non-numeric.";
return 0;
}
}
}
}
return 1;
}
sub getBounds {
my $self = shift;
$self->_getMinMax() unless $self->{'_validMinMax'};
return ($self->{'_xmin'}, $self->{'_ymin'},
$self->{'_xmax'}, $self->{'_ymax'});
}
sub image_type {
return (wantarray ? @_image_types : $_image_types[0]);
}
sub draw {
my $self = shift;
# draw stuff in the GD object
$self->_getMinMax() unless $self->{'_validMinMax'};
$self->_drawTitle() if $self->{'_title'}; # vert offset may be increased
$self->_draw_grid();
$self->{_im}->setThickness(2);
$self->_drawData();
$self->{_im}->setThickness(1);
$self->_drawAxes();
# construct the image and return it.
# $_image_types[0] is the supported GD format, gif or png or jpeg
# Damien says no good way around this temp variable
if ($_[0]) { # image type argument
unless ( $self->{'_im'}->can($_[0]) ) {
$self->{'_errorMessage'} = "The image format $_[0] is not supported by this version $GD::VERSION of GD";
return undef;
}
my $type = $_[0]; # forgot these in ver 0.10
return $self->{'_im'}->$type(); # an embarrassment
} else {
my $type = $_image_types[0];
return $self->{'_im'}->$type();
}
}
sub getGDobject {
my $self = shift;
if (wantarray) {
return ( $self->{'_im'},
$self->{'_black'}, $self->{'_white'},
$self->{'_red'}, $self->{'_green'}, $self->{'_blue'},
);
} else {
return $self->{'_im'};
}
}
sub data2pxl {
my $self = shift;
my @data = @_;
# calculate required translation parameters
$self->_getMinMax() unless $self->{'_validMinMax'};
return $self->_data2pxl (@data);
}
#===================#
# private methods #
#===================#
# initialization
# this contains a record of all private data except class variables, up top
sub _init {
my $self = shift;
# create an image object
if ($#_ == 1) {
$self->{'_im'} = new GD::Image($_[0], $_[1]);
$self->{'_imx'} = $_[0];
$self->{'_imy'} = $_[1];
}
else {
$self->{'_im'} = new GD::Image(400,300);
$self->{'_imx'} = 400;
$self->{'_imy'} = 300;
}
# find format(s) supported by GD
unless (@_image_types) {
for ( qw(png gif jpeg) ) {
push @_image_types, $_ if $self->{'_im'}->can($_);
}
}
# set graph offset; graph will be centered this many pixels within image
$self->{'_horGraphOffset'} = 50;
$self->{'_vertGraphOffset'} = 50;
# create an empty hash for the datsets
# data sets and their styles are hashes whose keys are 1 ... _numDataSets
# and values are refs to flat data arrays or style strings, respectively
$self->{'_data'} = {};
$self->{'_dataStyle'} = {};
$self->{'_numDataSets'} = 0;
# calculated by _getMinMax and used in translating _data2pxl()
$self->{'_xmin'} = 0; $self->{'_xmax'} = 0; # among all datasets
$self->{'_ymin'} = 0; $self->{'_ymax'} = 0;
$self->{'_xslope'} = 0; $self->{'_yslope'} = 0; # for _data2pxl()
$self->{'_ax'} = 0; $self->{'_ay'} = 0;
$self->{"_omx"} = 0; $self->{"_omy"} = 0; # for axis ticks
$self->{'_validMinMax'} = 0; # last calculated min and max still valid
# initialize text
($self->{'_horAxisLabel'}, $self->{'_vertAxisLabel'}) = ('','');
$self->{'_title'} = '';
$self->{'_errorMessage'} = '';
# initialize custom tick labels
($self->{'_xTickLabels'}, $self->{'_yTickLabels'}) = (0,0);
# allocate some colors
$self->{'_white'} = $self->{'_im'}->colorAllocate( 45, 45, 55 );
$self->{'_gray'} = $self->{'_im'}->colorAllocate(254,254,254);
$self->{'_medium'} = $self->{'_im'}->colorAllocate(64,64,64);
$self->{'_black'} = $self->{'_im'}->colorAllocate(0,0,0);
$self->{'_red'} = $self->{'_im'}->colorAllocate(255,0,0);
$self->{'_blue'} = $self->{'_im'}->colorAllocate(0,0,255);
$self->{'_green'} = $self->{'_im'}->colorAllocate(0,255,0);
# make the background transparent and interlaced
#$self->{'_im'}->transparent($self->{'_white'}) if $_[3];
$self->{'_im'}->interlaced('true');
# Put a black frame around the picture
$self->{'_im'}->rectangle( 0, 0,
$self->{'_imx'}-1, $self->{'_imy'}-1,
$self->{'_black'});
# undocumented: in script, use as $plotObject->{'_debugging'} = 1;
$self->{'_debugging'} = 0;
}
# sets min and max values of all data (_xmin, _ymin, _xmax, _ymax);
# also sets _xslope, _yslope, _ax and _ay used in _data2pxl;
# usage: $self->_getMinMax
sub _getMinMax {
my $self = shift;
my ($i, $arrayref);
my ($xmin, $ymin, $xmax, $ymax);
# if no data, set arbitrary bounds
($xmin, $ymin, $xmax, $ymax) = (0,0,1,1) unless keys %{$self->{'_data'}} > 0;
# initialize to zero
$xmin = $xmax = $ymin = $ymax = 0;
# # or to first data point of an arbitrary dataset
# foreach (keys %{$self->{'_data'}}) {
# $arrayref = $self->{'_data'}->{$_};
# $xmin = $xmax = ($self->{'_noZeroX'} ? $$arrayref[0] : 0);
# $ymin = $ymax = ($self->{'_noZeroY'} ? $$arrayref[1] : 0);
# last; # skip any other datasets
# }
# cycle through the datasets looking for min and max values
foreach (keys %{$self->{'_data'}}) {
$arrayref = $self->{'_data'}->{$_};
for ($i=0; $i<$#{$arrayref}; $i++) {
$xmin = ($xmin > $$arrayref[$i] ? $$arrayref[$i] : $xmin);
$xmax = ($xmax < $$arrayref[$i] ? $$arrayref[$i] : $xmax);
$i++;
$ymin = ($ymin > $$arrayref[$i] ? $$arrayref[$i] : $ymin);
$ymax = ($ymax < $$arrayref[$i] ? $$arrayref[$i] : $ymax);
}
}
# set axes data ranges as decimal order of magnitude of widest dataset
($self->{'_xmin'}, $self->{'_xmax'}) = $self->_getOM ('x', $xmin,$xmax);
($self->{'_ymin'}, $self->{'_ymax'}) = $self->_getOM ('y', $ymin,$ymax);
# calculate conversion constants for _data2pxl()
$self->{'_xslope'} = ($self->{'_imx'} - 2 * $self->{'_horGraphOffset'})
/ ($self->{'_xmax'} - $self->{'_xmin'});
$self->{'_yslope'} = ($self->{'_imy'} - 2 * $self->{'_vertGraphOffset'})
/ ($self->{'_ymax'} - $self->{'_ymin'});
$self->{'_ax'} = $self->{'_horGraphOffset'};
$self->{'_ay'} = $self->{'_imy'} - $self->{'_vertGraphOffset'};
$self->{'_validMinMax'} = 1;
print STDERR
"_getMinMax(): ($self->{'_omx'}, $self->{'_omy'}) "
. "($xmin,$xmax) ($ymin,$ymax) "
. "($self->{'_xmin'}, $self->{'_xmax'}) "
. "($self->{'_ymin'}, $self->{'_ymax'})\n"
if $self->{'_debugging'};
}
# returns order of magnitude (with decimal) greater than +/- min and max
# sets _omx or _omy used for translating _data2pxl
# usage: ($min, $max) = $self->_getOM ('x', $xmin, $xmax); # or ('y', $ymin, $ymax)
sub _getOM {
my $self = shift;
my $xory = shift;
my @nums = @_;
my ($tmp, $om) = (0,0);
my @sign = ();
if ($nums[0] == 0 && $nums[1] == 0) {
$self->{"_om$xory"} = 1;
return (0,1);
}
# find the (exponential) order of magnitude eg, 1000
foreach (@nums) {
if ($_<0) {
push @sign, ('-1');
$_ = - $_;
} elsif ($_ == 0) {
push @sign, ('0');
next;
} else {
push @sign, ('1');
}
$tmp = 10 ** (int (log($_) / log(10))); # 1, 10, 100, ... less than $_
$om = ( $tmp>$om ? $tmp : $om );
}
$self->{"_om$xory"} = $om;
# return the decimal order of magnitude eg, 7000
# epsilon adjustment in case om equals min or max
return (0,0) if $om == 0; # to prevent divide by zero
return ( $om * (int(($_[0]-0.00001*$sign[0])/$om) + $sign[0]),
$om * (int(($_[1]-0.00001*$sign[1])/$om) + $sign[1])
);
}
# draws all the datasets in $self->{'_data'}
# usage: $self->_drawData()
sub _drawData {
my $self = shift;
my ($i, $num, $px, $py, $prevpx, $prevpy, $dataSetLabel, $color);
foreach $dataSetLabel (keys %{$self->{'_data'}}) {
# get color
my $color = $self->{'_im'}->colorAllocate(@{$self->{'_dataStyle'}->{$dataSetLabel}});
# draw the first point
($px, $py) = $self->_data2pxl (
$self->{'_data'}->{$dataSetLabel} [0],
$self->{'_data'}->{$dataSetLabel} [1]
);
#$self->{'_im'}->arc($px, $py,4,4,0,360,$color)
# unless $self->{'_dataStyle'}->{$dataSetLabel} =~ /nopoint/i;
($prevpx, $prevpy) = ($px, $py);
# debugging
if ($self->{'_debugging'}) {
$self->{'_im'}->string(gdSmallFont,$px,$py-10,
"0($px,$py)",$color);
print STDERR "pxldata: 0 ($px, $py)";
}
# draw the rest of the points and lines
$num = @{ $self->{'_data'}->{$dataSetLabel} };
for ($i=2; $i<$num; $i+=2) {
# get next point
($px, $py) = $self->_data2pxl (
$self->{'_data'}->{$dataSetLabel}[$i],
$self->{'_data'}->{$dataSetLabel}[$i+1]
);
# draw point, maybe
$self->{'_im'}->arc($px, $py,4,4,0,360,$color) if $i == $num-2;
# draw line from previous point, maybe
if ($self->{'_dataStyle'}->{$dataSetLabel} =~ /dashed/) {
$self->{'_im'}->dashedLine($prevpx, $prevpy, $px, $py, $color);
} elsif ($self->{'_dataStyle'}->{$dataSetLabel} =~ /noline/i) {
next;
} else { # default to solid line
$self->{'_im'}->line($prevpx, $prevpy, $px, $py, $color);
}
($prevpx, $prevpy) = ($px, $py);
# debugging
if ($self->{'_debugging'}) {
$self->{'_im'}->string(gdSmallFont,$px-10,$py+10,
"$i($px,$py)",$color);
print STDERR "$i ($px, $py)";
}
}
}
}
# translate a data point to the nearest pixel point within the graph
# usage: (px,py) = $self->_data2pxl (x,y)
sub _data2pxl {
my $self = shift;
my ($x, $y) = @_;
return ( int ( $self->{'_ax'}
+ ($x - $self->{'_xmin'}) * $self->{'_xslope'} ),
int ( $self->{'_ay'}
- ($y - $self->{'_ymin'}) * $self->{'_yslope'} )
);
}
# draw the axes, axis labels, ticks and tick labels
# usage: $self->_drawAxes
sub _draw_grid {
# axes run from data points: x -- ($xmin,0) ($xmax,0);
# y -- (0,$ymin) (0,$ymax);
# these mins and maxes are decimal orders of magnitude bounding the data
my $self = shift;
my ($w,$h) = (gdSmallFont->width, gdSmallFont->height);
### horizontal axis
my ($p1x, $p1y) = $self->_data2pxl( $self->{'_xmin'}, 0 );
my ($p2x, $p2y) = $self->_data2pxl( $self->{'_xmax'}, 0 );
$self->{'_im'}->line($p1x, $p1y, $p2x, $p2y, $self->{'_gray'});
### vertical axis
my ($vp1x, $vp1y) = $self->_data2pxl (0, $self->{'_ymin'});
my ($vp2x, $vp2y) = $self->_data2pxl (0, $self->{'_ymax'});
$self->{'_im'}->line($vp1x, $vp1y, $vp2x, $vp2y, $self->{'_gray'});
### draw axis ticks and tick labels
my ($i,$px,$py, $step);
### horizontal
if ($self->{'_xTickLabels'}) {
# a hashref with horizontal data point and label
# example: %{$self->{'_xTickLabels'} = (10 => 'Ten', 20 => 'Twenty', ...)
foreach ( keys %{$self->{'_xTickLabels'}} ) {
($px,$py) = $self->_data2pxl($_, 0);
$self->{'_im'}->line($px, $vp1y, $px, $vp2y, $self->{'_medium'});
}
}
else {
# horizontal step calculation
$step = $self->{'_omx'};
# step too large
$step /= 2 if ($self->{'_xmax'} - $self->{'_xmin'}) / $step < 6;
# once again. A poor hack for case om = max.
$step /= 2 if ($self->{'_xmax'} - $self->{'_xmin'}) / $step < 6;
# step too small. As long as we are doing poor hacks
$step *= 2 if ($self->{'_xmax'} - $self->{'_xmin'}) / $step > 12;
for ($i=$self->{'_xmin'}; $i <= $self->{'_xmax'}; $i+=$step ) {
($px,$py) = $self->_data2pxl($i, 0);
$self->{'_im'}->line($px, $vp1y, $px, $vp2y, $self->{'_medium'});
}
}
### vertical
if ($self->{'_yTickLabels'}) {
foreach ( keys %{$self->{'_yTickLabels'}} ) {
($px,$py) = $self->_data2pxl(0, $_);
$self->{'_im'}->line($p1x, $py, $p2x, $py, $self->{'_medium'});
}
}
else {
$step = $self->{'_omy'};
$step /= 2 if ($self->{'_ymax'} - $self->{'_ymin'}) / $step < 6;
$step /= 2 if ($self->{'_ymax'} - $self->{'_ymin'}) / $step < 6;
$step *= 2 if ($self->{'_ymax'} - $self->{'_ymin'}) / $step > 12;
for ($i=$self->{'_ymin'}; $i <= $self->{'_ymax'}; $i+=$step ) {
($px,$py) = $self->_data2pxl (0, $i);
$self->{'_im'}->line($p1x, $py, $p2x, $py, $self->{'_medium'});
}
}
}
# draw the axes, axis labels, ticks and tick labels
# usage: $self->_drawAxes
sub _drawAxes {
# axes run from data points: x -- ($xmin,0) ($xmax,0);
# y -- (0,$ymin) (0,$ymax);
# these mins and maxes are decimal orders of magnitude bounding the data
my $self = shift;
my ($w,$h) = (gdSmallFont->width, gdSmallFont->height);
### horizontal axis
my ($p1x, $p1y) = $self->_data2pxl ($self->{'_xmin'}, 0);
my ($p2x, $p2y) = $self->_data2pxl ($self->{'_xmax'}, 0);
$self->{'_im'}->line($p1x, $p1y, $p2x, $p2y, $self->{'_gray'});
### axis label
my $len = $w * length ($self->{'_horAxisLabel'});
my $xStart = ($p2x+$len/2 > $self->{'_imx'}-10) # center under right end of axis
? ($self->{'_imx'}-10-$len) : ($p2x-$len/2); # or right justify
$self->{'_im'}->string (gdSmallFont, $xStart, $p2y+3*$h/2,
$self->{'_horAxisLabel'},
$self->{'_gray'});
print STDERR "\nHor: p1 ($p1x, $p1y) p2 ($p2x, $p2y)\n"
if $self->{'_debugging'};
### vertical axis
($p1x, $p1y) = $self->_data2pxl (0, $self->{'_ymin'});
($p2x, $p2y) = $self->_data2pxl (0, $self->{'_ymax'});
$self->{'_im'}->line($p1x, $p1y, $p2x, $p2y, $self->{'_gray'});
### axis label
$xStart = $p2x - length ($self->{'_vertAxisLabel'}) * $w / 2;
$self->{'_im'}->string (gdSmallFont, ($xStart>10 ? $xStart : 10), $p2y - 2*$h,
$self->{'_vertAxisLabel'},
$self->{'_gray'});
print STDERR "Ver: p1 ($p1x, $p1y) p2 ($p2x, $p2y)\n"
if $self->{'_debugging'};
###
### draw axis ticks and tick labels
###
my ($i,$px,$py, $step);
###
### horizontal
###
# if horizontal custom tick labels
if ($self->{'_xTickLabels'}) {
# a hashref with horizontal data point and label
# example: %{$self->{'_xTickLabels'} = (10 => 'Ten', 20 => 'Twenty', ...)
foreach ( keys %{$self->{'_xTickLabels'}} ) {
($px,$py) = $self->_data2pxl($_, 0);
$self->{'_im'}->line($px, $py-2, $px, $py+2, $self->{'_gray'});
$self->{'_im'}->string ( gdSmallFont,
$px-length( ${$self-> {'_xTickLabels'}}{$_} ) * $w/2,
$py+$h/2,
${$self->{'_xTickLabels'}}{$_},
$self->{'_gray'}
);
}
} else {
# horizontal step calculation
$step = $self->{'_omx'};
# step too large
$step /= 2 if ($self->{'_xmax'} - $self->{'_xmin'}) / $step < 6;
# once again. A poor hack for case om = max.
$step /= 2 if ($self->{'_xmax'} - $self->{'_xmin'}) / $step < 6;
# step too small. As long as we are doing poor hacks
$step *= 2 if ($self->{'_xmax'} - $self->{'_xmin'}) / $step > 12;
for ($i=$self->{'_xmin'}; $i <= $self->{'_xmax'}; $i+=$step ) {
($px,$py) = $self->_data2pxl($i, 0);
$self->{'_im'}->line($px, $py-2, $px, $py+2, $self->{'_gray'});
$self->{'_im'}->string (gdSmallFont,
$px-length($i)*$w/2, $py+$h/2,
$i, $self->{'_gray'}) unless $i == 0;
}
print STDERR "Horstep: $step ($self->{'_xmax'} - $self->{'_xmin'})/$self->{'_omx'})\n"
if $self->{'_debugging'};
}
###
### vertical
###
if ($self->{'_yTickLabels'}) {
foreach ( keys %{$self->{'_yTickLabels'}} ) {
($px,$py) = $self->_data2pxl(0, $_);
$self->{'_im'}->line($px-2, $py, $px+2, $py, $self->{'_gray'});
$self->{'_im'}->string ( gdSmallFont,
$px-(1+length( ${$self->{'_yTickLabels'}}{$_} )) * $h/2,
$py-$h/2,
${$self->{'_yTickLabels'}}{$_},
$self->{'_gray'});
}
} else {
$step = $self->{'_omy'};
$step /= 2 if ($self->{'_ymax'} - $self->{'_ymin'}) / $step < 6;
$step /= 2 if ($self->{'_ymax'} - $self->{'_ymin'}) / $step < 6;
$step *= 2 if ($self->{'_ymax'} - $self->{'_ymin'}) / $step > 12;
for ($i=$self->{'_ymin'}; $i <= $self->{'_ymax'}; $i+=$step ) {
($px,$py) = $self->_data2pxl (0, $i);
$self->{'_im'}->line($px-2, $py, $px+2, $py, $self->{'_gray'});
$self->{'_im'}->string (gdSmallFont,
$px-5-length($i)*$w, $py-$h/2,
$i, $self->{'_gray'}) unless $i == 0;
}
print STDERR "Verstep: $step ($self->{'_ymax'} - $self->{'_ymin'})/$self->{'_omy'})\n"
if $self->{'_debugging'};
}
}
sub _drawTitle {
my $self = shift;
my ($w,$h) = (gdMediumBoldFont->width, gdMediumBoldFont->height);
# increase vert offset and recalculate conversion constants for _data2pxl()
$self->{'_vertGraphOffset'} += 2*$h;
$self->{'_xslope'} = ($self->{'_imx'} - 2 * $self->{'_horGraphOffset'})
/ ($self->{'_xmax'} - $self->{'_xmin'});
$self->{'_yslope'} = ($self->{'_imy'} - 2 * $self->{'_vertGraphOffset'})
/ ($self->{'_ymax'} - $self->{'_ymin'});
$self->{'_ax'} = $self->{'_horGraphOffset'};
$self->{'_ay'} = $self->{'_imy'} - $self->{'_vertGraphOffset'};
# centered below chart
my ($px,$py) = ($self->{'_imx'}/2, # $self->{'_vertGraphOffset'}/2);
$self->{'_imy'} - $self->{'_vertGraphOffset'}/2);
($px,$py) = ($px - length ($self->{'_title'}) * $w/2, $py+$h/2);
$self->{'_im'}->string (gdMediumBoldFont, $px, $py,
$self->{'_title'},
$self->{'_gray'});
}
1;
__END__
=head1 NAME
Plot - Plot two dimensional data in an image. Version 0.10.
=head1 SYNOPSIS
use Plot;
my $img = Plot->new();
my $anotherImg = Plot->new ($image_width, $image_height);
$img->setData (\@dataset) or die( $img->error() );
$img->setData (\@xdataset, \@ydataset);
$img->setData (\@anotherdataset, 'red_dashedline_points');
$img->setData (\@xanotherdataset, \@yanotherdataset,
'Blue SolidLine NoPoints');
my ($xmin, $ymin, $xmax, $ymax) = $img->getBounds();
$img->setGraphOptions ('horGraphOffset' => 75,
'vertGraphOffset' => 100,
'title' => 'My Graph Title',
'horAxisLabel' => 'my X label',
'vertAxisLabel' => 'my Y label' );
print $img->draw();
=head1 DESCRIPTION
I wrote B<Plot> to create images of some simple graphs
of two dimensional data. The other graphing interface modules to GD.pm
I saw on CPAN either could not handle negative data, or could only
chart evenly spaced horizontal data. (If you have evenly spaced or
nonmetric horizontal data and you want a bar or pie chart, I have
successfully used the GIFgraph and Chart::* modules, available on
CPAN.)
B<Plot> will plot multiple data sets in the same graph, each
with some negative or positive values in the independent or dependent
variables. Each dataset can be a scatter graph (data are represented
by graph points only) or with lines connecting successive data points,
or both. Colors and dashed lines are supported, as is scientific
notation (1.7E10). Axes are scaled and positioned automatically
and 5-10 ticks are drawn and labeled on each axis.
You must have already installed the B<GD.pm> library by Lincoln Stein,
available on B<CPAN> or at http://stein.cshl.org/WWW/software/GD/
Versions of GD below 1.19 supported only gif image format. Versions
between 1.20 and 1.26 support only png format. GD version 1.27
supports either png or jpg image formats. Plot will draw
whichever format your version of GD will draw. (See below for a method
to determine which format your version supports.)
=head1 USAGE
=head2 Create an image object: new()
use Plot;
my $img = Plot->new;
my $img = Plot->new ( $image_width, $image_height );
my $anotherImg = new Plot;
Create a new empty image with the new() method. It will be transparent
and interlaced if your version of GD supports gif format. png does
not yet support either. If image size is not specified, the default is 400
x 300 pixels, or you can specify a different image size. You can also
create more than one image in the same script.
=head2 Acquire a dataset: setData()
$img->setData (\@data);
$img->setData (\@xdata, \@ydata);
$img->setData (\@data, 'red_dashedline_points');
$img->setData (\@xdata, \@ydata, 'blue solidline');
The setData() method reads in a two-dimensional dataset to be plotted
into the image. You can pass the dataset either as one flat array
containing the paired x,y data or as two arrays, one each for the x
and y data.
As a single array, in your script construct a flat array of the
form (x0, y0, ..., xn, yn) containing n+1 x,y data points . Then plot
the dataset by passing a reference to the data array to the setData()
method. (If you do not know what a reference is, just put a backslash
(\) in front of the name of your data array when you pass it as an
argument to setData().) Like this:
my @data = qw( -3 9 -2 4 -1 1 0 0 1 1 2 4 3 9);
$img->setData (\@data);
Or, you may find it more convenient to construct two equal length
arrays, one for the horizontal and one for the corresponding vertical
data. Then pass references to both arrays (horizontal first) to
setData():
my @xdata = qw( -3 -2 -1 0 1 2 3 );
my @ydata = qw( 9 4 1 0 1 4 9 );
$img->setData (\@xdata, \@ydata);
In the current version, if you pass a reference to a single, flat
array to setData(), then only a reference to the data array is stored
internally in the plot object, not a copy of the array. The object
does not modify your data, but you can and the modified data will be
drawn. On the other hand, if you pass references to two arrays, then
copies of the data are stored internally, and you cannot modify them
from within your script. This inconsistent behavior is probably a
bug, though it might be useful from time to time.
You can also plot multiple datasets in the same graph by calling
C<$img-E<gt>setData()> repeatedly on different datasets.
B<Error checking:> The setData() method returns a postive integer on
success and 0 on failure. If setData() fails, you can recover an error
message about the most recent failure with the error() method. The
error string returned will either be "The data set does not contain an
equal number of x and y values." or "The data element ... is
non-numeric."
$p->setData (\@data) or die( $p->error() );
In the current version, only numerals, decimal points (apologies to
Europeans), minus signs, and more generally, scientific notation
(+1.7E-10 or -.298e+17) are supported. Commas (,), currencies ($),
time (11:23am) or dates (23/05/98) are not yet supported and will
generate errors. I hope to figure these out sometime in the future.
Be cautious with scientific notation, since the axis tick labels will
probably become unwieldy. Consider rescaling your data by orders of
magnitude or using logarithmic transforms before plotting them. Or
experiment with image size and graph offset.
B<Style options:> You can also specify certain graphing style options
for each dataset by passing an optional final string argument to
setData() with a concatenated list of selections from each of the
following groups:
BLACK SOLIDLINE POINTS
red dashedline nopoints
green noline
blue
The capitalized options in each group are the default for that group. If
you do not specify any options, you will get black solid lines
connecting successive data points with dots at each data point
('black_solidline_points'). If you want a red scatter plot (red dots
but no lines) you could specify either
$p->setData (\@data, 'redNOLINE');
$p->setData (\@xdata, \@ydata, 'Points Noline Red');
Options are detected by a simple regexp match, so order does not
matter in the option string, options are not case sensitive and
extraneous characters between options are ignored. There is no harm
in specifying a default. There is also no error checking.
=head2 Obtain current graph boundaries: getBounds()
my ($xmin, $ymin, $xmax, $ymax) = $img->getBounds;
This method returns the data values of the lower left corner and upper
right corner of the graph, based on the datasets so far set. If you
have only positive data, then $xmin and $ymin will be 0. The upper
values will typically not be the data maxima, since axis tick ranges
are usually a little beyond the range of the data. If you add another
dataset, these values may become inaccurate, so you will need to call
the method again. As an example, I use this to draw a least squares
regression line (using Statistics::OLS) through a scatter plot of the
data, running from the edges of the graph rather than from the bounds
of the data.
=head2 Graph-wide options: setGraphOptions()
$img->setGraphOptions ('title' => 'My Graph Title',
'horAxisLabel' => 'my X label',
'vertAxisLabel' => 'my Y label'
'horGraphOffset' => $numHorPixels,
'vertGraphOffset' => $numvertPixels);
my %xTickLabels = qw (1 One o'clock 2 Two o'clock 3 Three o'clock);
my %yTickLabels = qw (1 Jan 2 Feb 3 Mar);
$img->setGraphOptions ('xTickLabels' => \%xTickLabels,
'yTickLabels' => \%yTickLabels)
or die ($img->error);
This method and each of its arguments are optional. You can call it
with one, some or all options, or you can call it repeatedly to set or
change options. This method will also accept a hash.
In the current version, Plot is a little smarter about
placement of text, but is still not likely to satisfy everyone, If you
are not constructing images on the fly, you might consider leaving
these blank and using a paint program to add text by hand. Or place
descriptive text in a caption outside the image.
Titles and Axis labels are blank, by default. The title will be
centered in the margin space below the graph. A little extra vertical
offset space (the margin between the edges of the graph proper and the
image) is added to allow room. There is no support for multi-line
strings. You can specify empty strings for one or the other of the
axis labels. The vertical label will be centered or left justified
above the vertical axis; the horizontal label will be placed below the
end of the horizontal axis, centered or right justified.
By default, the graph will be centered within the image, with 50
pixels offset distance from its edges to the edges of the image
(though a title will increase the vertical offset). Axis and tick
labels and the title will appear in this margin (assuming all data are
positive). You can obtain more space for a title or a horizontal label
by increasing the image size (method new() ) and adjusting the
offset.
B<Custom Tick Labels:> Normally, Plot will draw five to ten
ticks on each axis and label them with their corresponding data
values. You can override this and supply your own custom tick labels
to either axis in a hash reference, in which the axis position (eg,
the axis data coordinate) is the key and the label at that distance
along the axis is its value. If a key is not a number, an error
message is set to the effect that a tick label is non-numeric. If you
supply an empty hash reference, all ticks will be suppressed.
=head2 Draw the image: draw()
$img->draw();
$img->draw('jpeg') or die "$img->error()";
This method draws the image and returns it as a string, which you can
print to a file or to STDOUT. (This should be the last method called
from the $img object.) You will generally need to know which image
format your version of GD supports: if it supports png, then to save
the image in a file:
open (WR,'>plot.png') or die ("Failed to write file: $!");
binmode WR; # for DOSish platforms
print WR $img->draw();
close WR;
Or, to return the graph from a cgi script:
print "Content-type: image/png\n\n";
print $img->draw();
Or, to pipe it to a viewing program which accepts STDIN (such as xv on
Unix)
open (VIEWER,'| /usr/X11R6/bin/xv -') or die ("Failed to open viewer: $!");
print VIEWER $img->draw();
close VIEWER;
Of course, if you have a version of GD which supports only gif, change
the file names and types to gif. GD version 1.19 and below supported
only gif image format. Versions between 1.20 and 1.26 support only png
format. If you are not sure, or suspect the supported formats may
change in the future, you can use
$extension = $img->image_type();
open (WR,">plot.$extension");
to obtain the type, 'png' or 'gif'. Often, you must know the type to
write the correct file extension or to return the correct content type
from a cgi script.
GD version 1.27 supports both png and jpeg image formats. For this
version, C<draw()> will default to 'png' unless you supply 'jpeg' as
the argument. C<image_type()> will return 'png' in scalar context and
the list of all supported formats C<(png,jpeg)> in array context.
If the argument to C<draw()> is not a supported image format by the
local version of GD, C<draw()> will return undef and an error message
will be set. C<error()> will return 'The image format ... is not
supported by this version ... of GD.'
=head2 Accessing the GD object directly
Plot is a front end to GD and creates an internal GD
object. You can access the GD object directly, to use GD methods to
draw on it in ways that Plot does not anticipate. The
C<getGDobject()> method in Plot returns the object reference to
its internal GD object.
my $GDobject = $img->getGDobject();
my ($GDobject, $black, $white, $red, $green, $blue)
= $img->getGDObject();
In scalar context, this method returns only the reference to the GD
object. It can also return a list containing the reference to the
image object and the colors already created by Plot for that GD
onject, in the order specified above. If you do not obtain these
colors, you will need to allocate your own colors before drawing,
example below.
When you call the C<draw()> method of Plot (typically the last
step in your script) any drawing you have done in your script with GD
on the GD object will also be drawn.
Since Plot works with data values and GD works with pixel
values, you will need the C<data2pxl()> method of Plot to
translate (x,y) pairs of data values to (px,py) pairs of pixel
values. (You call this method on the Plot object, not the GD
object.) You must call this method only after all datasets have been
registered with the setData() method, since the graph scaling and this
translation may change with each new dataset.
Here is a brief example which draws small blue circles around each
data point in the chart.
use Plot;
my $img = Plot->new;