<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:fo="http://www.w3.org/1999/XSL/Format"
     xmlns:svg="http://www.w3.org/2000/svg"
     xmlns:bg="http://zenteq.am/bg">
<!-- ==================================================== 
  ==  Style sheet for Bussines Graphic rendering       ==
  ==  mode attribute controls geometry ("bar" or "pie")==
  ==  parameter perspective controls  2D or  3D        ==
  ==  for 2D - set zero value for $perspective param   ==
  ==  for 3D - set non-zero value (in bg:table template)  == 
  == ================================================= -->

<!--  include trig functions for PIE-charts  -->
<xsl:include href="sin_cos.xsl"/>


<!-- global variables for BAR-charts -->
<xsl:variable name="bg-graph-height" select="number(500)"/>
<xsl:variable name="bg-extra-height" select="number(300)"/>
<xsl:variable name="bg-graph-width" select="number(700)"/>
<xsl:variable name="bg-left-margin" select="number(100)"/>
<xsl:variable name="bg-depth" select="number(3)"/>
<xsl:variable name="bg-legend-X" select="number(820)"/>
<xsl:variable name="bg-legend-Y" select="number(10)"/>
<xsl:variable name="default-caption-style" select="'font: 26pt Helvetica'"/>
<xsl:variable name="default-label-style" select="'font: 12pt Helvetica'"/>
<xsl:variable name="default-legend-style"
	select="'font: 12pt Helvetica; font-style: oblique'"/>

<!-- ======================================================== -->
<!-- variables and parameters for PIE-charts -->
<!-- define the size of the pie chart -->
<xsl:param name="pie_radiusX" select="number(350)"/>
<xsl:param name="pie_radiusY" select="$pie_radiusX"/>
<!-- define the center position of the pie chart -->
<xsl:param name="pie_centerX" select="$pie_radiusX + 150"/>
<xsl:param name="pie_centerY" select="$pie_radiusY + 50"/>

<!-- define the start angle of the first pie segment -->
<xsl:param name="pie_start_angle" select="number(0)"/>
<!-- define the threshold at which below this % a segment -->
<!-- is not shown individually but grouped                -->
<xsl:param name="pie_lowest_show_percent" select="number(5)"/>
<!-- xsl:param name="perspective"/ -->

