Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
|
|
|
Generating Bar Sets with Unix Scripts
The text file
Listing 17.8 barSet1.txt
112 224 333 188 255 272 350 312 224 133 288 155 312 224 133
The shell script
Consider the bar set displayed in Figure 17.3.
The Unix shell script
Listing 17.9 barSet1.sh
# STEP 1: initialize variables fileName="barSet1.txt" index=0 count=0 height=0 color="" barWidth=20 xPosition=0 yPosition=0 baseLine=400 # STEP 2: does input file exist? if [ ! -f $fileName ] then echo "Cannot open $fileName" else # STEP 3: print header information echo "<svg>" echo " <g transform=\"translate(50,50)\">" # STEP 4: generate bar-related information for height in `cat $fileName` do index=`expr $count % 4` if [ "$index" -eq "0" ] then color="red" elif [ "$index" -eq "1" ] then color="green" elif [ "$index" -eq "2" ] then color="blue" elif [ "$index" -eq "3" ] then color="yellow" fi yPosition=`expr $baseLine - $height` subline1=" <rect x=\"$xPosition\" y=\"$yPosition\" " subline2=" width=\"$barWidth\" height=\"$height\"" subline3=" style=\"fill:$color\"/>" fullline="${subline1} ${subline2}" echo $fullline echo $subline3 xPosition=`expr $xPosition + $barWidth` count=`expr $count + 1` done # STEP 5: print trailer information echo " </g>" echo "</svg>" fi
Remarks
Listing 17.9 is a straightforward Unix shell script. The Unix echo statement is analogous to the Perl print statement, and the if/then/fi construct is analogous to the Perl if statement. You need to make the shell script executable by typing something like this:
chmod +x barSet1.sh
or you can explicitly set the octal values like this:
chmod 755 barSet.sh
Next, add the current directory to the PATH variables like this:
PATH=.:$PATH; export PATH (Bourne shell) export PATH=.:$PATH
and then you can redirect the output of the Unix shell script to an SVG file as follows:
barSet1.sh >unixBarSet1.svg
The SVG document unixBarSet1.svg in Listing 17.10 is generated by the Unix shell script
Listing 17.10 unixBarSet1.svg
<svg> <g transform="translate(50,50)"> <rect x="0" y="288" width="20" height="112" /> <rect x="20" y="176" width="20" height="224" /> <rect x="40" y="67" width="20" height="333" /> <rect x="60" y="212" width="20" height="188" /> <rect x="80" y="145" width="20" height="255" /> <rect x="100" y="128" width="20" height="272" /> <rect x="120" y="50" width="20" height="350" /> <rect x="140" y="88" width="20" height="312" /> <rect x="160" y="176" width="20" height="224" /> <rect x="180" y="267" width="20" height="133" /> <rect x="200" y="112" width="20" height="288" /> <rect x="220" y="245" width="20" height="155" /> <rect x="240" y="88" width="20" height="312" /> <rect x="260" y="176" width="20" height="224" /> <rect x="280" y="267" width="20" height="133" /> </g> </svg>
|
|
|