<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="http://www.w3.org/2002/02/style-xsl.css" type="text/css"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:an="http://www.w3.org/2003/05/tr-history/analysis#"
 xmlns:contact="http://www.w3.org/2000/10/swap/pim/contact#"
 xmlns:org="http://www.w3.org/2001/04/roadmap/org#"
 xmlns:doc="http://www.w3.org/2000/10/swap/pim/doc#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:rec="http://www.w3.org/2001/02pd/rec54#"
 xmlns="http://www.w3.org/2000/svg"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
 version="1.0">

 <xsl:output method="xml" encoding="utf-8" indent="yes"/>

 <xsl:param name="group-by"/>
 <xsl:param name="property"/>
 <xsl:param name="title"/>
 <xsl:param name="x-axis-label"/>
 <xsl:param name="y-stop"/>
 <xsl:param name="y-step-param"/>
 <xsl:param name="y-start-param"/>
 <xsl:param name="y-scale-param"/>
 <xsl:variable name="y-axis-label" select="'# of Publications'"/>
 <xsl:variable name="bar-width" select="'20'"/>
 <xsl:variable name="y-frame" select="'200'"/>
 <xsl:variable name="y-scale-default" select="'10'"/>
 <xsl:variable name="y-step-default" select="'2'"/>
 <xsl:variable name="y-start-default" select="'1'"/>

 <xsl:variable name="y-scale">
  <xsl:choose>
   <xsl:when test="string-length($y-scale-param)=0">
    <xsl:value-of select="$y-scale-default"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$y-scale-param"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>

 <xsl:variable name="y-step">
  <xsl:choose>
   <xsl:when test="string-length($y-step-param)=0">
    <xsl:value-of select="$y-step-default"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$y-step-param"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>

 <xsl:variable name="y-start">
  <xsl:choose>
   <xsl:when test="string-length($y-start-param)=0">
    <xsl:value-of select="$y-start-default"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$y-start-param"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>

 <xsl:template match="/rdf:RDF">
  <xsl:variable name="maxXValue">
   <xsl:call-template name="max">
    <xsl:with-param name="start-node" select="'1'"/>
    <xsl:with-param name="relevant-nodeset" select="rdf:Description/*[local-name()=$property]"/>
   </xsl:call-template>
  </xsl:variable>
  <svg viewBox="{concat('-25 ',$y-frame - $y-stop * $y-scale - 40,' ',ceiling($maxXValue div $group-by) * $bar-width + 25,' 240')}">
   <style type="text/css"><![CDATA[
   .x-label {fill:#666;font-family:Verdana;font-size:8}
   .y-label {fill:#666;font-family:Verdana;font-size:8}
   .y-guide {stroke:#ccc;stroke-width:1}
   ]]></style>
   <title><xsl:value-of select="$title"/></title>
   <defs>
    <linearGradient id="grad" gradientTransform="rotate(90)">
     <stop offset="5%" stop-color="#f00"/>
     <stop offset="95%" stop-color="#f90"/>
    </linearGradient>
   </defs>
   <text x="30" y="{$y-frame + 25}" class="x-label">
    <xsl:value-of select="$x-axis-label"/>
   </text>
   <text x="-25" y="{$y-frame - 10}" class="y-label" transform="{concat('rotate(-90,-25,',$y-frame - 10,')')}">
    <xsl:value-of select="$y-axis-label"/>
   </text>
   <xsl:call-template name="graph-info">
    <xsl:with-param name="max-x" select="$maxXValue"/>
   </xsl:call-template>
  </svg>
 </xsl:template>

 <xsl:template name="graph-info">
  <xsl:param name="max-x"/>
  <xsl:variable name="bar-label-mod" select="$max-x mod $group-by"/>
  <xsl:variable name="bar-label-max">
   <xsl:choose>
    <xsl:when test="$bar-label-mod &gt; 0">
     <xsl:value-of select="ceiling($max-x div $group-by)"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="($max-x div $group-by) + 1"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>

  <rect width="{$bar-width * $bar-label-max}" height="2" fill="#000" x="0" y="{$y-frame}"/>
  <rect width="2" height="{$y-stop * $y-scale + 12}" fill="#000" x="-2" y="{$y-frame - $y-stop * $y-scale - 10}"/>

  <g id="y-labels" transform="translate(-14,4)">
   <xsl:call-template name="draw-y-labels">
    <xsl:with-param name="start-label" select="$y-start"/>
    <xsl:with-param name="last-label" select="$y-stop"/>
   </xsl:call-template>
  </g>

  <g id="y-bar-guides">
   <xsl:call-template name="draw-y-guides">
    <xsl:with-param name="start-label" select="$y-start"/>
    <xsl:with-param name="last-label" select="$y-stop"/>
    <xsl:with-param name="width" select="$bar-width * $bar-label-max"/>
   </xsl:call-template>
  </g>

  <g id="bars">
   <xsl:call-template name="draw-bars">
    <xsl:with-param name="start-label" select="'0'"/>
    <xsl:with-param name="last-label" select="$bar-label-max - 1"/>
   </xsl:call-template>
  </g>

  <g id="x-labels" transform="translate(-5,0)">
   <xsl:call-template name="draw-x-labels">
    <xsl:with-param name="start-label" select="'0'"/>
    <xsl:with-param name="last-label" select="$bar-label-max"/>
   </xsl:call-template>
  </g>
 </xsl:template>

 <xsl:template name="max">
  <xsl:param name="current-max"/>
  <xsl:param name="relevant-nodeset"/>
  <xsl:param name="start-node"/>
  <xsl:variable name="local-max">
   <xsl:choose>
    <xsl:when test="string-length($current-max)=0">
     <xsl:value-of select="$relevant-nodeset[position()=$start-node]"/>
    </xsl:when>
    <xsl:when test="$relevant-nodeset[position()=$start-node]&gt;$current-max">
     <xsl:value-of select="$relevant-nodeset[position()=$start-node]"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$current-max"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:choose>
   <xsl:when test="number($start-node) &gt; count($relevant-nodeset)">
    <xsl:value-of select="$current-max"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:call-template name="max">
     <xsl:with-param name="current-max" select="$local-max"/>
     <xsl:with-param name="relevant-nodeset" select="$relevant-nodeset"/>
     <xsl:with-param name="start-node" select="$start-node + 1"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="draw-x-labels">
  <xsl:param name="start-label"/>
  <xsl:param name="last-label"/>
  <xsl:choose>
   <xsl:when test="($start-label div $group-by) &gt; $last-label">
    <xsl:value-of select="''"/>
   </xsl:when>
   <xsl:otherwise>
    <text x="{($start-label div $group-by) * $bar-width}" y="{$y-frame + 10}" class="x-label">
     <xsl:value-of select="$start-label"/>
    </text>
    <xsl:call-template name="draw-x-labels">
     <xsl:with-param name="start-label" select="$start-label + $group-by"/>
     <xsl:with-param name="last-label" select="$last-label"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="draw-bars">
  <xsl:param name="start-label"/>
  <xsl:param name="last-label"/>
  <xsl:choose>
   <xsl:when test="($start-label div $group-by) &gt; $last-label">
    <xsl:value-of select="''"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:variable name="bin-count">
     <xsl:value-of select="count(//*[local-name()=$property][number(text()) &gt;= number($start-label) and number(text()) &lt;= number($start-label + $group-by - 1)])"/>
    </xsl:variable>
    <rect width="{$bar-width}" height="{$y-scale*$bin-count}" fill="url(#grad)" x="{$bar-width * ($start-label div $group-by)}" y="{$y-frame - $y-scale * $bin-count}"/>
    <xsl:call-template name="draw-bars">
     <xsl:with-param name="start-label" select="$start-label + $group-by"/>
     <xsl:with-param name="last-label" select="$last-label"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="draw-y-labels">
  <xsl:param name="start-label"/>
  <xsl:param name="last-label"/>
  <xsl:choose>
   <xsl:when test="$start-label &gt; $last-label">
    <xsl:value-of select="''"/>
   </xsl:when>
   <xsl:otherwise>
    <text class="y-label" x="0" y="{$y-frame - $y-scale * $start-label}">
     <xsl:value-of select="$start-label"/>
    </text>
    <xsl:call-template name="draw-y-labels">
     <xsl:with-param name="start-label" select="$start-label + $y-step"/>
     <xsl:with-param name="last-label" select="$last-label"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="draw-y-guides">
  <xsl:param name="start-label"/>
  <xsl:param name="last-label"/>
  <xsl:param name="width"/>
  <xsl:choose>
   <xsl:when test="$start-label &gt; $last-label">
    <xsl:value-of select="''"/>
   </xsl:when>
   <xsl:otherwise>
    <line x2="{$width}" y1="{$y-frame - $y-scale * $start-label}" y2="{$y-frame - $y-scale * $start-label}" class="y-guide"/>
    <xsl:call-template name="draw-y-guides">
     <xsl:with-param name="start-label" select="$start-label + $y-step"/>
     <xsl:with-param name="last-label" select="$last-label"/>
     <xsl:with-param name="width" select="$width"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