<!-- ======================================================== -->

  <xsl:output method="xml" indent="yes"/>
  
      <!--           DRAW BAR-CHART                 -->
  <xsl:template match="bg:table" mode="bar">        
    <xsl:param name="perspective" />
    <xsl:param name="caption-style" select="$default-caption-style"/>
    <xsl:param name="label-style" select="$default-label-style"/>
    <xsl:param name="legend-style" select="$default-legend-style"/>
    <xsl:param name="param-width"  select="3"/>
    <xsl:param name="param-height" select="2"/>
    <svg:svg width="{$param-width}in" height="{$param-height}in" 
             viewBox="100 -200 1600 800" >
      <!-- Draw vertical axis {$bg-graph-height + $bg-extra-height} -->
      <xsl:variable name="max_Y_value">
        <xsl:for-each select="//bg:row[position() &gt; 1]/bg:cell[position() &gt; 1]">
          <xsl:sort select="." data-type="number" order="descending"/>
          <xsl:if test="position() = 1">
            <xsl:value-of select="."/>
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>
      <xsl:variable name="max_bg-graph-height" select="floor(($max_Y_value + 9) div 10) * 10"/>
      <xsl:call-template name="draw-Y">
        <xsl:with-param name="label-style" select="$label-style"/>
        <xsl:with-param name="step" select="$max_bg-graph-height div 10"/>
      </xsl:call-template> 

      <!-- Draw horizontal axis  -->
      <xsl:variable name="X_strip_count">
        <xsl:value-of select="count(//bg:row) - 1"/>
      </xsl:variable>
      <xsl:call-template name="draw-X">
        <xsl:with-param name="label-style" select="$label-style"/>
        <xsl:with-param name="loop" select="$X_strip_count"/>
      </xsl:call-template>  

      <!--           Draw BAR-segments  and legends    -->
      <xsl:apply-templates select="bg:row">
        <xsl:with-param name="legend-style" select="$legend-style"/>
        <xsl:with-param name="row_cnt" select="$X_strip_count"/> 
        <xsl:with-param name="vert_scale" select="$bg-graph-height div $max_bg-graph-height"/>
        <xsl:with-param name="perspective" select="$perspective"/>
      </xsl:apply-templates>
      
      <!-- draw the chart Title -->
      <xsl:if test="bg:caption">
        <svg:text text-anchor="middle" 
           x="{$bg-graph-width div 2 + $bg-left-margin}" y="0"
           style="{$caption-style}">
              <xsl:value-of select="bg:caption"/>  
        </svg:text>  
      </xsl:if>  
    </svg:svg>
  </xsl:template>

  <xsl:template name="draw-X">
    <xsl:param name="label-style"/>
    <xsl:param name="loop"/>
    <xsl:param name="iter">1</xsl:param>
    <xsl:param name="step">
      <xsl:value-of select="$bg-graph-width div $loop"/> 
    </xsl:param>
    <xsl:param name="currentX">
       <xsl:value-of select="$bg-left-margin"/>
    </xsl:param>

      <svg:path fill="none" stroke="black" d="M {$currentX + $step},{$bg-graph-height + 50} v10"/>

      <svg:text x="{$step div 2}" text-anchor="middle"
        baseline-shift="-10"
        transform="matrix(1 0 0 1 {$currentX} {$bg-graph-height + 60})"
        style="{$label-style}">
       <xsl:value-of select="//bg:row[position() = ($iter + 1)]/bg:cell[position()=1]"/>
      </svg:text>
      <xsl:choose>
        <xsl:when test="$iter &lt; $loop">
          <xsl:call-template name="draw-X">
            <xsl:with-param name="label-style" select="$label-style"/>
            <xsl:with-param name="step" select="$step"/>
            <xsl:with-param name="loop" select="$loop"/>
            <xsl:with-param name="iter" select="$iter + 1"/>
            <xsl:with-param name="currentX" select="$currentX + $step"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise >
<!-- Draw horizontal line -->
          <svg:path fill="none" stroke="black" d="M {$bg-left-margin},{$bg-graph-height + 50} h {$bg-graph-width + 20}"/>    
        </xsl:otherwise>
      </xsl:choose>   

  </xsl:template>

  <xsl:template name="draw-Y">
    <xsl:param name="label-style"/>
    <xsl:param name="iter"> 0 </xsl:param>
    <xsl:param name="step">$bg-graph-height div 10 </xsl:param>
    <xsl:param name="currentY">
       <xsl:value-of select="$bg-graph-height + 50"/>
    </xsl:param>
    <svg:path fill="none" stroke="black" d="M {$bg-left-margin - 10},{$currentY} h10"/>
    <svg:text text-anchor="end" baseline-shift="-3"
      transform="matrix(1 0 0 1 {$bg-left-margin - 15} {$currentY})"
      style="{$label-style}">
       <xsl:value-of select="$iter * $step"/>
    </svg:text>
    <xsl:choose>
      <xsl:when test="$iter &lt; 10">
        <xsl:call-template name="draw-Y">
          <xsl:with-param name="label-style" select="$label-style"/>
          <xsl:with-param name="iter" select="$iter + 1"/>
          <xsl:with-param name="currentY" select="$currentY - 50"/>
          <xsl:with-param name="step" select="$step"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise >
