<?xml version="1.0" encoding="utf-8"?>
<xslt:stylesheet version="1.0"
				xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
				xmlns="http://www.w3.org/2000/svg"
				xmlns:exslt="http://exslt.org/common"
				xmlns:math="http://exslt.org/math"
				xmlns:lwm="http://www.linkwerk.com/namespaces/math/"
				xmlns:lw="http://www.linkwerk.com/namespaces/2005-06-09/svg/charts/"
				xmlns:func="http://exslt.org/functions"
				xmlns:xlink="http://www.w3.org/1999/xlink"
				exclude-result-prefixes="xslt exslt math lwm lw func xlink">

<!--
     © 2005 Stefan Mintert 
	 This is copyrighted material. Please read
	 http://www.linkwerk.com/pub/svg/charts/licence.txt
	 
	 The purpose of this XSLT style sheet is described online
	 at http://www.linkwerk.com/pub/svg/charts/

	$Date: 2005/06/16 12:52:53 $ $Revision: 1.6 $


	Acknowledgments
	===============
	Thanks to
	- Thomas Meinike for bugfixing

-->

	<xslt:output 
		method="xml"
		encoding="utf-8"
		indent="yes"
		doctype-public="-//W3C//DTD SVG 1.1//EN"
		doctype-system="http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"/>

	<xslt:variable name="faktor" select="'100'"/>
	<xslt:variable name="viewBoxMax">
		<xslt:value-of select="$faktor * 3"/>
	</xslt:variable>

	<xslt:variable name="pi" select="3.1415926535897932384626433832795"/>

	<func:function name="lwm:cosDegree">
		<xslt:param name="degree" select="0"/>	
		<func:result select="number(math:cos(number($pi) * (number($degree) div 180)))"/>
	</func:function>
	<func:function name="lwm:sinDegree">
		<xslt:param name="degree" select="0"/>	
		<func:result select="number(math:sin(number($pi) * (number($degree) div 180)))"/>
	</func:function>

	<xslt:template match="lw:chart/lw:pie">
		<svg width="{@width}" height="{@height}" viewBox="0 0 {$viewBoxMax} {$viewBoxMax}"  version="1.1">
		  <!-- Das folgende <g>rouping-Elemente definiert ein Koordinatensystem
		  	   mit dem Nullpunkt (0,0) in der Mitte des Viewports. Die x- und y-
		  	   Koordindaten liegen jeweils im Bereich [-0.5*viewBoxMax, .. , 0.5*viewBoxMax].
		  	   Die Orientierung der Achsen entspricht der mathematisch üblichen
		  	   Orientierung -->
		  <g transform="matrix(1 0 0 -1 {$viewBoxMax * 0.5 } {$viewBoxMax * 0.5 })">	
			<xslt:apply-templates/>
		  </g>
		</svg>
	</xslt:template>

	<xslt:template match="lw:pie/lw:segment">
		<xslt:variable name="anteil" select="number(text())"/>
		<xslt:variable name="vorgaengeranteile" select="sum(preceding-sibling::lw:segment/text())"/>
		
		<xslt:variable name="startwinkel" select="3.60 * number($vorgaengeranteile)"/>
		<xslt:variable name="endwinkel" select="(3.60 * $anteil) + $startwinkel"/>
	
		<xslt:choose>
  			<xslt:when test="($endwinkel - $startwinkel) &lt;= 180">
				<path d="M 0 0 L {$faktor * lwm:cosDegree($endwinkel)} {$faktor * lwm:sinDegree($endwinkel)} A {$faktor} {$faktor} 0 0,0 {$faktor * lwm:cosDegree($startwinkel)} {$faktor * lwm:sinDegree($startwinkel)} " 
			  		style="stroke: white; fill: {@color}; stroke-width: 0;"
		  			/>
			</xslt:when>
			<xslt:otherwise>
				<path d="M 0 0 L {$faktor * lwm:cosDegree($endwinkel)} {$faktor * lwm:sinDegree($endwinkel)} A {$faktor} {$faktor} 0 1,0 {$faktor * lwm:cosDegree($startwinkel)} {$faktor * lwm:sinDegree($startwinkel)} " 
			  		style="stroke: white; fill: {@color}; stroke-width: 0;"
		  			/>
			</xslt:otherwise> 
		</xslt:choose>

		<!-- Segmenttrenner: -->
	  	<path d="M 0 0 L {$faktor * lwm:cosDegree($startwinkel)} {$faktor * lwm:sinDegree($startwinkel)}" 
	  		style="stroke: white; fill: white; stroke-width: 3;"
	  		/>
	  	<path d="M 0 0 L {$faktor * lwm:cosDegree($endwinkel)} {$faktor * lwm:sinDegree($endwinkel)}" 
	  		style="stroke: white; fill: white; stroke-width: 3;"
	  		/>
	</xslt:template>
</xslt:stylesheet>
