piechart.xsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. xmlns:math="http://exslt.org/math" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl math">
  18. <xsl:import href="common.xsl" />
  19. <!-- Constants -->
  20. <xsl:variable name="MIN_VERTICAL_SPAN" select="100" />
  21. <xsl:variable name="CUT_OFF_PERCENTAGE" select="0.05" />
  22. <xsl:variable name="DEGREE_RADIAN_RATIO" select="0.0175" /> <!-- 1 degree = 0.0175 radians -->
  23. <xsl:variable name="HALF_CIRCLE_ANGLE" select="3.1415" /> <!-- 180 degrees = 3.14 radians -->
  24. <xsl:variable name="FULL_CIRCLE_ANGLE" select="$HALF_CIRCLE_ANGLE*2" /> <!-- 360 degrees = 6.29 radians -->
  25. <!--
  26. Prints a simple pie chart with legend
  27. 1. Will not work on transformers not supporting EXSLT extension
  28. 2. Percentages may not be accurate
  29. -->
  30. <xsl:template name="pieChart">
  31. <xsl:param name="xData" />
  32. <xsl:param name="yData" />
  33. <xsl:param name="width" select="'100%'" />
  34. <xsl:param name="height" select="'100%'" />
  35. <xsl:param name="viewBoxWidth" select="300" />
  36. <xsl:param name="viewBoxHeight" select="300" />
  37. <xsl:param name="padding" select="10" />
  38. <xsl:param name="verticalSpan" select="100" />
  39. <xsl:param name="othersLabel" select="'Others'" />
  40. <xsl:variable name="xCount" select="count($xData)" />
  41. <xsl:variable name="yCount" select="count($yData)" />
  42. <xsl:variable name="_verticalSpan">
  43. <xsl:choose>
  44. <xsl:when test="$verticalSpan &lt; $MIN_VERTICAL_SPAN">
  45. <xsl:value-of select="$MIN_VERTICAL_SPAN" />
  46. </xsl:when>
  47. <xsl:otherwise>
  48. <xsl:value-of select="$verticalSpan" />
  49. </xsl:otherwise>
  50. </xsl:choose>
  51. </xsl:variable>
  52. <svg:svg version="1.1" width="{$width}" height="{$height}" preserveAspectRatio="xMinYMid" viewBox="0 0 {$viewBoxWidth} {$viewBoxHeight}"
  53. xmlns:svg="http://www.w3.org/2000/svg">
  54. <xsl:if test="$xCount &gt; 0 and $yCount &gt; 0">
  55. <xsl:variable name="_aggregatedData">
  56. <xsl:call-template name="_aggregate">
  57. <xsl:with-param name="xData" select="$xData" />
  58. <xsl:with-param name="yData" select="$yData" />
  59. <xsl:with-param name="othersLabel" select="$othersLabel" />
  60. </xsl:call-template>
  61. </xsl:variable>
  62. <xsl:variable name="aggregatedData" select="exsl:node-set($_aggregatedData)/*" />
  63. <xsl:call-template name="_printPies">
  64. <xsl:with-param name="data" select="$aggregatedData" />
  65. <xsl:with-param name="padding" select="$padding" />
  66. <xsl:with-param name="radius" select="$verticalSpan div 2" />
  67. </xsl:call-template>
  68. <xsl:call-template name="_printLegend">
  69. <xsl:with-param name="data" select="$aggregatedData" />
  70. <xsl:with-param name="xStart" select="$padding*2.5+$_verticalSpan" />
  71. <xsl:with-param name="yStart" select="$padding" />
  72. </xsl:call-template>
  73. </xsl:if>
  74. </svg:svg>
  75. </xsl:template>
  76. <!-- Aggregates data to be printed; merges small values under 'Others' -->
  77. <xsl:template name="_aggregate">
  78. <xsl:param name="xData" />
  79. <xsl:param name="yData" />
  80. <xsl:param name="othersLabel" />
  81. <xsl:variable name="_transformedData">
  82. <xsl:call-template name="_transform">
  83. <xsl:with-param name="xData" select="$xData" />
  84. <xsl:with-param name="yData" select="$yData" />
  85. </xsl:call-template>
  86. </xsl:variable>
  87. <xsl:variable name="transformedData" select="exsl:node-set($_transformedData)/*" />
  88. <!-- filter 'other' items -->
  89. <xsl:for-each select="$transformedData">
  90. <xsl:sort select="." order="descending" data-type="number" />
  91. <xsl:if test="name()='item'">
  92. <xsl:copy-of select="." />
  93. </xsl:if>
  94. </xsl:for-each>
  95. <!-- add 'Others' with summed up value of 'other' items -->
  96. <xsl:variable name="sumOfOthers" select="sum($transformedData[name()='other'])" />
  97. <xsl:if test="$sumOfOthers &gt; 0">
  98. <xsl:element name="item">
  99. <xsl:attribute name="name">
  100. <xsl:value-of select="$othersLabel" />
  101. </xsl:attribute>
  102. <xsl:value-of select="$sumOfOthers" />
  103. </xsl:element>
  104. </xsl:if>
  105. </xsl:template>
  106. <!-- Converts raw data to a unified result-tree for ease of processing -->
  107. <xsl:template name="_transform">
  108. <xsl:param name="xData" />
  109. <xsl:param name="yData" />
  110. <xsl:variable name="total" select="sum($yData[. &gt; 0])" />
  111. <xsl:for-each select="$yData">
  112. <xsl:variable name="index" select="position()" />
  113. <xsl:choose>
  114. <xsl:when test=". &lt;= 0">
  115. <!-- ignore negative values -->
  116. </xsl:when>
  117. <!-- collect insignificant values (< cut-off) as 'other' -->
  118. <xsl:when test="(. div $total) &lt; $CUT_OFF_PERCENTAGE">
  119. <xsl:element name="other">
  120. <xsl:value-of select="." />
  121. </xsl:element>
  122. </xsl:when>
  123. <!-- collect significant values (> cut-off%) as 'item' -->
  124. <xsl:otherwise>
  125. <xsl:element name="item">
  126. <xsl:attribute name="name">
  127. <xsl:value-of select="$xData[$index]" />
  128. </xsl:attribute>
  129. <xsl:value-of select="." />
  130. </xsl:element>
  131. </xsl:otherwise>
  132. </xsl:choose>
  133. </xsl:for-each>
  134. </xsl:template>
  135. <!-- Prints each pie recursively -->
  136. <xsl:template name="_printPies">
  137. <xsl:param name="data" />
  138. <xsl:param name="total" select="sum($data)" />
  139. <xsl:param name="padding" />
  140. <xsl:param name="radius" />
  141. <xsl:param name="index" select="1" />
  142. <xsl:variable name="rotationInDegrees" select="270+(360*(sum($data[position()&lt;$index]) div $total))" />
  143. <xsl:variable name="angleInRadians" select="($data[$index] div $total)*$FULL_CIRCLE_ANGLE" />
  144. <xsl:call-template name="_printPie">
  145. <xsl:with-param name="index" select="$index" />
  146. <xsl:with-param name="rotationInDegrees" select="$rotationInDegrees" />
  147. <xsl:with-param name="angleInRadians" select="$angleInRadians" />
  148. <xsl:with-param name="padding" select="$padding" />
  149. <xsl:with-param name="radius" select="$radius" />
  150. </xsl:call-template>
  151. <xsl:call-template name="_printLabel">
  152. <xsl:with-param name="data" select="$data" />
  153. <xsl:with-param name="total" select="$total" />
  154. <xsl:with-param name="index" select="$index" />
  155. <xsl:with-param name="rotationInRadians" select="$rotationInDegrees*$DEGREE_RADIAN_RATIO" />
  156. <xsl:with-param name="angleInRadians" select="$angleInRadians div 2" />
  157. <xsl:with-param name="padding" select="$padding" />
  158. <xsl:with-param name="radius" select="$radius" />
  159. </xsl:call-template>
  160. <xsl:if test="$index &lt; count($data)">
  161. <xsl:call-template name="_printPies">
  162. <xsl:with-param name="data" select="$data" />
  163. <xsl:with-param name="total" select="$total" />
  164. <xsl:with-param name="padding" select="$padding" />
  165. <xsl:with-param name="index" select="$index+1" />
  166. <xsl:with-param name="radius" select="$radius" />
  167. </xsl:call-template>
  168. </xsl:if>
  169. </xsl:template>
  170. <!-- Prints pie -->
  171. <xsl:template name="_printPie">
  172. <xsl:param name="index" />
  173. <xsl:param name="rotationInDegrees" />
  174. <xsl:param name="angleInRadians" />
  175. <xsl:param name="padding" />
  176. <xsl:param name="radius" />
  177. <xsl:variable name="colour">
  178. <xsl:call-template name="colour">
  179. <xsl:with-param name="index" select="$index" />
  180. </xsl:call-template>
  181. </xsl:variable>
  182. <xsl:variable name="largeArcFlag">
  183. <xsl:choose>
  184. <xsl:when test="$angleInRadians &gt; $HALF_CIRCLE_ANGLE">
  185. <xsl:value-of select="1" />
  186. </xsl:when>
  187. <xsl:otherwise>
  188. <xsl:value-of select="0" />
  189. </xsl:otherwise>
  190. </xsl:choose>
  191. </xsl:variable>
  192. <xsl:variable name="centre" select="$padding+$radius" />
  193. <svg:path fill="{$colour}" stroke="white" stroke-width="1"
  194. transform="translate({$centre} {$centre}) rotate({$rotationInDegrees})"
  195. d="M {$radius} 0 A {$radius} {$radius} 0 {$largeArcFlag} 1 {$radius*math:cos($angleInRadians)} {$radius*math:sin($angleInRadians)} L 0 0 Z"
  196. xmlns:svg="http://www.w3.org/2000/svg" />
  197. </xsl:template>
  198. <!-- Prints label inside the pie -->
  199. <xsl:template name="_printLabel">
  200. <xsl:param name="data" />
  201. <xsl:param name="total" />
  202. <xsl:param name="index" />
  203. <xsl:param name="rotationInRadians" />
  204. <xsl:param name="angleInRadians" />
  205. <xsl:param name="padding" />
  206. <xsl:param name="radius" />
  207. <xsl:variable name="labelRadius" select="$radius*0.7" />
  208. <xsl:variable name="cosine" select="math:cos($rotationInRadians)" />
  209. <xsl:variable name="sine" select="math:sin($rotationInRadians)" />
  210. <xsl:variable name="x" select="math:cos($angleInRadians)*$labelRadius" />
  211. <xsl:variable name="y" select="math:sin($angleInRadians)*$labelRadius" />
  212. <xsl:variable name="centre" select="$padding+$radius" />
  213. <svg:text text-anchor="middle" fill="black" transform="translate({$centre} {$centre})"
  214. font-family="{$FONT}" font-size="{$FONT_SIZE}" xmlns:svg="http://www.w3.org/2000/svg">
  215. <xsl:attribute name="x">
  216. <xsl:value-of select="($x*$cosine)-($y*$sine)" />
  217. </xsl:attribute>
  218. <xsl:attribute name="y">
  219. <xsl:value-of select="($x*$sine)+($y*$cosine)" />
  220. </xsl:attribute>
  221. <xsl:value-of select="round(100*($data[$index] div $total))" />
  222. <xsl:text>%</xsl:text>
  223. </svg:text>
  224. </xsl:template>
  225. <!-- Prints legend -->
  226. <xsl:template name="_printLegend">
  227. <xsl:param name="data" />
  228. <xsl:param name="xStart" />
  229. <xsl:param name="yStart" />
  230. <xsl:for-each select="$data">
  231. <xsl:variable name="y" select="$yStart+(position()-1)*8" />
  232. <xsl:variable name="colour">
  233. <xsl:call-template name="colour">
  234. <xsl:with-param name="index" select="position()" />
  235. </xsl:call-template>
  236. </xsl:variable>
  237. <svg:rect x="{$xStart}" y="{$y}" rx="1" ry="1" width="10" height="5" fill="{$colour}"
  238. stroke="black" stroke-width="0.5" xmlns:svg="http://www.w3.org/2000/svg" />
  239. <svg:text text-anchor="start" x="{$xStart+15}" y="{$y + 4}" font-family="{$FONT}" font-size="{$FONT_SIZE}"
  240. xmlns:svg="http://www.w3.org/2000/svg">
  241. <xsl:value-of select="@name" />
  242. <xsl:text> (</xsl:text><xsl:value-of select="." /><xsl:text>)</xsl:text>
  243. </svg:text>
  244. </xsl:for-each>
  245. </xsl:template>
  246. </xsl:stylesheet>