<!-- Draw vertical line and mesaurement -->
        <svg:path fill="none" stroke="black" d="M {$bg-left-margin},{$currentY - 30} v {$bg-graph-height + 30}"/> 
        <svg:g transform="rotate(-90,{$bg-left-margin - 70},{$bg-graph-height div 2})"> 
           <svg:text text-anchor="middle" x="0" y="{$bg-graph-height div 2}"
             style="{$label-style}"> 
             <xsl:value-of select="//bg:row[position() = 1]/bg:cell[position() = 1]"/>
           </svg:text>        
        </svg:g>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="bg:row">
    <xsl:param name="legend-style"/>
    <xsl:param name="perspective" select="0"/>
    <xsl:param name="row_cnt"> 1 </xsl:param>
    <xsl:param name="vert_scale"> 1 </xsl:param>    
    <xsl:variable name="cell_cnt" select="count(bg:cell) - 1"/>
    <xsl:variable name="cell_width" select="($bg-graph-width div $row_cnt) div $cell_cnt"/>
    <xsl:variable name="cell_space" select="$cell_width div 20"/>
    <xsl:choose>
       <xsl:when test="position() = 1">
          <xsl:for-each select="bg:cell[position() &gt; 1]">
            <xsl:call-template name="legend">
              <xsl:with-param name="legend-style" select="$legend-style"/>
              <xsl:with-param name="position" select="position()"/>
              <xsl:with-param name="label">
                <xsl:value-of select="."/>
              </xsl:with-param>
           </xsl:call-template>
         </xsl:for-each>
       </xsl:when>
       <xsl:otherwise >
         <xsl:apply-templates select="bg:cell" >
            <xsl:with-param name="perspective" select="$perspective"/>
            <xsl:with-param name="row_w" select="$bg-graph-width div $row_cnt" />
            <xsl:with-param name="row_n" select="position() - 2"/>
            <xsl:with-param name="cell_w" select="$cell_width - 2*$cell_space"/> 
            <xsl:with-param name="cell_s" select="$cell_space"/>
            <xsl:with-param name="scale" select="$vert_scale"/>          
        </xsl:apply-templates>
     </xsl:otherwise>
  </xsl:choose>
  </xsl:template>
<!-- 
  <xsl:template match="bg:cell[position() &gt; 1]" mode="legend">
    <xsl:param name="legend-style"/>
    <xsl:call-template name="legend">
       <xsl:with-param name="legend-style" select="$legend-style"/>
       <xsl:with-param name="position" select="position()"/>
       <xsl:with-param name="label">
           <xsl:value-of select="."/>
       </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
-->
  <xsl:template match="bg:cell[position()&gt; 1]" >
    <xsl:param name="perspective" />
    <xsl:param name="row_n" />
    <xsl:param name="row_w" />
    <xsl:param name="cell_w" />
    <xsl:param name="cell_s" />
    <xsl:param name="scale" />
    <xsl:variable name="higth" select="."/>
    <xsl:variable name="pos" select="position() - 1" />
    <xsl:variable name="cell_depth" select="$pos * $bg-depth - $bg-depth" />
    <xsl:variable name="color">
       <xsl:call-template name="calc_color">
          <xsl:with-param name="rel_position" select="$pos"/>
       </xsl:call-template>
    </xsl:variable>
    <svg:g fill="{$color}" stroke="black" style="stroke-width:0.25; stroke: black; stroke-linecap: butt" >
      <xsl:choose>
        <xsl:when test="$perspective &gt; 0">
           <svg:path  
             d="M {$bg-left-margin +$cell_depth + $cell_s + $row_w *($row_n) + ($cell_s+$cell_w) *($pos - 1)} {$bg-graph-height + 50 - $cell_depth} 
                v {-1 * $higth * $scale} l {6 * $bg-depth},{-6 * $bg-depth} h{$cell_w} 
                v {$higth * $scale} l {-6 * $bg-depth},{6 * $bg-depth}  z
                m 0, {-1 * $higth * $scale}  h{$cell_w} v {$higth * $scale} 
                m 0,{-1 * $higth * $scale}  l {6 * $bg-depth},{-6 * $bg-depth}"/>
        </xsl:when>
        <xsl:otherwise>
           <svg:path 
             d="M {$bg-left-margin + 1 + $cell_s + $row_w *($row_n) + ($cell_s+$cell_w) *($pos - 1)} {$bg-graph-height + 50} 
          v {-1 * $higth * $scale} h{$cell_w} v {$higth * $scale} z" />
         </xsl:otherwise>
       </xsl:choose>
     </svg:g>  
  </xsl:template>


  <!-- ===== DRAW PIE-chart ====== -->
  <xsl:template match="bg:table" mode="pie">
    <xsl:param name="caption-style" select="$default-caption-style"/>
    <xsl:param name="label-style" select="$default-label-style"/>
    <xsl:param name="perspective" />
    <xsl:param name="param-width"  select="3"/>
    <xsl:param name="param-height" select="2"/>
  <!-- get list of distinct bg:rows -->
     <xsl:variable name="Segments" select="//bg:row[position() &gt; 1]"/>
  <!-- get total value of all numbers in bg:cells -->
     <xsl:variable name="TotalSum" select="sum($Segments/bg:cell[position() &gt; 1])"/>
  <!-- do the svg root stuff -->
    <svg:svg width="{$param-width}in" height="{$param-height}in" 
               viewBox="0 0 1500 1000" >
