Debian Codegraph @

contrib/charts/xsl/bar.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 bar graph (columns across) -->
24 <!-- -->
25 <!-- @author [email protected] -->
26 <!-- @version $Id$ -->
27 <!-- ====================================================================== -->
28
29 <xsl:stylesheet version="1.0"
30 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
31
32 <xsl:variable name="height" select="graph/meta/bar/height"/>
33 <xsl:variable name="width" select="graph/meta/bar/width"/>
34
35 <xsl:variable name="margin">10</xsl:variable>
36 <xsl:variable name="labelY">50</xsl:variable>
37 <xsl:variable name="labelX">20</xsl:variable>
38
39 <xsl:variable name="chartHeight" select="$height - (2 * $margin) - $labelX"/>
40 <xsl:variable name="chartWidth" select="$width - (2 * $margin) - $labelY"/>
41
42 <xsl:variable name="barHeight" select="1 div count(/graph/data/set)"/>
43
44 <xsl:variable name="horizontalRangeMax">
45 <xsl:for-each select="/graph/data//y">
46 <xsl:sort order="descending" data-type="number" select="text()"/>
47 <xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
48 </xsl:for-each>
49 </xsl:variable>
50
51 <xsl:variable name="verticalRangeMax">
52 <xsl:for-each select="/graph/data//x">
53 <xsl:sort order="descending" data-type="number" select="text()"/>
54 <xsl:if test="position() = 1"><xsl:value-of select="text() + 1"/></xsl:if>
55 </xsl:for-each>
56 </xsl:variable>
57
58 <xsl:variable name="horizontalScale">
59 <xsl:value-of select="$chartWidth div $horizontalRangeMax"/>
60 </xsl:variable>
61
62 <xsl:variable name="verticalScale">
63 <xsl:value-of select="$chartHeight div $verticalRangeMax"/>
64 </xsl:variable>
65
66 <xsl:template match="graph">
67 <svg
68 width="{$width}"
69 height="{$height}">
70
71 <xsl:comment>Horizontal range max = <xsl:value-of select="$horizontalRangeMax"/></xsl:comment>
72 <xsl:comment>Vertical range max = <xsl:value-of select="$verticalRangeMax"/></xsl:comment>
73 <xsl:comment>Horizontal scale = <xsl:value-of select="$horizontalScale"/></xsl:comment>
74 <xsl:comment>Vertical scale = <xsl:value-of select="$verticalScale"/></xsl:comment>
75 <xsl:comment>Bar height = <xsl:value-of select="$barHeight"/></xsl:comment>
76
77 <g transform="matrix(1 0 0 -1 {$margin + $labelY} {$height - ($margin + $labelX)})">
78 <xsl:call-template name="verticalGridlines">
79 <xsl:with-param name="value">0</xsl:with-param>
80 <xsl:with-param name="step">20000</xsl:with-param>
81 </xsl:call-template>
82
83 <xsl:call-template name="horizontalGridlines">
84 <xsl:with-param name="value">0</xsl:with-param>
85 <xsl:with-param name="step">1</xsl:with-param>
86 </xsl:call-template>
87
88 <xsl:apply-templates select="data/*"/>
89 </g>
90 </svg>
91 </xsl:template>
92
93 <xsl:template name="verticalGridlines">
94 <xsl:param name="value">0</xsl:param>
95 <xsl:param name="step"/>
96
97 <line
98 id="verticalGridline_{$value}"
99 x1="{$value * $horizontalScale}"
100 y1="-2"
101 x2="{$value * $horizontalScale}"
102 y2="{$verticalRangeMax * $verticalScale}"
103 style="fill:none;stroke:black;stroke-width:1"/>
104
105 <text
106 transform="matrix(1 0 0 -1 0 -{$labelX})"
107 x="{$value * $horizontalScale}"
108 y="0"
109 style="text-anchor:middle;font-size:12;fill:black">
110 <xsl:value-of select="$value"/>
111 </text>
112
113 <xsl:if test="$value + $step &lt; $horizontalRangeMax">
114 <xsl:call-template name="verticalGridlines">
115 <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
116 <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
117 </xsl:call-template>
118 </xsl:if>
119 </xsl:template>
120
121 <xsl:template name="horizontalGridlines">
122 <xsl:param name="value">0</xsl:param>
123 <xsl:param name="step"/>
124
125 <line
126 id="horizontalGridline_{$value}"
127 x1="-2"
128 y1="{$value * $verticalScale}"
129 x2="{$horizontalRangeMax * $horizontalScale}"
130 y2="{$value * $verticalScale}"
131 style="fill:none;stroke:black;stroke-width:1"/>
132
133 <text
134 transform="matrix(1 0 0 -1 -{$margin} 0)"
135 x="0"
136 y="-{(($value + 0.4) * $verticalScale)}"
137 style="text-anchor:end;font-size:12;fill:black">
138 <xsl:value-of select="//meta/axis/x/value[position() = $value + 1]"/>
139 </text>
140
141 <xsl:if test="$value + $step &lt; $verticalRangeMax">
142 <xsl:call-template name="horizontalGridlines">
143 <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
144 <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
145 </xsl:call-template>
146 </xsl:if>
147 </xsl:template>
148
149 <xsl:template match="set">
150 <xsl:variable name="red" select="colour/red/text()"/>
151 <xsl:variable name="green" select="colour/green/text()"/>
152 <xsl:variable name="blue" select="colour/blue/text()"/>
153
154 <xsl:variable name="label" select="label"/>
155 <xsl:variable name="col" select="position() - 1"/>
156
157 <xsl:for-each select="values/*">
158 <rect
159 id="{$label}-{position()}"
160 x="0"
161 y="{(x + ($barHeight * $col)) * $verticalScale}"
162 width="{y * $horizontalScale}"
163 height="{$barHeight * $verticalScale}"
164 rx="0"
165 ry="0"
166 style="stroke-width:1;stroke:rgb(0,0,0);fill:rgb({$red},{$green},{$blue})"/>
167 </xsl:for-each>
168
169 </xsl:template>
170
171 </xsl:stylesheet>