Debian Codegraph @

contrib/charts/xsl/pie.xsl

raw
1 <?xml version="1.0"?>
2
3 <!--
4 Licensed to the Apache Software Foundation (ASF) under one or more
5 contributor license agreements. See the NOTICE file distributed with
6 this work for additional information regarding copyright ownership.
7 The ASF licenses this file to You under the Apache License, Version 2.0
8 (the "License"); you may not use this file except in compliance with
9 the License. You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 -->
19
20 <!-- ====================================================================== -->
21 <!-- Generate a simple pie chart. Requires parser extensions for sin and -->
22 <!-- cos. -->
23 <!-- -->
24 <!-- @author [email protected] -->
25 <!-- @version $Id$ -->
26 <!-- ====================================================================== -->
27
28 <xsl:stylesheet version="1.0"
29 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
30 xmlns:math="http://xml.apache.org/xslt/java">
31
32 <xsl:variable name="height" select="graph/meta/pie/height"/>
33 <xsl:variable name="width" select="graph/meta/pie/width"/>
34
35 <xsl:variable name="radius">
36 <xsl:choose>
37 <xsl:when test="$height &lt; $width"><xsl:value-of select="2 * $height div 5"/></xsl:when>
38 <xsl:otherwise><xsl:value-of select="2 * $width div 5"/></xsl:otherwise>
39 </xsl:choose>
40 </xsl:variable>
41
42 <xsl:variable name="360degrees" select="sum(/graph/data//y)"/>
43 <xsl:variable name="1degree" select="360 div $360degrees"/>
44
45 <xsl:template match="graph">
46 <svg
47 width="{$width}"
48 height="{$height}">
49
50 <xsl:comment>radius = <xsl:value-of select="$radius"/></xsl:comment>
51 <xsl:comment>360 degrees = <xsl:value-of select="$360degrees"/></xsl:comment>
52 <xsl:comment>1 degree = <xsl:value-of select="$1degree"/></xsl:comment>
53
54 <g transform="matrix(1 0 0 1 {$width div 2} {$height div 2})">
55 <xsl:apply-templates select="data/set"/>
56 </g>
57 </svg>
58 </xsl:template>
59
60 <xsl:template match="set">
61
62 <xsl:variable name="curpos" select="position()"/>
63 <xsl:variable name="angleStart" select="$1degree * sum(//set[position() &lt; $curpos]/values/*/y)"/>
64 <xsl:variable name="angle" select="sum(descendant::y) * $1degree"/>
65
66 <xsl:comment>angle start = <xsl:value-of select="$angleStart"/></xsl:comment>
67 <xsl:comment>angle = <xsl:value-of select="$angle"/></xsl:comment>
68 <xsl:comment>angle end = <xsl:value-of select="$angle + $angleStart"/></xsl:comment>
69
70 <xsl:variable name="xystart">
71 <xsl:call-template name="xy">
72 <xsl:with-param name="angle" select="$angleStart"/>
73 </xsl:call-template>
74 </xsl:variable>
75
76 <xsl:variable name="xyend">
77 <xsl:call-template name="xy">
78 <xsl:with-param name="angle" select="$angle + $angleStart"/>
79 </xsl:call-template>
80 </xsl:variable>
81
82 <path
83 d="M0, 0 L {$xystart} A {$radius}, {$radius} 0 0 1 {$xyend} z"
84 style="fill:rgb({colour/red/text()},{colour/green/text()},{colour/blue/text()}); stroke-width:1"/>
85
86 </xsl:template>
87
88 <xsl:template name="xy">
89 <xsl:param name="angle"/>
90
91 <xsl:variable name="rad" select="math:java.lang.Math.toRadians($angle)"/>
92
93 <xsl:value-of select="$radius * math:java.lang.Math.cos($rad)"/><xsl:text>, </xsl:text>
94 <xsl:value-of select="$radius * math:java.lang.Math.sin($rad)"/>
95 </xsl:template>
96
97 </xsl:stylesheet>