<!-- viewBox="0 0 
                   {$pie_centerY + $pie_radiusY + 200} {$pie_centerY + $pie_radiusY + 200}"> -->
    <!-- call recursive template to work though the bg:rows -->   
        <svg:g>
           <xsl:call-template name="do_segments">
               <xsl:with-param name="label-style" select="$label-style"/>
               <xsl:with-param name="distinct_segments" select="$Segments"/>
               <xsl:with-param name="total_sum" select="$TotalSum"/>
               <xsl:with-param name="perspective" select="$perspective"/>
           </xsl:call-template>
        </svg:g>
    <!-- draw the pie chart title -->
        <xsl:if test="bg:caption">
         <svg:text text-anchor="middle" 
           x="{$pie_centerX}" 
           y="{$pie_radiusY - ($perspective div 2) * $pie_radiusY+$pie_centerY + 50 +$perspective *50 }"
           style="{$caption-style}">
            <xsl:value-of select="bg:caption"/>
            (in %)
         </svg:text>
        </xsl:if>    
     </svg:svg> 
  </xsl:template>
<!--   + $pie_radiusX}"         y="{$pie_centerY + $pie_radiusY + 50}" dx="{2 * $pie_radiusX}" -->
<xsl:template name="do_segments">
  <xsl:param name="label-style"/>
  <xsl:param name="distinct_segments"/>
  <xsl:param name="total_sum"/>
  <xsl:param name="perspective" select="number(0)"/>
  <!-- params used in recursive calls -->
  <xsl:param name="prevSin" select="number(0)"/> <!-- Sin(0) -->
  <xsl:param name="prevCos" select="number(1)"/> <!-- Cos(0) -->
  <xsl:param name="total_perc_so_far" select="number(0)"/>
  <xsl:param name="_i" select="number(1)"/> <!-- used to track actual indice within node-set -->
  <xsl:param name="_i2" select="number(1)"/> <!-- only incremented when a segement is actually drawn --> 
  <xsl:param name="_left_overs_perc" select="number(0)"/>
  <!-- are we handling the left overs or an actual product -->
  <xsl:choose>
    <xsl:when test="$distinct_segments[$_i]">
      <!-- calc total of this row cells -->
      <xsl:variable name="this_segment_sum" 
         select="sum($distinct_segments[$_i]/bg:cell[position() &gt; 1])" />
      <!-- calc the percentage of this sum over total -->
      <xsl:variable name="perc_of_total" select="$this_segment_sum div $total_sum"/>
      <!-- include in "Others" - decide whether it is below draw segment threshold -->
      <xsl:choose>
        <xsl:when test="$perc_of_total &lt; ($pie_lowest_show_percent div 100)">
          <!-- skip it and show it bunched with rest of below thresholds -->
          <xsl:call-template name="do_segments">
            <xsl:with-param name="label-style" select="$label-style"/>
            <xsl:with-param name="distinct_segments" select="$distinct_segments"/>
            <xsl:with-param name="total_sum" select="$total_sum"/>
            <xsl:with-param name="total_perc_so_far" select="$total_perc_so_far"/>
            <xsl:with-param name="_i" select="$_i + 1"/>
            <xsl:with-param name="_i2" select="$_i2"/>
            <xsl:with-param name="perspective" select="$perspective"/>
            <xsl:with-param name="_left_overs_perc" select="$_left_overs_perc + $perc_of_total"/>
            <xsl:with-param name="prevSin" select="$prevSin"/>
            <xsl:with-param name="prevCos" select="$prevCos"/>            
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <!-- draw it -->
          <xsl:variable name="currSin">
             <xsl:call-template name="sin">
                <xsl:with-param name="pX" select=
                   "$pie_start_angle + 360 * ($total_perc_so_far + $perc_of_total)"/>
             </xsl:call-template>
          </xsl:variable>
          <xsl:variable name="currCos">
             <xsl:call-template name="cos">
                <xsl:with-param name="pX" select=
                   "$pie_start_angle + 360 * ($total_perc_so_far + $perc_of_total)"/>
             </xsl:call-template>
          </xsl:variable>
          <xsl:call-template name="draw_pie_segment">
            <xsl:with-param name="label-style" select="$label-style"/>
            <xsl:with-param name="angle" select="360 * $perc_of_total"/>
            <xsl:with-param name="position" select="$_i2"/>
            <xsl:with-param name="prev_angle" select="$pie_start_angle + (360 * $total_perc_so_far)"/>
            <xsl:with-param name="label" select="concat($distinct_segments[$_i]/bg:cell[position() = 1],' (',format-number($perc_of_total * 100,'0.0'),'%)')"/>
            <xsl:with-param name="fill_color">
              <xsl:call-template name="calc_color">
                <xsl:with-param name="rel_position" select="$_i2"/>
              </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name="radiusX" select="$pie_radiusX"/>
            <xsl:with-param name="radiusY" select="$pie_radiusY - ($perspective div 2) * $pie_radiusY"/>
            <xsl:with-param name="centerX" select="$pie_centerX"/>
            <xsl:with-param name="centerY" select="$pie_centerY"/>
            <xsl:with-param name="prevSin" select="$prevSin"/>
            <xsl:with-param name="prevCos" select="$prevCos"/>            
            <xsl:with-param name="currSin" select="$currSin"/>            
            <xsl:with-param name="currCos" select="$currCos"/>            
          </xsl:call-template>
          <!-- and do the next segment (or any left overs) -->
          <xsl:call-template name="do_segments">
            <xsl:with-param name="label-style" select="$label-style"/>
            <xsl:with-param name="distinct_segments" select="$distinct_segments"/>
            <xsl:with-param name="total_sum" select="$total_sum"/>
            <xsl:with-param name="total_perc_so_far" select="$total_perc_so_far + $perc_of_total"/>
            <xsl:with-param name="_i" select="$_i + 1"/>
            <xsl:with-param name="_i2" select="$_i2 + 1"/>
            <xsl:with-param name="perspective" select="$perspective"/>
            <xsl:with-param name="_left_overs_perc" select="$_left_overs_perc"/>
            <xsl:with-param name="prevSin" select="$currSin"/>
            <xsl:with-param name="prevCos" select="$currCos"/>            
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$_left_overs_perc &gt; 0">
      <!-- left overs - with some to actually do -->
      <xsl:call-template name="draw_pie_segment">
        <xsl:with-param name="label-style" select="$label-style"/>
        <xsl:with-param name="angle" select="360 * $_left_overs_perc"/>
        <xsl:with-param name="position" select="$_i2"/>
        <xsl:with-param name="prev_angle" select="$pie_start_angle + (360 * $total_perc_so_far)"/>
        <xsl:with-param name="label" select="concat('Others (',format-number($_left_overs_perc * 100,'0.0'),'%)')"/>
        <xsl:with-param name="fill_color">
          <xsl:call-template name="calc_color">
            <xsl:with-param name="rel_position" select="$_i2"/>
          </xsl:call-template>
        </xsl:with-param>
        <xsl:with-param name="radiusX" select="$pie_radiusX"/>
        <xsl:with-param name="radiusY" select="$pie_radiusY - ($perspective div 2) * $pie_radiusY "/>
        <xsl:with-param name="centerX" select="$pie_centerX"/>
        <xsl:with-param name="centerY" select="$pie_centerY"/>
            <xsl:with-param name="prevSin" select="$prevSin"/>
            <xsl:with-param name="prevCos" select="$prevCos"/>            
            <xsl:with-param name="currSin" select="0"/>            
            <xsl:with-param name="currCos" select="1"/>            
      </xsl:call-template>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<!-- ========================================================= -->
