Generating a Simple SVG Document with Perl Functions

   



Generating Bar Sets with Perl

Consider the bar set displayed in Figure 17.2.

click to expand
Figure 17.2: A bar set.

The text file  barSet1.txt in Listing 17.5 contains a list of numbers that represent the height of the rectangles in the bar set displayed in Figure 17.2.

Listing 17.5 barSet1.txt

start example
112 224 333 188 255 272 350 312 224 133 288 155 312 224 133
end example

The Perl script  barSet1.pl in Listing 17.6 generates an SVG document that renders a bar set.

Listing 17.6 barSet1.pl

start example
# STEP 1:  initialize variables my($fileName)   = "barSet1.txt"; my($index)      = 0; my($height)     = 0; my($color)      = ""; my($barWidth)   = 20; my($xPosition)  = 0; my($yPosition)  = 0; my($baseLine)   = 400; my(@barColors)  = ("red", "green", "blue", "yellow"); my($colorCount) = scalar(@barColors); my(@dataValues); # STEP 2:  does input file exist? open(INPUT_FILE, "<$fileName") ||        die "Cannot open $fileName: $!\n"; (@dataValues) = <INPUT_FILE>; chomp (@dataValues); # STEP 3:  print header information print "<svg>\n"; print "  <g transform=\"translate(50,50)\">\n"; # STEP 4:  generate bar-related information foreach $index (0..$#dataValues) {   $height    = $dataValues[$index];   $color     = $barColors[$index%$colorCount];   $yPosition = $baseLine-$height;   print "  <rect x=\"$xPosition\" y=\"$yPosition\" ";   print " width=\"$barWidth\" height=\"$height\"\n";   print "        style=\"fill:$color\"/>\n";   $xPosition += $barWidth; } # STEP 5:  print trailer information print "  </g>\n"; print "</svg>\n"; # STEP 6:  close input file close(INPUT_FILE);
end example

Remarks

Make sure that the directory with the Perl executable is included in your PATH variable, and then type the following at the command line:

perl barSet1.pl >barSet1.svg

The SVG document in Listing 17.7 is generated by the Perl script in Listing 17.6.

Listing 17.7 barSet1.svg

start example
<svg> <g transform="translate(50,50)">  <rect x="0" y="288"  width="20" height="112"        style="fill:red"/>  <rect x="20" y="176"  width="20" height="224"        style="fill:green"/>  <rect x="40" y="67"  width="20" height="333"        style="fill:blue"/>  <rect x="60" y="212"  width="20" height="188"        style="fill:yellow"/>  <rect x="80" y="145"  width="20" height="255"        style="fill:red"/>  <rect x="100" y="128"  width="20" height="272"        style="fill:green"/>  <rect x="120" y="50"  width="20" height="350"        style="fill:blue"/>  <rect x="140" y="88"  width="20" height="312"        style="fill:yellow"/>  <rect x="160" y="176"  width="20" height="224"        style="fill:red"/>  <rect x="180" y="267"  width="20" height="133"        style="fill:green"/>  <rect x="200" y="112"  width="20" height="288"        style="fill:blue"/>  <rect x="220" y="245"  width="20" height="155"        style="fill:yellow"/>  <rect x="240" y="88"  width="20" height="312"        style="fill:red"/>  <rect x="260" y="176"  width="20" height="224"        style="fill:green"/>  <rect x="280" y="267"  width="20" height="133"        style="fill:blue"/>  </g> </svg>
end example

Remarks

The Perl script in Listing 17.6 can be easily enhanced so that the heights of the rectangles in the bar set are scaled between 0 and 100. These enhancements will always generate a reasonably sized bar set based on an input file that contains positive numbers. The CD-ROM contains  barSet2.pl which will generate a scaled bar set. Note that there are other enhancements that can be added to the Perl script as well, such as ensuring that all the numbers in the text file are positive and ensuring that every line consists of digits only. However, these latter enhancements are data integrity issues, and therefore they won't be covered in this book.



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net