/[cvs.andrew.net.au]/disruptive/allhomes/graph
ViewVC logotype

Contents of /disruptive/allhomes/graph

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Tue Apr 26 12:38:21 2005 UTC (17 years, 3 months ago) by mikal
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
Mikal: New scripts

1 #!/usr/bin/perl
2
3 # Draw a bar graph for the property values for a given area
4 # Copyright (C) Michael Still 2005
5 # Written 26 - 27 February 2005
6
7 use strict;
8 use GD;
9
10 my($image, $black, $white, $red, $green, $blue, $count);
11
12 # Configuration for the graph
13 my($graphx, $graphy) = (340, 600);
14 my($leftmargin, $rightmargin, $topmargin, $bottommargin) = (10, $graphx - 10, 10, $graphy - 10);
15 my($dataspacing, $datapoints, $lowdatapoint, $datapointspacing, $datapointwidth, $barspacing) = (50, 13, 100, 25, 10, 5);
16 my($labellength, $maxxlabellen, $xscalefactor) = (100, 40, 10);
17 my($x);
18
19 my(%suburbdata, %regiondata, %canberradata);
20
21 # A new image, with some colours and a given background...
22 $image = new GD::Image($graphx, $graphy);
23
24 $red = $image->colorAllocate(255, 0, 0);
25 $blue = $image->colorAllocate(0, 0, 255);
26 $black = $image->colorAllocate(0, 0, 0);
27 $white = $image->colorAllocate(255, 255, 255);
28
29 $image->transparent($white);
30 $image->interlaced('true');
31 $image->filledRectangle(0, 0, $graphx, $graphy, $white);
32
33 # Snarf data from stdin
34 while(<STDIN>){
35 if(/^canberra (.*)/) { $canberradata{($1 - $lowdatapoint) / $dataspacing}++; }
36 if(/^region (.*)/) { $regiondata{($1 - $lowdatapoint) / $dataspacing}++; }
37 if(/^suburb (.*)/) { $suburbdata{($1 - $lowdatapoint) / $dataspacing}++; }
38 }
39
40 # And plot it
41 for($count = 0; $count < $datapoints; $count++)
42 {
43 # Canberra
44 $x = $leftmargin + ($count * $datapointspacing);
45 $image->rectangle($x,
46 $bottommargin - $maxxlabellen - ($canberradata{$count} * $xscalefactor),
47 $x + 10,
48 $bottommargin - $maxxlabellen,
49 $black);
50
51 # Region
52 $x = $leftmargin + ($count * $datapointspacing) + $barspacing;
53 $image->filledRectangle($x,
54 $bottommargin - $maxxlabellen - ($regiondata{$count} * $xscalefactor),
55 $x + 10,
56 $bottommargin - $maxxlabellen,
57 $blue);
58 $image->rectangle($x,
59 $bottommargin - $maxxlabellen - ($regiondata{$count} * $xscalefactor),
60 $x + 10,
61 $bottommargin - $maxxlabellen,
62 $black);
63
64 # Suburb
65 $x = $leftmargin + ($count * $datapointspacing) + $barspacing * 2;
66 $image->filledRectangle($x,
67 $bottommargin - $maxxlabellen - ($suburbdata{$count} * $xscalefactor),
68 $x + 10,
69 $bottommargin - $maxxlabellen,
70 $red);
71 $image->rectangle($x,
72 $bottommargin - $maxxlabellen - ($suburbdata{$count} * $xscalefactor),
73 $x + 10,
74 $bottommargin - $maxxlabellen,
75 $black);
76
77 if($count == 0){ $image->stringUp(gdSmallFont, $leftmargin, $bottommargin, "< $lowdatapoint", $black); }
78 elsif($count == $datapoints - 1){ $image->stringUp(gdSmallFont, $leftmargin + ($count * $datapointspacing), $bottommargin,
79 "> " . (($count * $dataspacing) + $lowdatapoint), $black); }
80 else{ $image->stringUp(gdSmallFont, $leftmargin + ($count * $datapointspacing), $bottommargin, ($count * $dataspacing) + $lowdatapoint, $black); }
81 }
82
83 # Write out some sexy labels
84 $image->filledRectangle($leftmargin + ($labellength + 10) * 0, $topmargin, $leftmargin + ($labellength + 10) * 1 - 10, $topmargin + 15, $red);
85 $image->rectangle($leftmargin + ($labellength + 10) * 0, $topmargin, $leftmargin + ($labellength + 10) * 1 - 10, $topmargin + 15, $black);
86 $image->string(gdSmallFont, $leftmargin + ($labellength + 10) * 0 + 5, $topmargin + 1, $ARGV[0], $black);
87
88 $image->filledRectangle($leftmargin + ($labellength + 10) * 1, $topmargin, $leftmargin + ($labellength + 10) * 2 - 10, $topmargin + 15, $blue);
89 $image->rectangle($leftmargin + ($labellength + 10) * 1, $topmargin, $leftmargin + ($labellength + 10) * 2 - 10, $topmargin + 15, $black);
90 $image->string(gdSmallFont, $leftmargin + ($labellength + 10) * 1 + 5, $topmargin + 1, $ARGV[1], $black);
91
92 $image->rectangle($leftmargin + ($labellength + 10) * 2, $topmargin, $leftmargin + ($labellength + 10) * 3 - 10, $topmargin + 15, $black);
93 $image->string(gdSmallFont, $leftmargin + ($labellength + 10) * 2 + 5, $topmargin + 1, "Canberra", $black);
94
95 # Dump the image out to a file...
96 binmode STDOUT;
97 print $image->png;

  ViewVC Help
Powered by ViewVC 1.1.22