<!-- Template for drawing a single pie chart segment           -->
<!-- Nnote: All info that this template requires for drawing   -->
<!--        the segment is passed in as a parameter.  Thus,    -->
<!--        this template is re-usable to some extent.         -->
<xsl:template name="draw_pie_segment">
  <xsl:param name="label-style"/>
  <xsl:param name="label"/>
  <xsl:param name="position"/>
  <xsl:param name="fill_color" select="'red'"/>
  <xsl:param name="line_color" select="'black'"/>
  <xsl:param name="radiusX" select="number(200)"/>
  <xsl:param name="radiusY" select="number(200)"/>
  <xsl:param name="prevSin" select="number(0)"/>
  <xsl:param name="prevCos" select="number(1)"/>
  <xsl:param name="currSin" select="number(0)"/>
  <xsl:param name="currCos" select="number(1)"/>
  <xsl:param name="centerX" select="$radiusX + 50"/>
  <xsl:param name="centerY" select="$radiusY + 50"/>
  <!-- (X0,Y0) - circle center -->
  <xsl:variable name="X0"> <xsl:value-of select="$centerX"/>
  </xsl:variable>
  <xsl:variable name="Y0"> <xsl:value-of select="$centerY"/>
  </xsl:variable>
  <!-- calc the arc coordinates of the segment -->
  <xsl:variable name="X1"> <xsl:value-of select="($prevSin * $radiusX)"/>
  </xsl:variable>
  <xsl:variable name="Y1"> <xsl:value-of select="0 - ($prevCos * $radiusY)"/>
  </xsl:variable>
  <xsl:variable name="X2"> <xsl:value-of select="($currSin * $radiusX) - $X1"/>
  </xsl:variable>
  <xsl:variable name="Y2"> <xsl:value-of select="(0 - ($currCos * $radiusY)) - $Y1"/>
  </xsl:variable>
  <!-- calc X and Y posn for the label -->
  <xsl:variable name="XTxt">
    <xsl:value-of select="$X1 + ($X2 - $X1) div 2"/>
  </xsl:variable>
  <xsl:variable name="YTxt">
    <xsl:value-of select="(0 - ($Y1 + ($Y2 - $Y1) div 2))"/>
  </xsl:variable>
  <!-- do the actual svg -->
  <svg:g id="{$label}">

    <!-- 3D effect           -->
