linechart.xsl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Original Author: Frankline Francis
  4. Copyright (c) 2012, Imaginea. All rights reserved.
  5. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND DEVELOPERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  6. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  7. IN NO EVENT SHALL THE COPYRIGHT OWNER OR DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  8. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  9. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11. Redistribution and use, with or without modification, are permitted provided that the following conditions are met:
  12. # Redistribution of source code must retain the above copyright notice, this list of conditions and the disclaimer.
  13. # Neither the name of Imaginea nor the names of the developers may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. -->
  16. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  17. <xsl:import href="common.xsl" />
  18. <!-- Constants -->
  19. <xsl:variable name="MIN_VERTICAL_SPAN" select="75" />
  20. <xsl:variable name="POINT_RADIUS" select="1.5" />
  21. <!-- Prints a simple line chart with grid -->
  22. <xsl:template name="lineChart">
  23. <xsl:param name="xData" />
  24. <xsl:param name="yData" />
  25. <xsl:param name="lineColour" select="'black'" />
  26. <xsl:param name="pointColour" select="'red'" />
  27. <xsl:param name="width" select="'100%'" />
  28. <xsl:param name="height" select="'100%'" />
  29. <xsl:param name="viewBoxWidth" select="300" />
  30. <xsl:param name="viewBoxHeight" select="300" />
  31. <xsl:param name="xDelta" select="15" />
  32. <xsl:param name="leftPadding" select="20" />
  33. <xsl:param name="rightPadding" select="10" />
  34. <xsl:param name="verticalSpan" select="100" />
  35. <xsl:variable name="xCount" select="count($xData)" />
  36. <xsl:variable name="yCount" select="count($yData)" />
  37. <xsl:variable name="_verticalSpan">
  38. <xsl:choose>
  39. <xsl:when test="$verticalSpan &lt; $MIN_VERTICAL_SPAN">
  40. <xsl:value-of select="$MIN_VERTICAL_SPAN" />
  41. </xsl:when>
  42. <xsl:otherwise>
  43. <xsl:value-of select="$verticalSpan" />
  44. </xsl:otherwise>
  45. </xsl:choose>
  46. </xsl:variable>
  47. <svg:svg version="1.1" width="{$width}" height="{$height}" preserveAspectRatio="xMinYMin" viewBox="0 0 {$viewBoxWidth} {$viewBoxHeight}"
  48. xmlns:svg="http://www.w3.org/2000/svg">
  49. <xsl:if test="$xCount &gt; 0 and $yCount &gt; 0">
  50. <xsl:variable name="yDataMin">
  51. <xsl:call-template name="minimum">
  52. <xsl:with-param name="numbers" select="$yData" />
  53. </xsl:call-template>
  54. </xsl:variable>
  55. <xsl:variable name="yDataMax">
  56. <xsl:call-template name="maximum">
  57. <xsl:with-param name="numbers" select="$yData" />
  58. </xsl:call-template>
  59. </xsl:variable>
  60. <xsl:variable name="yScale" select="$_verticalSpan div ($yDataMax - $yDataMin)" />
  61. <xsl:variable name="yDelta" select="round(($yDataMax - $yDataMin) div $yCount)*2" />
  62. <xsl:variable name="xMin" select="$leftPadding" />
  63. <xsl:variable name="xMax" select="$xMin+count($yData)*$xDelta+$rightPadding" />
  64. <xsl:variable name="xStart" select="$xMin+$xDelta div 2" />
  65. <xsl:variable name="yMin">
  66. <xsl:choose>
  67. <xsl:when test="$yDataMin &lt; 0">
  68. <xsl:value-of select="$yDataMin - $yDelta" />
  69. </xsl:when>
  70. <xsl:otherwise>
  71. <xsl:value-of select="0" />
  72. </xsl:otherwise>
  73. </xsl:choose>
  74. </xsl:variable>
  75. <xsl:variable name="yMax" select="$yDataMax+$yDelta" />
  76. <xsl:variable name="bottom">
  77. <xsl:choose>
  78. <xsl:when test="$yDataMin &lt; 0">
  79. <xsl:value-of select="- $_verticalSpan" />
  80. </xsl:when>
  81. <xsl:otherwise>
  82. <xsl:value-of select="0" />
  83. </xsl:otherwise>
  84. </xsl:choose>
  85. </xsl:variable>
  86. <xsl:variable name="yStep" select="$yDelta*$yScale" />
  87. <xsl:variable name="totalHeight" select="round($_verticalSpan+$bottom+2*$yStep)" />
  88. <xsl:variable name="yStart">
  89. <xsl:choose>
  90. <xsl:when test="$yDataMin &lt; 0">
  91. <xsl:value-of select="$bottom - $yStep" />
  92. </xsl:when>
  93. <xsl:otherwise>
  94. <xsl:value-of select="$bottom" />
  95. </xsl:otherwise>
  96. </xsl:choose>
  97. </xsl:variable>
  98. <xsl:variable name="yCentre">
  99. <xsl:choose>
  100. <xsl:when test="$yMin &lt; 0">
  101. <xsl:value-of select="floor($yScale*($yMax + $yDelta div 2))" />
  102. </xsl:when>
  103. <xsl:otherwise>
  104. <xsl:value-of select="floor($yScale*$yMax)" />
  105. </xsl:otherwise>
  106. </xsl:choose>
  107. </xsl:variable>
  108. <!-- Print centre -->
  109. <svg:circle cx="{$xMin}" cy="{$yCentre}" r="1" />
  110. <!-- Print y-axis -->
  111. <xsl:call-template name="printYAxis">
  112. <xsl:with-param name="index" select="$yMin" />
  113. <xsl:with-param name="step" select="$yDelta" />
  114. <xsl:with-param name="xMin" select="$xMin" />
  115. <xsl:with-param name="xMax" select="$xMax" />
  116. <xsl:with-param name="yMin" select="$yMin" />
  117. <xsl:with-param name="yMax" select="$yMax" />
  118. <xsl:with-param name="yScale" select="$yScale" />
  119. </xsl:call-template>
  120. <svg:g transform="translate(0 {$totalHeight}) scale(1 -1)">
  121. <svg:line x1="{$xMin}" y1="{$yStart}" x2="{$xMin}" y2="{$totalHeight}"
  122. stroke="black" stroke-width="2" />
  123. </svg:g>
  124. <!-- Print points -->
  125. <svg:g transform="translate(0 {$yCentre}) scale(1 -1)">
  126. <xsl:call-template name="_printPoints">
  127. <xsl:with-param name="yData" select="$yData" />
  128. <xsl:with-param name="xMin" select="$xStart" />
  129. <xsl:with-param name="xDelta" select="$xDelta" />
  130. <xsl:with-param name="yScale" select="$yScale" />
  131. <xsl:with-param name="lineColour" select="$lineColour" />
  132. <xsl:with-param name="pointColour" select="$pointColour" />
  133. </xsl:call-template>
  134. </svg:g>
  135. <!-- Print x-axis -->
  136. <svg:g transform="translate(0 {$yCentre})">
  137. <xsl:call-template name="printXAxis">
  138. <xsl:with-param name="xData" select="$xData" />
  139. <xsl:with-param name="step" select="$xDelta" />
  140. <xsl:with-param name="xMin" select="$xStart" />
  141. <xsl:with-param name="xMax" select="$yCount" />
  142. </xsl:call-template>
  143. <svg:line x1="{$xMin}" y1="0" x2="{$xMax}" y2="0" stroke="black" stroke-width="2" />
  144. </svg:g>
  145. </xsl:if>
  146. </svg:svg>
  147. </xsl:template>
  148. <!-- Prints points and joins them -->
  149. <xsl:template name="_printPoints">
  150. <xsl:param name="yData" />
  151. <xsl:param name="index" select="1" />
  152. <xsl:param name="xMin" />
  153. <xsl:param name="xDelta" />
  154. <xsl:param name="yScale" />
  155. <xsl:param name="lineColour" />
  156. <xsl:param name="pointColour" />
  157. <xsl:variable name="x" select="$xMin+($index - 1)*$xDelta" />
  158. <xsl:variable name="y" select="$yData[$index]*$yScale" />
  159. <xsl:if test="$yData[$index+1]">
  160. <svg:line x1="{$x}" y1="{$y}" x2="{$xMin+($index)*$xDelta}" y2="{$yData[$index+1]*$yScale}"
  161. stroke="{$lineColour}" stroke-width="1" xmlns:svg="http://www.w3.org/2000/svg" />
  162. </xsl:if>
  163. <svg:circle cx="{$x}" cy="{$y}" r="{$POINT_RADIUS}" fill="white" stroke="{$pointColour}" stroke-width="1"
  164. xmlns:svg="http://www.w3.org/2000/svg" />
  165. <xsl:if test="$index &lt; count($yData)">
  166. <xsl:call-template name="_printPoints">
  167. <xsl:with-param name="yData" select="$yData" />
  168. <xsl:with-param name="index" select="$index+1" />
  169. <xsl:with-param name="xMin" select="$xMin" />
  170. <xsl:with-param name="xDelta" select="$xDelta" />
  171. <xsl:with-param name="yScale" select="$yScale" />
  172. <xsl:with-param name="lineColour" select="$lineColour" />
  173. <xsl:with-param name="pointColour" select="$pointColour" />
  174. </xsl:call-template>
  175. </xsl:if>
  176. </xsl:template>
  177. </xsl:stylesheet>