Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/delibero.git

Replaced copyright sine function with own implementation.

Author Dave Jarvis <email>
Date 2014-08-01 12:06:04 GMT-0700
Commit 00a977ab69a46c4246f57ae17301efac56b318e8
Parent 2a062c8
xml/chart.xsl
<div class="chart">
- <svg:svg width="30%" height="30%" viewBox="0 0 {$pie_width} {$pie_height}" version="1.1">
- <svg:g stroke-width="0" fill="#000" stroke="#000">
- <xsl:for-each select="data">
- <xsl:variable name="piece" select="position()" />
+ <div class="graph">
+ <svg:svg width="30%" height="30%" viewBox="0 0 {$pie_width} {$pie_height}" version="1.1">
+ <svg:g stroke-width="0" fill="#000" stroke="#000">
+ <xsl:for-each select="data">
+ <xsl:variable name="piece" select="position()" />
- <!-- Calculate pie piece fill colour. -->
- <xsl:variable name="fill">
- <xsl:call-template name="fill">
- <xsl:with-param name="piece" select="$piece" />
- <xsl:with-param name="pieces" select="$pieces" />
- <xsl:with-param name="colour" select="$base_colour" />
- </xsl:call-template>
- </xsl:variable>
+ <!-- Calculate pie piece fill colour. -->
+ <xsl:variable name="fill">
+ <xsl:call-template name="fill">
+ <xsl:with-param name="piece" select="$piece" />
+ <xsl:with-param name="pieces" select="$pieces" />
+ <xsl:with-param name="colour" select="$base_colour" />
+ </xsl:call-template>
+ </xsl:variable>
- <xsl:for-each select="value">
- <!-- Pie piece item number. -->
- <xsl:variable name="item" select="position()" />
+ <xsl:for-each select="value">
+ <!-- Pie piece item number. -->
+ <xsl:variable name="item" select="position()" />
- <xsl:variable name="title"><xsl:value-of select="../name" /></xsl:variable>
- <xsl:variable name="percent" select="concat($title,' (',format-number(. div $total, '0.00%'),')')" />
+ <xsl:variable name="title"><xsl:value-of select="../name" /></xsl:variable>
+ <xsl:variable name="percent" select="concat($title,' (',format-number(. div $total, '0.00%'),')')" />
- <xsl:variable name="x" select="0" />
- <xsl:variable name="y" select="0" />
- <xsl:variable name="rx" select="$pie_width * 0.5" />
- <xsl:variable name="ry" select="$pie_height * 0.5" />
-
- <svg:rect x="{360 * (1 - sum(//data[position() &lt;= $piece -1]/value[position() = $item]) div $total)}" />
-
- <xsl:variable name="deg">
- <xsl:choose>
- <!-- Whole pie. -->
- <xsl:when test="$piece = 1">360</xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="360 * (1 - sum(//data[position() &lt;= $piece -1]/value[position() = $item]) div $total)" />
- </xsl:otherwise>
- </xsl:choose>
- </xsl:variable>
- <xsl:variable name="c">
- <xsl:call-template name="sine">
- <xsl:with-param name="deg" select="$deg" />
- </xsl:call-template>
- </xsl:variable>
- <xsl:variable name="s">
- <xsl:call-template name="sine">
- <xsl:with-param name="deg" select="-$deg - 90" />
- </xsl:call-template>
- </xsl:variable>
+ <xsl:variable name="x" select="0" />
+ <xsl:variable name="y" select="0" />
+ <xsl:variable name="rx" select="$pie_width * 0.5" />
+ <xsl:variable name="ry" select="$pie_height * 0.5" />
+
+ <svg:rect x="{360 * (1 - sum(//data[position() &lt;= $piece -1]/value[position() = $item]) div $total)}" />
+
+ <xsl:variable name="deg">
+ <xsl:choose>
+ <!-- Whole pie. -->
+ <xsl:when test="$piece = 1">360</xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="360 * (1 - sum(//data[position() &lt;= $piece -1]/value[position() = $item]) div $total)" />
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:variable>
+ <xsl:variable name="c">
+ <xsl:call-template name="sin">
+ <xsl:with-param name="degrees" select="$deg" />
+ </xsl:call-template>
+ </xsl:variable>
+ <xsl:variable name="s">
+ <xsl:call-template name="sin">
+ <xsl:with-param name="degrees" select="-$deg - 90" />
+ </xsl:call-template>
+ </xsl:variable>
- <xsl:variable name="deg2" select="360 * (1 - sum(//data[position() &lt;= $piece]/value[position() = $item]) div $total)" />
- <xsl:variable name="c2">
- <xsl:call-template name="sine">
- <xsl:with-param name="deg" select="$deg2" />
- </xsl:call-template>
- </xsl:variable>
- <xsl:variable name="s2">
- <xsl:call-template name="sine">
- <xsl:with-param name="deg" select="-$deg2 - 90" />
- </xsl:call-template>
- </xsl:variable>
+ <xsl:variable name="deg2" select="360 * (1 - sum(//data[position() &lt;= $piece]/value[position() = $item]) div $total)" />
+ <xsl:variable name="c2">
+ <xsl:call-template name="sin">
+ <xsl:with-param name="degrees" select="$deg2" />
+ </xsl:call-template>
+ </xsl:variable>
+ <xsl:variable name="s2">
+ <xsl:call-template name="sin">
+ <xsl:with-param name="degrees" select="-$deg2 - 90" />
+ </xsl:call-template>
+ </xsl:variable>
- <xsl:variable name="opt"><xsl:choose><xsl:when test="$deg - $deg2 &lt; 180">0</xsl:when><xsl:otherwise>1</xsl:otherwise></xsl:choose></xsl:variable>
+ <xsl:variable name="opt"><xsl:choose><xsl:when test="$deg - $deg2 &lt; 180">0</xsl:when><xsl:otherwise>1</xsl:otherwise></xsl:choose></xsl:variable>
- <!-- Fill the pie piece. -->
- <svg:path fill="{$fill}" title="{$percent}" stroke="white" stroke-width="0.5" d="{concat('M',$x+$rx,' ',$y+$ry,'L',$x+$rx+$rx*$c2,' ',$y+$ry+$ry*$s2,'A',$rx,' ',$ry,' 0 ',$opt,' 1 ',$x+$rx + $rx * $c,' ',$y+$ry +$ry*$s,'z')}" />
+ <!-- Fill the pie piece. -->
+ <svg:path fill="{$fill}" title="{$percent}" stroke="white" stroke-width="0.5" d="{concat('M',$x+$rx,' ',$y+$ry,'L',$x+$rx+$rx*$c2,' ',$y+$ry+$ry*$s2,'A',$rx,' ',$ry,' 0 ',$opt,' 1 ',$x+$rx + $rx * $c,' ',$y+$ry +$ry*$s,'z')}" />
+ </xsl:for-each>
</xsl:for-each>
- </xsl:for-each>
- </svg:g>
- </svg:svg>
+ </svg:g>
+ </svg:svg>
+ </div>
+ <div class="legend">
+ <xsl:for-each select="//name">
+ <xsl:variable name="fill">
+ <xsl:call-template name="fill">
+ <xsl:with-param name="piece" select="position()" />
+ <xsl:with-param name="pieces" select="$pieces" />
+ <xsl:with-param name="colour" select="$base_colour" />
+ </xsl:call-template>
+ </xsl:variable>
+
+ <xsl:variable name="percent" select="concat(.,' (',format-number(../value div $total, '0.00%'),')')" />
+
+ <div class="piece">
+ <div class="bullet" style="background-color:{$fill}" />
+ <xsl:value-of select="$percent" />
+ </div>
+
+ </xsl:for-each>
+ </div>
</div>
-
- <xsl:variable name="bullet_size" select="5" />
- <xsl:variable name="font_size" select="6" />
- <xsl:variable name="line_space" select="2" />
- <xsl:variable name="legend_width" select="$pie_width" />
- <xsl:variable name="legend_height" select="($font_size + $line_space) * $pieces + $bullet_size" />
+ </xsl:template>
- <div class="legend">
- <svg:svg width="30%" height="30%" viewBox="0 0 {$legend_width + $bullet_size * 2} {$legend_height}" version="1.1">
+ <!-- Select legend colour for corresponding pie piece.
+ <xsl:variable name="font_size" select="6" />
+ <xsl:variable name="line_space" select="2" />
+ <xsl:variable name="legend_width" select="$pie_width" />
+ <xsl:variable name="legend_height" select="($font_size + $line_space) * $pieces + $bullet_size" />
+
+ <svg:svg width="{$legend_width}" height="{$legend_height}" viewBox="0 0 {$legend_width + $bullet_size * 2} {$legend_height}" version="1.1">
<xsl:for-each select="//name">
- <!-- Select legend colour for corresponding pie piece. -->
<xsl:variable name="fill">
<xsl:call-template name="fill">
<xsl:variable name="percent" select="concat(.,' (',format-number(../value div $total, '0.00%'),')')" />
- <svg:rect x="0" y="{$dy - $line_space}" width="{$bullet_size}" height="{$bullet_size}" fill="{$fill}" />
- <svg:text x="10" y="{$dy + $line_space}" font-size="{$font_size}"><xsl:value-of select="$percent" /></svg:text>
+ <svg:g>
+ <svg:rect x="0" y="{$dy - $line_space}" width="{$bullet_size}" height="{$bullet_size}" fill="{$fill}" />
+ <svg:text x="10" y="{$dy + $line_space}" font-size="{$font_size}"><xsl:value-of select="$percent" /></svg:text>
+ </svg:g>
</xsl:for-each>
</svg:svg>
- </div>
- </xsl:template>
+ -->
+
<!-- http://en.wikipedia.org/wiki/HSL_and_HSV -->
<xsl:template name="fill">
xml/math.xsl
</xsl:choose>
</xsl:template>
-
- <xsl:template name="sine">
- <xsl:param name="deg" />
- <xsl:param name="radians">
- <xsl:value-of select="$deg div 180 * $PI" />
- </xsl:param>
- <xsl:param name="answer">
- <xsl:value-of select="$radians" />
- </xsl:param>
- <xsl:param name="loc">1</xsl:param>
- <xsl:param name="fract">
- <xsl:value-of select="$radians" />
- </xsl:param>
- <xsl:variable name="fract2" select="-1 * $fract * $radians * $radians div ($loc *2 * ($loc * 2 + 1))" />
+
+ <xsl:template name="power">
+ <xsl:param name="n"/>
+ <xsl:param name="exponent"/>
+ <xsl:param name="result" select="1" />
<xsl:choose>
- <xsl:when test="$fract2 &lt; $PRECISION and $fract2 &gt; -$PRECISION">
- <xsl:value-of select="$answer" />
+ <xsl:when test="$exponent = 0">1</xsl:when>
+ <xsl:when test="$exponent = 1">
+ <xsl:value-of select="$result * $n"/>
</xsl:when>
<xsl:otherwise>
- <xsl:call-template name="sine">
+ <xsl:call-template name="power">
+ <xsl:with-param name="n" select="$n"/>
+ <xsl:with-param name="exponent" select="$exponent - 1"/>
+ <xsl:with-param name="result" select="$result * $n"/>
+ </xsl:call-template>
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+
+ <xsl:template name="factorial">
+ <xsl:param name="n" select="1" />
+
+ <xsl:choose>
+ <xsl:when test="$n &lt;= 1">1</xsl:when>
+ <xsl:otherwise>
+ <xsl:variable name="f">
+ <xsl:call-template name="factorial">
+ <xsl:with-param name="n" select="$n - 1" />
+ </xsl:call-template>
+ </xsl:variable>
+ <xsl:value-of select="$n * $f" />
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+
+ <!-- sin(x) = x - x^3/3! + x^5/5! - x^7/7! ... -->
+ <xsl:template name="sin">
+ <xsl:param name="degrees" />
+ <xsl:param name="radians" select="$degrees * $PI div 180" />
+ <xsl:param name="iter" select="1" />
+ <xsl:param name="maxiter" select="15" />
+ <xsl:param name="result" select="0" />
+
+ <xsl:variable name="taylor" select="($iter - 1) * 2 + 1" />
+ <xsl:variable name="numerator">
+ <xsl:call-template name="power">
+ <xsl:with-param name="n" select="$radians" />
+ <xsl:with-param name="exponent" select="$taylor" />
+ </xsl:call-template>
+ </xsl:variable>
+ <xsl:variable name="denominator">
+ <xsl:call-template name="factorial">
+ <xsl:with-param name="n" select="$taylor" />
+ </xsl:call-template>
+ </xsl:variable>
+
+ <xsl:variable name="term">
+ <xsl:choose>
+ <xsl:when test="$iter mod 2 = 1">
+ <xsl:value-of select="$numerator div $denominator" />
+ </xsl:when>
+ <xsl:otherwise><xsl:value-of select="-1 * $numerator div $denominator" /></xsl:otherwise>
+ </xsl:choose>
+ </xsl:variable>
+
+ <xsl:choose>
+ <xsl:when test="$iter &lt; $maxiter">
+ <xsl:call-template name="sin">
<xsl:with-param name="radians" select="$radians" />
- <xsl:with-param name="answer" select="$answer + $fract2" />
- <xsl:with-param name="loc" select="$loc+1" />
- <xsl:with-param name="fract" select="$fract2" />
+ <xsl:with-param name="iter" select="$iter + 1" />
+ <xsl:with-param name="result" select="$result + $term" />
</xsl:call-template>
- </xsl:otherwise>
+ </xsl:when>
+ <xsl:otherwise><xsl:value-of select="$result" /></xsl:otherwise>
</xsl:choose>
</xsl:template>
xml/resources.css
}
-.paragraph {
- padding-top: 0.5em;
+.paragraph, .legend {
line-height: 120%;
+ margin-top: 0.5em;
}
-.legend {
- margin-top: 0.5em;
+.bullet {
+ margin-right:0.5em;
+ padding-bottom:0.1em;
+
+ width:1ex;
+ height:1ex;
+ display: inline-block;
}
xml/resources.xml
<title>Expenses</title>
<dataset>
- <!--
- <data>
- <name>Public Debt</name>
- <value>150</value>
- </data>
- <data>
- <name>Miscellaneous</name>
- <value>21</value>
- </data>
- <data>
- <name>Program Spending</name>
- <value>43</value>
- </data>
- <data>
- <name>Transfers to Persons</name>
- <value>26</value>
- </data>
- <data>
- <name>Transfers to Government</name>
- <value>21</value>
- </data>
- -->
<data>
<name>Personnel</name>
Delta 174 lines added, 120 lines removed, 54-line increase