<xsl:if test="$radiusX &gt; $radiusY">
  <xsl:if test="($currCos &lt; 0 or $prevCos &lt; 0)">  
                   
    <svg:path fill="{none}" stroke="{$line_color}" stroke-width="0.5" 
         d="M{$X0},{$Y0} m{$X1},{$Y1} v30"/>
    <svg:path transform="translate(0, 30)" fill="{none}" stroke="{$line_color}" 
         d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2}"/>
    <svg:path transform="translate(0, 27)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2}"/>
    <svg:path transform="translate(0, 24)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
    <svg:path transform="translate(0, 21)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
    <svg:path transform="translate(0, 18)" fill="{none}" 
         stroke="{$fill_color}" stroke-width="0.5" 
         d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
    <svg:path transform="translate(0, 15)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
    <svg:path transform="translate(0, 12)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
    <svg:path transform="translate(0, 9)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
    <svg:path transform="translate(0, 6)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
    <svg:path transform="translate(0, 3)" fill="{none}" stroke="{$fill_color}" 
         stroke-width="0.5" d="M{$X0},{$Y0} m{$X1},{$Y1} a{$radiusX},{$radiusY} 
         0 0 1 {$X2},{$Y2} m{$X0},{$Y0}"/>
  </xsl:if>
</xsl:if>
    <!-- draw the segment itself -->
    <svg:path fill="{$fill_color}" stroke="{$line_color}" d="M{$X0},{$Y0} l{$X1},{$Y1} a{$radiusX},{$radiusY} 0 0 1 {$X2},{$Y2} L{$X0},{$Y0}"/>
