<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xlink="http://www.w3.org/1999/xlink">

<!-- ===========================================================================
     GanttMagic.xsl ::: (c) Copyright 2002 PolyGlot, Inc.
     This file contains the XSLT stylesheet that transforms an XML file
     in GanttMagic data format (as defined in the XSchema with the URI
     "http://www.polyglotinc.com/NS/GanttMagic/V1/") into an SVG 1.0 file (which
     is itself XML) that describes a graphic Gantt Chart display of the sorted
     input data complete with summary information, titles, etc.
     ===========================================================================
-->

  <!-- generate SVG doctype declarations in generated XML -->
  <xsl:output method="xml"
    doctype-public="-//W3C//DTD SVG 1.0//EN"
    doctype-system="http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"/>

  <!-- parameters to this stylesheet with default values -->

  <xsl:param name="pageNo">1</xsl:param>
  <xsl:param name="path"></xsl:param>

  <!-- Setup calculated constants -->

  <xsl:variable name="lowBarCount" select="12" />
  <xsl:variable name="midBarCount" select="22" />

  <xsl:variable name="barCount" select="count(ganttMagic/bar)"/>

  <xsl:variable name="shortFudge">
    <xsl:choose>
      <xsl:when test="$barCount &lt; $lowBarCount">
	<xsl:text>-300</xsl:text>
      </xsl:when>
      <xsl:when test="$barCount &lt; $midBarCount">
	<xsl:text>-175</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>0</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="pageUnitsW" select="700" />

  <xsl:variable name="pageUnitsH">
    <xsl:choose>
      <xsl:when test="$barCount &lt; $lowBarCount">
	<xsl:text>200</xsl:text>
      </xsl:when>
      <xsl:when test="$barCount &lt; $midBarCount">
	<xsl:text>300</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>500</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <!--
   <xsl:variable name="pageWidth"  select="11in" />
   <xsl:variable name="pageHeight" select="8.25in" />
   -->

  <xsl:variable name="pageWidth"   select="'100%'" />
  <xsl:variable name="pageHeight"  select="'100%'" />
  <xsl:variable name="gridOffsetW" select="80" />
  <xsl:variable name="gridOffsetH" select="40" />
  <xsl:variable name="infoOffsetW" select="10" />
  <xsl:variable name="infoOffsetH" select="50" />	<!-- space till page bottom-->

  <xsl:variable name="slotUnitsH"     select="10" />
  <xsl:variable name="slotPerPage"    select="($pageUnitsH - ($gridOffsetH+$infoOffsetH)) div $slotUnitsH" />
  <xsl:variable name="slotMin"        select="($pageNo - 1) * $slotPerPage" />
  <xsl:variable name="slotMax"        select="($pageNo - 0) * $slotPerPage" />
  <xsl:variable name="slotsPerStripe" select="4" />
  <xsl:variable name="stripeUnitsH"   select="$slotsPerStripe * $slotUnitsH" />
  <xsl:variable name="totalColor"     select="'grey'" />
  <xsl:variable name="actveColor"     select="'red'" />

  <xsl:variable name="baseYear"   select="1974" />
  <xsl:variable name="yearUnitsW" select="20" />

  <xsl:variable name="pagesN" select="ceiling($barCount div $slotPerPage)" />

  <!-- =============== -->
  <!--  MAIN TEMPLATE  -->
  <!-- =============== -->

  <xsl:template match="/">
    <!-- generate reference to CSS stylesheet in generated XML -->
    <xsl:processing-instruction name="xml-stylesheet">href="<xsl:value-of select="$path"/>ganttMagic.css" type="text/css"</xsl:processing-instruction>

    <svg width="{$pageWidth}" height="{$pageHeight}" viewBox="0 0 {$pageUnitsW} {$pageUnitsH}" preserveAspectRatio="xMidYMid meet">
      <defs>
	<line id="stripe" x1="00" y1="{$slotUnitsH div 2}" x2="{$pageUnitsW - 20}" y2="{$slotUnitsH div 2}" style="stroke: #cfc; stroke-width: {$slotUnitsH - 2};"/>
      </defs>
      <title>
	<xsl:value-of select="ganttMagic/@name"/>; Page <xsl:value-of select="$pageNo"/> of <xsl:value-of select="$pagesN"/></title>
      <desc>GanttMagic beta SVG display.</desc>

      <!-- draw background stripes -->
	<use xlink:href="#stripe" y="{$stripeUnitsH *  1}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH *  2}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH *  3}"/>
      <xsl:if test="$barCount &gt;= $lowBarCount">
	<use xlink:href="#stripe" y="{$stripeUnitsH *  4}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH *  5}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH *  6}"/>
      </xsl:if>
      <xsl:if test="$barCount &gt;= $midBarCount">
	<use xlink:href="#stripe" y="{$stripeUnitsH *  7}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH *  8}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH *  9}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH * 10}"/>
	<use xlink:href="#stripe" y="{$stripeUnitsH * 11}"/>
      </xsl:if>

      <!-- draw bounding rectangle for whole page -->
      <!--<rect x="0" y="0" width="99%" height="99%"
		style="fill: none; stroke: green; stroke-width: 2;"/>-->
      <!-- draw empty grid, axis, and labels -->
      <xsl:choose>
	<xsl:when test="$barCount &lt; $lowBarCount">
	  <image xlink:href="{$path}gantt_ruler10.svg" x="0" y="0" width="{$pageUnitsW}" height="{$pageUnitsH}" transform="translate({$gridOffsetH},0)"/>
	</xsl:when>
	<xsl:when test="$barCount &lt; $midBarCount">
	  <image xlink:href="{$path}gantt_ruler20.svg" x="0" y="0" width="{$pageUnitsW}" height="{$pageUnitsH}" transform="translate({$gridOffsetH},0)"/>
	</xsl:when>
	<xsl:otherwise>
	  <image xlink:href="{$path}gantt_ruler.svg" x="0" y="0" width="{$pageUnitsW}" height="{$pageUnitsH}" transform="translate({$gridOffsetH},0)"/>
	</xsl:otherwise>
      </xsl:choose>

      <text style="font-family: sans-serif; font-size: 6pt;">
	<tspan x="{$infoOffsetW}" y="{$gridOffsetH}" style="baseline-shift: 1.5em; fill: {$actveColor}; stroke-width: 0;">Total Years</tspan>
	<tspan x="{$infoOffsetW}" y="{$gridOffsetH}" style="baseline-shift: 0.5em; fill: {$totalColor}; stroke-width: 0;">(spanning)</tspan>
      </text>

      <!--Start Bar data here-->
      <xsl:for-each select="ganttMagic/bar">
	<xsl:sort select="number(@to)"   data-type="number" order="descending"/>
	<xsl:sort select="number(@from)" data-type="number" order="ascending"/>
	<xsl:if test="position() &lt;= $slotMax">
	  <xsl:if test="position() &gt;  $slotMin">
	    <xsl:call-template name="doBar">
	      <xsl:with-param name="slot" select="(position()-1) mod $slotPerPage"/>
	      <xsl:with-param name="xi" select="(@from - $baseYear)*$yearUnitsW"/>
	      <xsl:with-param name="L" select="(number(@to) - number(@from))*$yearUnitsW"/>
	    </xsl:call-template>
	  </xsl:if>
	</xsl:if>
      </xsl:for-each>
      <text style="font-family: sans-serif; font-size:9pt;">
	<tspan x="{$gridOffsetW}" y="{$pageUnitsH -20}" style="baseline-shift: 1em;">
	  <xsl:value-of select="ganttMagic/@name"/>
	</tspan>
	<xsl:if test="$pagesN != 1">
	  <tspan x="{$gridOffsetW}" y="{$pageUnitsH -20}" style="baseline-shift: 0em;">Page <xsl:value-of select="$pageNo"/> of <xsl:value-of select="$pagesN"/></tspan>
	</xsl:if>
      </text>
      <!--End Bar data here -->
    </svg>
  </xsl:template>

  <!-- =============== -->
  <!-- NAMED TEMPLATES -->
  <!-- =============== -->

  <xsl:template name="doBar">
    <xsl:param name="slot"/>
    <xsl:param name="xi" select="@from"/>
    <xsl:param name="L" select="@length"/>
    <!--if total area should go from xi for length L top aligned at yi then
      - const kThick=10; kSep=1;
      - <rect x="xi" y="yi" width="L" height="kThick-kSep" style="fill: gray;"/>
      - <polyline points="xi yi+kThick-kSep, xi   yi,             xi+L yi"
      -       style="stroke: silver; stroke-width: 1; fill: none;"/>
      - <polyline points="xi yi+kThick-kSep, xi+L yi+kThick-kSep, xi+L yi"
      -       style="stroke: black; stroke-width: 1; fill: none;"/>
      -->
    <xsl:variable name="yi" select="$slot*$slotUnitsH"/>
    <xsl:variable name="t" select="$slotUnitsH - 1"/>
    <xsl:variable name="span" select="$L div $yearUnitsW"/>

    <g transform="translate({$gridOffsetW},{$gridOffsetH})">

      <rect class="total" x="{$xi}" y="{$yi}" width="{$L}" height="{$t}"/>
      <polyline class="tupbevel" points="{$xi} {$yi+$t}, {$xi}    {$yi},    {$xi+$L} {$yi}"/>
      <polyline class="tdnbevel" points="{$xi} {$yi+$t}, {$xi+$L} {$yi+$t}, {$xi+$L} {$yi}"/>

      <xsl:for-each select="active">
	<xsl:call-template name="doActive">
	  <xsl:with-param name="yi" select="$yi"/>
	  <xsl:with-param name="xi" select="(@from - $baseYear)*$yearUnitsW"/>
	  <xsl:with-param name="L" select="number(.)*$yearUnitsW"/>
	</xsl:call-template>
      </xsl:for-each>

      <xsl:variable name="fspan" select="concat(' ',format-number($span,'#0.00'))"/>
      <xsl:variable name="ftotl" select="concat(' ',format-number(sum(active),'#0.00'))"/>
	 <text class="actve"  x="{$infoOffsetW + 2}"            y="{$yi+($t - 1)}"><xsl:value-of select="text()"/></text>
	 <text class="ayears" x="{$infoOffsetW - $gridOffsetW}" y="{$yi+($t - 1)}" xml:space="preserve"><tspan style="fill: {$actveColor}; stroke-width: 0;"><xsl:value-of select="substring($ftotl,string-length($ftotl) - 4)"/></tspan><tspan style="fill: {$totalColor}; stroke-width: 0;"> (<xsl:value-of select="substring($fspan,string-length($fspan) - 4)"/>)</tspan></text>
    </g>
  </xsl:template>

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

  <xsl:template name="doActive">
    <xsl:param name="yi"/>
    <xsl:param name="xi" select="@from"/>
    <xsl:param name="L" select="@length"/>
    <xsl:variable name="t" select="$slotUnitsH - 1"/>
    <g>
      <rect class="actve" x="{$xi}" y="{$yi}" width="{$L}" height="{$t}"/>
      <polyline class="aupbevel" points="{$xi} {$yi+$t}, {$xi}    {$yi},    {$xi+$L} {$yi}"/>
      <polyline class="adnbevel" points="{$xi} {$yi+$t}, {$xi+$L} {$yi+$t}, {$xi+$L} {$yi}"/>
    </g>
  </xsl:template>

</xsl:stylesheet><!-- Stylus Studio meta-information - (c)1998-2001 eXcelon Corp.
<metaInformation>
<scenarios ><scenario default="no" name="ganttMagic" userelativepaths="yes" url="ganttMagic.xml" htmlbaseurl="" processortype="internal" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext=""/><scenario default="yes" name="useNameSpace" userelativepaths="yes" url="ganttMagicNS.xml" htmlbaseurl="" processortype="internal" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext=""/></scenarios><MapperInfo  srcSchemaPath="" srcSchemaRoot="" srcSchemaPathIsRelative="yes" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" />
</metaInformation>
-->