Example 16-8: Drawing an SVG bar for a bar chart

<!-- LabeledSVGBar.xsl: Draw a colorful, labeled rectangle for SVG Bar Chart -->
<xsl:stylesheet version="1.0"
     xmlns:xsl    ="http://www.w3.org/1999/XSL/Transform"
     xmlns:Mayura ="http://www.mayura.com/"
     exclude-result-prefixes="Mayura">
 <!-- Include the stylesheet with the "Mayura:CoolColor" template. -->
 <xsl:include href="CoolColor.xsl"/>
 <!-- Draw a rectangle by creating an SVG <rect> element -->
 <xsl:template name="drawBar">
   <xsl:param name="barIndex"   select="0"/>
   <xsl:param name="label"/>
   <xsl:param name="value"      select="0"/>
   <xsl:param name="chartWidth" select="1000"/>
   <xsl:param name="maxValue"   select="$chartWidth"/>
   <xsl:param name="rowCount"   select="1"/>
   <!-- Setup some constant values to use for calculations -->
   <xsl:variable name="xOffset"   select="100"/>
   <xsl:variable name="yOffset"   select="1"/>
   <xsl:variable name="barHeight" select="20"/>
   <xsl:variable name="gap"       select="4"/>
   <!-- Calculate the "x" and "y" coordinates for the rectangle -->
   <xsl:variable name="x" select="$xOffset"/>
   <xsl:variable name="y" select="$yOffset + $barIndex * ($barHeight + $gap)"/>
   <!--
    | Calculate the width of the bar
    |
    | NOTE: division in XPath is done with the "x div y" and not "x/y"
    |       since "x/y" is a pattern that matches <y> elements when
    |       they occur as children of <x> elements.
    +-->
   <xsl:variable name="barWidth"
               select="($chartWidth * $value) div (2 * $maxValue)"/>
   <xsl:variable name="fontHeight" select="18"/>
   <!-- Create SVG <text> element to display the label -->
   <text x="20" y="{$y + $fontHeight}">
     <xsl:value-of select="$label"/>
   </text>
   <!-- Create SVG <rect> element to display the label -->
   <rect x="{$x}" y="{$y}" height="{$barHeight}" width="{$barWidth}">
     <!-- Create the <rect> elements "style" attribute w/ dynamic value -->
     <xsl:attribute name="style">
       <xsl:text>fill:#</xsl:text>
       <!--
        | Call the "coolColor" template to get RRGGBB hex value for
        | a pleasing color combination
        +-->
       <xsl:call-template name="Mayura:CoolColor">
         <xsl:with-param name="colorIndex"  select="$barIndex"/>
         <xsl:with-param name="totalColors" select="$rowCount"/>
       </xsl:call-template>
       <xsl:text> </xsl:text>
     </xsl:attribute>
   </rect>
   <xsl:variable name="x2" select="$xOffset + $barWidth + 10"/>
   <!-- Create SVG <text> element to display the value being charted -->
   <text x="{$x2}" y="{$y + $fontHeight}">
     <xsl:value-of select="$value"/>
   </text>
  </xsl:template>
</xsl:stylesheet>