<!-- draw the segment label 
    <svg:text text-anchor="middle" x="{$X0+$XTxt}" y="{$Y0+$YTxt}" style="{$label-style}">
      <xsl:value-of select="$label"/>
    </svg:text>
-->
    <xsl:call-template name="legend">
       <xsl:with-param name="legend-style" select="$label-style"/>
       <xsl:with-param name="position" select="$position"/>
       <xsl:with-param name="label" select="$label"/>
    </xsl:call-template>    
  </svg:g>
</xsl:template>



  <xsl:template name="legend">
    <xsl:param name="legend-style"/>
    <xsl:param name="position" select="position()"/>
    <xsl:param name="label"/>

    <xsl:variable name = "Y" select="$bg-legend-Y + 25 * ($position+1)" />
    <xsl:variable name="color">
       <xsl:call-template name="calc_color">
          <xsl:with-param name="rel_position" select="$position"/>
       </xsl:call-template>
    </xsl:variable>
    <svg:text text-anchor="start" x="{$bg-legend-X + 25}" y="{$Y}"
      style="{$legend-style}">
        <xsl:value-of select="$label"/>
    </svg:text>    
    <svg:rect x="{$bg-legend-X - 5}" y="{$Y - 10}" width="{25}" height="{15}" fill="{$color}"
          style="stroke-width:0.5; stroke: black; stroke-linecap: butt"/>
  </xsl:template>


<!-- ========================================================= -->
<!-- Called template for calculating colors                    -->
<!-- this is a crude way of giving each segment a different    -->
<!-- color - calculated according to a relative position       -->
<xsl:template name="calc_color">
  <xsl:param name="rel_position" select="number(0)"/>
  <xsl:variable name="color_lightnesses" select="'F0D0B09070503010E0C0A08060402000'"/>
  <xsl:text>#</xsl:text>
  <!-- red part -->
  <xsl:choose>
    <xsl:when test="($rel_position mod 3) = 0">F0</xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring($color_lightnesses,(($rel_position mod 16) * 2)+1,2)"/>
    </xsl:otherwise>
  </xsl:choose>
  <!-- blue part -->
  <xsl:choose>
    <xsl:when test="($rel_position mod 3) = 1">F0</xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring($color_lightnesses,(($rel_position mod 16) * 2)+1,2)"/>
    </xsl:otherwise>
  </xsl:choose>
  <!-- green part -->
  <xsl:choose>
    <xsl:when test="($rel_position mod 3) = 2">F0</xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring($color_lightnesses,(($rel_position mod 16) * 2)+1,2)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
