<?xml version="1.0"?>
<!-- ============================================================ -->
<!--                                                              -->
<!-- 	This file makes a part of RenderX XSL Test Suite          -->
<!--                                                              -->
<!--    Author: Alexander Peshkov                                 -->
<!--                                                              -->
<!--    (c) RenderX, 2003                                         -->
<!--                                                              -->
<!-- ============================================================ -->
<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">
	<xsl:import href="generic.xsl"/>
	
	<xsl:output indent="yes"/>

    <xsl:template match="meteodata">
      <fo:table width="100%" table-layout="fixed">
        <fo:table-column column-width="2in"/>
        <fo:table-body display-align="center">
          <fo:table-cell>
            <xsl:apply-templates select="." mode="text"/>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block>
              <fo:instream-foreign-object>
                <xsl:apply-templates select="." mode="svg"/>
              </fo:instream-foreign-object>
            </fo:block>
          </fo:table-cell>
        </fo:table-body>
      </fo:table>
    </xsl:template>

  
    <xsl:template match="meteodata" mode="text">      
      <fo:table space-before="1pt" text-indent="0pt" border="1pt solid gray">
        <fo:table-column column-width="0.4in" text-align="center"/>
        <fo:table-column column-width="0.5in" text-align="center"/>
        <fo:table-column column-width="0.5in" text-align="center"/>
        <fo:table-column column-width="0.5in" text-align="center"/>
        <fo:table-header background-color="silver" font-size="10pt" font-weight="bold" display-align="center">
          <fo:table-row>
            <fo:table-cell number-rows-spanned="2" text-align="from-table-column()">
              <fo:block>Date</fo:block>
            </fo:table-cell>
            <fo:table-cell number-columns-spanned="3" text-align="center">
              <fo:block>Temperature, &#x00B0;C</fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row>
            <fo:table-cell text-align="from-table-column()">
              <fo:block>min</fo:block>
            </fo:table-cell>
            <fo:table-cell text-align="from-table-column()">
              <fo:block>avg</fo:block>
            </fo:table-cell>
            <fo:table-cell text-align="from-table-column()">
              <fo:block>max</fo:block>
            </fo:table-cell>
          </fo:table-row>
        </fo:table-header>
      
        <fo:table-body font-size="10pt" >
          <xsl:apply-templates select="point" mode="text"/>
        </fo:table-body>
      </fo:table>              
    </xsl:template>
  
  <xsl:template match="point" mode="text">
    <fo:table-row>
      <fo:table-cell text-align="from-table-column()">
        <fo:block><xsl:value-of select="@date"/></fo:block>
      </fo:table-cell>
      <fo:table-cell text-align="from-table-column()">
        <fo:block><xsl:value-of select="@min"/></fo:block>
      </fo:table-cell>
      <fo:table-cell text-align="from-table-column()">
        <fo:block><xsl:value-of select="@avg"/></fo:block>
      </fo:table-cell>
      <fo:table-cell text-align="from-table-column()">
        <fo:block><xsl:value-of select="@max"/></fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>
  
  <xsl:template match="meteodata" mode="svg">        
    <svg:svg width="5in" height="5in" viewBox="-60 -500 700 800">
      <!-- Draw axes -->
      <xsl:call-template name="draw-axes"/>
      
      <xsl:apply-templates select="point" mode="svg"/>      
      
      <svg:polyline 
         style="fill:none; stroke-width:8; stroke: maroon; stroke-linecap: round; stroke-linejoin: round">
        <xsl:attribute name="points">
          <xsl:for-each select="point">
            <xsl:sort select="@date" data-type="number"/>
            <xsl:value-of select="@date * 20"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="-10 * @avg"/>
            <xsl:text> </xsl:text>            
          </xsl:for-each>
        </xsl:attribute>
      </svg:polyline>
    </svg:svg>
  </xsl:template>
  
  <xsl:template match="point" mode="svg">
    <xsl:variable name="x" select="@date * 20"/>
    <xsl:variable name="ymax" select="-10 * @max"/>
    <xsl:variable name="ymin" select="-10 * @min"/>
    
    <svg:line x1="{$x}" x2="{$x}" y1="{$ymax}" y2="{$ymin}"
        style="stroke-width:6; stroke: blue; stroke-linecap: butt"/>
    <svg:line x1="{$x - 5}" x2="{$x + 5}" y1="{$ymax}" y2="{$ymax}"
        style="stroke-width:1; stroke: blue; stroke-linecap: butt"/>
    <svg:line x1="{$x - 5}" x2="{$x + 5}" y1="{$ymin}" y2="{$ymin}"
        style="stroke-width:1; stroke: blue; stroke-linecap: butt"/>        
  </xsl:template>

  <xsl:template name="draw-axes">    
    <xsl:variable name="len" select="count(//point)"/>
    <svg:line style="stroke:black; stroke-width:4; stroke-linecap:square"
       x1="0" y1="-450" x2="0" y2="250"/>
    <svg:line style="stroke:black; stroke-width:4; stroke-linecap:square"
       x1="0" y1="0" x2="{$len * 20}" y2="0"/>

    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">-20</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">-15</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">-10</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">-5</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">0</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">5</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">10</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">15</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">20</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">25</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">30</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">35</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="draw-notch">
      <xsl:with-param name="value">40</xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template name="draw-notch">
    <xsl:param name="value">0</xsl:param>
    <xsl:variable name="y" select="-10 * $value"/>
 
    <svg:line style="stroke:black; stroke-width:2; stroke-linecap:round"
       x1="-10" y1="{$y}" x2="0" y2="{$y}"/>
    <svg:text font-family="Helvetica" font-size="25"
       x="-15" y="{$y + 10}" text-anchor="end">
       <xsl:value-of select="$value"/>
       <xsl:text>&#x00B0;</xsl:text>
    </svg:text>
  </xsl:template>  
</xsl:stylesheet>
