Debian Codegraph @

contrib/charts/xsl/line.xsl

raw
1 <?xml version="1.0"?>
2
3 <!--
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 -->
21
22 <!-- ====================================================================== -->
23 <!-- Generate a simple line graph. -->
24 <!-- Need to correct incorrect assumption about x axis steps -->
25 <!-- -->
26 <!-- @author [email protected] -->
27 <!-- @version $Id$ -->
28 <!-- ====================================================================== -->
29
30 <xsl:stylesheet version="1.0"
31 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
32
33 <xsl:variable name="height" select="graph/meta/line/height"/>
34 <xsl:variable name="width" select="graph/meta/line/width"/>
35
36 <xsl:variable name="margin">10</xsl:variable>
37 <xsl:variable name="labelY">50</xsl:variable>
38 <xsl:variable name="labelX">20</xsl:variable>
39
40 <xsl:variable name="chartHeight" select="$height - (2 * $margin) - $labelX"/>
41 <xsl:variable name="chartWidth" select="$width - (4 * $margin) - $labelY"/>
42
43 <xsl:variable name="verticalRangeMax">
44 <xsl:for-each select="//y">
45 <xsl:sort order="descending" data-type="number" select="text()"/>
46 <xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
47 </xsl:for-each>
48 </xsl:variable>
49
50 <xsl:variable name="horizontalRangeMax">
51 <xsl:for-each select="//x">
52 <xsl:sort order="descending" data-type="number" select="text()"/>
53 <xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
54 </xsl:for-each>
55 </xsl:variable>
56
57 <xsl:variable name="verticalScale">
58 <xsl:value-of select="$chartHeight div $verticalRangeMax"/>
59 </xsl:variable>
60
61 <xsl:variable name="horizontalScale">
62 <xsl:value-of select="$chartWidth div $horizontalRangeMax"/>
63 </xsl:variable>
64
65 <xsl:template match="graph">
66 <svg
67 width="{$width}"
68 height="{$height}">
69
70 <g transform="matrix(1 0 0 -1 {$margin + $labelY} {$height - ($margin + $labelX)})">
71 <xsl:call-template name="verticalGridlines">
72 <xsl:with-param name="value">0</xsl:with-param>
73 <xsl:with-param name="step">1</xsl:with-param>
74 </xsl:call-template>
75
76 <xsl:call-template name="horizontalGridlines">
77 <xsl:with-param name="value">0</xsl:with-param>
78 <xsl:with-param name="step">20000</xsl:with-param>
79 </xsl:call-template>
80
81 <xsl:apply-templates select="data/*"/>
82 </g>
83 </svg>
84 </xsl:template>
85
86 <xsl:template name="verticalGridlines">
87 <xsl:param name="value">0</xsl:param>
88 <xsl:param name="step"/>
89
90 <line
91 id="verticalGridline_{$value}"
92 x1="{$value * $horizontalScale}"
93 y1="-2"
94 x2="{$value * $horizontalScale}"
95 y2="{$verticalRangeMax * $verticalScale}"
96 style="fill:none;stroke:black;stroke-width:1"/>
97
98 <text
99 transform="matrix(1 0 0 -1 0 -{$labelX})"
100 x="{$value * $horizontalScale}"
101 y="0"
102 style="text-anchor:middle;font-size:12;fill:black">
103 <xsl:value-of select="//meta/axis/x/value[position() = $value + 1]"/>
104 </text>
105
106 <xsl:if test="$value + $step &lt; $horizontalRangeMax">
107 <xsl:call-template name="verticalGridlines">
108 <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
109 <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
110 </xsl:call-template>
111 </xsl:if>
112 </xsl:template>
113
114 <xsl:template name="horizontalGridlines">
115 <xsl:param name="value">0</xsl:param>
116 <xsl:param name="step"/>
117
118 <line
119 id="horizontalGridline_{$value}"
120 x1="-2"
121 y1="{$value * $verticalScale}"
122 x2="{$horizontalRangeMax * $horizontalScale}"
123 y2="{$value * $verticalScale}"
124 style="fill:none;stroke:black;stroke-width:1"/>
125
126 <text
127 transform="matrix(1 0 0 -1 -{$margin} -5)"
128 x="0"
129 y="-{$value * $verticalScale}"
130 style="text-anchor:end;font-size:12;fill:black">
131 <xsl:value-of select="$value"/>
132 </text>
133
134 <xsl:if test="$value + $step &lt; $verticalRangeMax">
135 <xsl:call-template name="horizontalGridlines">
136 <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
137 <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
138 </xsl:call-template>
139 </xsl:if>
140 </xsl:template>
141
142 <xsl:template match="set">
143 <xsl:variable name="red" select="colour/red/text()"/>
144 <xsl:variable name="green" select="colour/green/text()"/>
145 <xsl:variable name="blue" select="colour/blue/text()"/>
146
147 <xsl:variable name="line">
148 <xsl:for-each select="values/*">
149 <xsl:choose>
150 <xsl:when test="position() = 1"><xsl:text>M </xsl:text></xsl:when>
151 <xsl:otherwise><xsl:text>L </xsl:text></xsl:otherwise>
152 </xsl:choose>
153 <xsl:value-of select="x * $horizontalScale"/><xsl:text> </xsl:text><xsl:value-of select="y * $verticalScale"/>
154 </xsl:for-each>
155 </xsl:variable>
156
157 <path
158 id="{label}"
159 d="{$line}"
160 style="fill:none;stroke:rgb({$red},{$green},{$blue});stroke-width:2"/>
161
162 </xsl:template>
163
164 </xsl:stylesheet>