Dave Jarvis' Repositories

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

Added legend to page. Two SVG images.

Author Dave Jarvis <email>
Date 2014-07-30 20:15:47 GMT-0700
Commit 6e61d183dee4530fa966b2009193ebc2162a7ab4
Parent df2594f
xml/chart.xsl
<!-- Globals -->
<xsl:variable name="PI">3.14159265358979323846</xsl:variable>
- <xsl:variable name="fontAdjustment">1.2</xsl:variable>
- <xsl:variable name="fontWidthEstimate">1.8</xsl:variable>
- <xsl:variable name="epsilon">0.0001</xsl:variable>
- <xsl:variable name="tickWidth">2.5</xsl:variable>
+ <xsl:variable name="precision">0.0001</xsl:variable>
<!-- Convert XML data into a Pie Chart. -->
<xsl:template match="dataset" mode="piechart">
- <xsl:variable name="width">100</xsl:variable>
- <xsl:variable name="height" select="$width - $PI" />
-
- <svg:svg width="30%" height="30%" viewBox="0 0 {$width} {$height}" version="1.1">
- <svg:g stroke-width="0" font-family="sans-serif" fill="#000" stroke="#000">
-
- <!-- Number of pie pieces -->
- <xsl:variable name="pieces" select="count(data)" />
- <!-- Calculate the total value for all pieces -->
- <xsl:variable name="total" select="sum(//value)" />
-
- <xsl:for-each select="data">
- <xsl:variable name="piece" select="position()" />
+ <xsl:variable name="pie_width">100</xsl:variable>
+ <xsl:variable name="pie_height" select="$pie_width - $PI" />
- <!-- Calculate Fill Color -->
- <xsl:variable name="fill">
- <xsl:call-template name="fill">
- <xsl:with-param name="piece" select="$piece" />
- <xsl:with-param name="pieces" select="$pieces" />
- </xsl:call-template>
- </xsl:variable>
+ <!-- Number of pie pieces -->
+ <xsl:variable name="pieces" select="count(data)" />
- <xsl:for-each select="value">
- <xsl:variable name="row" select="position()" />
+ <!-- Calculate the total value for all pieces -->
+ <xsl:variable name="total" select="sum(//value)" />
- <xsl:variable name="title"><xsl:value-of select="../name" /></xsl:variable>
- <xsl:variable name="percent" select="concat($title,' (',format-number(. div $total, '0.00%'),')')" />
+ <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()" />
- <xsl:variable name="x" select="0" />
- <xsl:variable name="y" select="0" />
- <xsl:variable name="rx" select="$width * 0.5" />
- <xsl:variable name="ry" select="$height * 0.5" />
-
- <xsl:variable name="deg">
- <xsl:choose>
- <xsl:when test="$piece = 1">360</xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="360 * (1-sum(//data[position() &lt;=$piece -1]/value[position() = $row]) 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" />
+ <!-- Calculate Fill Color -->
+ <xsl:variable name="fill">
+ <xsl:call-template name="fill">
+ <xsl:with-param name="piece" select="$piece" />
+ <xsl:with-param name="pieces" select="$pieces" />
</xsl:call-template>
</xsl:variable>
- <xsl:variable name="deg2" select="360 * (1-sum(//data[position() &lt;=$piece]/value[position() = $row]) 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:for-each select="value">
+ <xsl:variable name="row" select="position()" />
- <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 piece. -->
- <svg:path fill="{$fill}" title="{$percent}" stroke="none" 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: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" />
+
+ <xsl:variable name="deg">
+ <xsl:choose>
+ <xsl:when test="$piece = 1">360</xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="360 * (1-sum(//data[position() &lt;=$piece -1]/value[position() = $row]) 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="deg2" select="360 * (1-sum(//data[position() &lt;=$piece]/value[position() = $row]) 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="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="none" 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>
+ </svg:g>
+ </svg:svg>
+ </div>
+
+ <xsl:variable name="font_size">6</xsl:variable>
+ <xsl:variable name="line_space">2</xsl:variable>
+ <xsl:variable name="legend_width" select="$pie_width" />
+ <xsl:variable name="legend_height" select="($font_size + $line_space) * $pieces" />
+
+ <div class="legend">
+ <svg:svg width="30%" height="30%" viewBox="0 0 {$legend_width} {$legend_height}" version="1.1">
+ <xsl:for-each select="//name">
+ <!-- Calculate Fill Color -->
+ <xsl:variable name="fill">
+ <xsl:call-template name="fill">
+ <xsl:with-param name="piece" select="position()" />
+ <xsl:with-param name="pieces" select="$pieces" />
+ </xsl:call-template>
+ </xsl:variable>
+
+ <xsl:variable name="dy" select="position() * ($font_size + $line_space)" />
+
+ <svg:rect x="0" y="{$dy - 2 * $line_space}" width="5" height="5" fill="{$fill}" />
+ <svg:text x="10" y="{$dy}" font-size="{$font_size}"><xsl:value-of select="." /></svg:text>
</xsl:for-each>
- </svg:g>
- </svg:svg>
- </xsl:template>
+ </svg:svg>
+ </div>
- <xsl:template name="root">
- <xsl:param name="value" />
- <xsl:param name="result">1</xsl:param>
- <xsl:variable name="res" select="($result + $value div $result) div 2" />
- <xsl:choose>
- <xsl:when test="$res &lt; $result + $epsilon and $res &gt; $result - $epsilon">
- <xsl:value-of select="$result" />
- </xsl:when>
- <xsl:otherwise>
- <xsl:call-template name="root">
- <xsl:with-param name="value" select="$value" />
- <xsl:with-param name="result" select="$res" />
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
</xsl:template>
<xsl:variable name="fract2" select="-1 * $fract * $radians * $radians div ($loc *2 * ($loc * 2 + 1))" />
<xsl:choose>
- <!-- Exit when at end of precision -->
- <xsl:when test="$fract2 &lt; $epsilon and $fract2 &gt; -$epsilon">
+ <xsl:when test="$fract2 &lt; $precision and $fract2 &gt; -$precision">
<xsl:value-of select="$answer" />
</xsl:when>
xml/common.xsl
</xsl:template>
-<!--
-<xsl:template match="dataset" mode="chart">
- <svg:svg width="200px" height="200px" viewBox="0 0 200 200">
- <svg:rect x="10" y="10" width="200" height="200" fill="red" stroke="black"/>
- </svg:svg>
- <svg:svg version="1.0" width="500px" height="500px" viewBox="0 0 500 500">
- </svg:svg>
-
-</xsl:template>
--->
-
</xsl:stylesheet>
-
Delta 95 lines added, 101 lines removed, 6-line decrease