You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by vh...@apache.org on 2001/05/08 00:27:59 UTC

cvs commit: xml-batik/contrib/charts/xsl bar.xsl column.xsl line.xsl pie.xsl

vhardy      01/05/07 15:27:59

  Added:       contrib/charts README
               contrib/charts/xml data.xml
               contrib/charts/xsl bar.xsl column.xsl line.xsl pie.xsl
  Log:
  Initial commit for john.r.morrison@ntlworld.com's contribution of
  XSL stylesheets for charts.
  
  Revision  Changes    Path
  1.1                  xml-batik/contrib/charts/README
  
  Index: README
  ===================================================================
  #
  # Author  : Vincent Hardy
  # Version : $Id: README,v 1.1 2001/05/07 22:27:56 vhardy Exp $
  #
  This directory contains XSL stylesheets to create charts from XML data. 
  This has been contributed by John Morrison (john.r.morrison@ntlworld.com).
  
  The <xsl> directory contains stylesheets for different kinds of charts,
  such as bar charts and pie charts. The <xml> directory contains an example
  of XML document that can be processed by the XSL stylesheets. The DTD has
  not been contributed yet but will be added shortly.
  
  
  
  
  1.1                  xml-batik/contrib/charts/xml/data.xml
  
  Index: data.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <!--
   *****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   *                                                                           *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************
  -->
  
  <graph>
    <data>
      <set>
        <colour>
          <red>255</red>
          <green>0</green>
          <blue>0</blue>
        </colour>
        <label>England &amp; Wales</label>
        <values>
          <value><x>0</x><y>74778.12</y></value>
          <value><x>1</x><y>76895.72</y></value>
          <value><x>2</x><y>79491.88</y></value>
          <value><x>3</x><y>82136.99</y></value>
          <value><x>4</x><y>83806.98</y></value>
          <value><x>5</x><y>85052.68</y></value>
          <value><x>6</x><y>87249.84</y></value>
          <value><x>7</x><y>91087.80</y></value>
          <value><x>8</x><y>94992.48</y></value>
          <value><x>9</x><y>97919.13</y></value>
          <value><x>10</x><y>101212.15</y></value>
          <value><x>11</x><y>103540.02</y></value>
        </values>
      </set>
      <set>
        <colour>
          <red>0</red>
          <green>255</green>
          <blue>0</blue>
        </colour>
        <label>Leicestershire</label>
        <values>
          <value><x>0</x><y>55073.23</y></value>
          <value><x>1</x><y>56523.02</y></value>
          <value><x>2</x><y>58513.11</y></value>
          <value><x>3</x><y>60394.70</y></value>
          <value><x>4</x><y>61359.47</y></value>
          <value><x>5</x><y>62228.44</y></value>
          <value><x>6</x><y>63341.27</y></value>
          <value><x>7</x><y>65208.29</y></value>
          <value><x>8</x><y>67718.87</y></value>
          <value><x>9</x><y>68927.73</y></value>
          <value><x>10</x><y>70599.73</y></value>
          <value><x>11</x><y>72210.70</y></value>
        </values>
      </set>
      <set>
        <colour>
          <red>0</red>
          <green>0</green>
          <blue>255</blue>
        </colour>
        <label>LE14 4</label>
        <values>
          <value><x>0</x><y>96667.00</y></value>
          <value><x>1</x><y>99930.00</y></value>
          <value><x>2</x><y>100449.00</y></value>
          <value><x>3</x><y>114722.00</y></value>
          <value><x>4</x><y>107342.00</y></value>
          <value><x>5</x><y>105519.00</y></value>
          <value><x>6</x><y>105411.00</y></value>
          <value><x>7</x><y>97556.00</y></value>
          <value><x>8</x><y>114184.00</y></value>
          <value><x>9</x><y>121402.00</y></value>
          <value><x>10</x><y>127240.00</y></value>
          <value><x>11</x><y>133445.00</y></value>
        </values>
      </set>
    </data>
    <meta>
      <line>
        <height>200</height>
        <width>500</width>
      </line>
      <column>
        <height>200</height>
        <width>500</width>
      </column>
      <bar>
        <height>500</height>
        <width>500</width>
      </bar>
      <pie>
        <height>200</height>
        <width>200</width>
      </pie>
      <axis>
        <x>
          <value>Q4/97</value>
          <value>Q1/98</value>
          <value>Q2/98</value>
          <value>Q3/98</value>
          <value>Q4/98</value>
          <value>Q1/99</value>
          <value>Q2/99</value>
          <value>Q3/99</value>
          <value>Q4/99</value>
          <value>Q1/00</value>
          <value>Q2/00</value>
          <value>Q3/00</value>
        </x>
        <y>
          <value>Q4/97</value>
          <value>Q1/98</value>
          <value>Q2/98</value>
          <value>Q3/98</value>
          <value>Q4/98</value>
          <value>Q1/99</value>
          <value>Q2/99</value>
          <value>Q3/99</value>
          <value>Q4/99</value>
          <value>Q1/00</value>
          <value>Q2/00</value>
          <value>Q3/00</value>
        </y>
      </axis>
    </meta>
  </graph>
  
  
  1.1                  xml-batik/contrib/charts/xsl/bar.xsl
  
  Index: bar.xsl
  ===================================================================
  <?xml version="1.0"?>
  
  <!--
   *****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   *                                                                           *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************
  -->
  <!-- ====================================================================== -->
  <!-- Generate a bar graph (columns across)                                  -->
  <!--                                                                        -->
  <!-- @author john.r.morrison@ntworld.com                                    -->
  <!-- @version $Id: bar.xsl,v 1.1 2001/05/07 22:27:58 vhardy Exp $                                                          -->
  <!-- ====================================================================== -->
  
  <xsl:stylesheet version="1.0"
  	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:variable name="height" select="graph/meta/bar/height"/>
  <xsl:variable name="width"  select="graph/meta/bar/width"/>
  
  <xsl:variable name="margin">10</xsl:variable>
  <xsl:variable name="labelY">50</xsl:variable>
  <xsl:variable name="labelX">20</xsl:variable>
  
  <xsl:variable name="chartHeight" select="$height - (2 * $margin) - $labelX"/>
  <xsl:variable name="chartWidth"  select="$width  - (2 * $margin) - $labelY"/>
  
  <xsl:variable name="barHeight" select="1 div count(/graph/data/set)"/>
  
  <xsl:variable name="horizontalRangeMax">
    <xsl:for-each select="/graph/data//y">
      <xsl:sort order="descending" data-type="number" select="text()"/>
      <xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:variable name="verticalRangeMax">
    <xsl:for-each select="/graph/data//x">
      <xsl:sort order="descending" data-type="number" select="text()"/>
      <xsl:if test="position() = 1"><xsl:value-of select="text() + 1"/></xsl:if>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:variable name="horizontalScale">
    <xsl:value-of select="$chartWidth  div $horizontalRangeMax"/>
  </xsl:variable>
  
  <xsl:variable name="verticalScale">
    <xsl:value-of select="$chartHeight div $verticalRangeMax"/>
  </xsl:variable>
  
  <xsl:template match="graph">
    <svg
      width="{$width}"
      height="{$height}">
  
      <xsl:comment>Horizontal range max = <xsl:value-of select="$horizontalRangeMax"/></xsl:comment>
      <xsl:comment>Vertical range max   = <xsl:value-of select="$verticalRangeMax"/></xsl:comment>
      <xsl:comment>Horizontal scale     = <xsl:value-of select="$horizontalScale"/></xsl:comment>
      <xsl:comment>Vertical scale       = <xsl:value-of select="$verticalScale"/></xsl:comment>
      <xsl:comment>Bar height           = <xsl:value-of select="$barHeight"/></xsl:comment>
  
  	  <g transform="matrix(1 0 0 -1 {$margin + $labelY} {$height - ($margin + $labelX)})">
        <xsl:call-template name="verticalGridlines">
          <xsl:with-param name="value">0</xsl:with-param>
          <xsl:with-param name="step">20000</xsl:with-param>
        </xsl:call-template>
  
        <xsl:call-template name="horizontalGridlines">
          <xsl:with-param name="value">0</xsl:with-param>
          <xsl:with-param name="step">1</xsl:with-param>
        </xsl:call-template>
  
        <xsl:apply-templates select="data/*"/>
  	  </g>
    </svg>
  </xsl:template>
  
  <xsl:template name="verticalGridlines">
    <xsl:param name="value">0</xsl:param>
    <xsl:param name="step"/>
  
  	<line
  	  id="verticalGridline_{$value}"
  	  x1="{$value * $horizontalScale}"
  	  y1="-2"
  	  x2="{$value * $horizontalScale}"
  	  y2="{$verticalRangeMax * $verticalScale}"
  	  style="fill:none;stroke:black;stroke-width:1"/>
  
  	<text
  	  transform="matrix(1 0 0 -1 0 -{$labelX})"
  	  x="{$value * $horizontalScale}"
  	  y="0"
  	  style="text-anchor:middle;font-size:12;fill:black">
      <xsl:value-of select="$value"/>
  	</text>
  
    <xsl:if test="$value + $step &lt; $horizontalRangeMax">
      <xsl:call-template name="verticalGridlines">
        <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
        <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="horizontalGridlines">
    <xsl:param name="value">0</xsl:param>
    <xsl:param name="step"/>
  
  	<line
  	  id="horizontalGridline_{$value}"
  	  x1="-2"
  	  y1="{$value * $verticalScale}"
  	  x2="{$horizontalRangeMax * $horizontalScale}"
  	  y2="{$value * $verticalScale}"
  	  style="fill:none;stroke:black;stroke-width:1"/>
  
  	<text
  	  transform="matrix(1 0 0 -1 -{$margin} 0)"
  	  x="0"
  	  y="-{(($value + 0.4) * $verticalScale)}"
  	  style="text-anchor:end;font-size:12;fill:black">
      <xsl:value-of select="//meta/axis/x/value[position() = $value + 1]"/>
  	</text>
  
    <xsl:if test="$value + $step &lt; $verticalRangeMax">
      <xsl:call-template name="horizontalGridlines">
        <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
        <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="set">
    <xsl:variable name="red"   select="colour/red/text()"/>
    <xsl:variable name="green" select="colour/green/text()"/>
    <xsl:variable name="blue"  select="colour/blue/text()"/>
  
    <xsl:variable name="label" select="label"/>
    <xsl:variable name="col"   select="position() - 1"/>
  
    <xsl:for-each select="values/*">
      <rect
        id="{$label}-{position()}"
        x="0"
        y="{(x + ($barHeight * $col)) * $verticalScale}"
        width="{y * $horizontalScale}"
        height="{$barHeight * $verticalScale}"
        rx="0"
        ry="0"
        style="stroke-width:1;stroke:rgb(0,0,0);fill:rgb({$red},{$green},{$blue})"/>
    </xsl:for-each>
  
  </xsl:template>
  
  </xsl:stylesheet>
  
  
  1.1                  xml-batik/contrib/charts/xsl/column.xsl
  
  Index: column.xsl
  ===================================================================
  <?xml version="1.0"?>
  
  <!--
   *****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   *                                                                           *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************
  -->
  <!-- ====================================================================== -->
  <!-- Generate a column graph (bars up the ways)                             -->
  <!--                                                                        -->
  <!-- @author john.r.morrison@ntworld.com                                    -->
  <!-- @version $Id: column.xsl,v 1.1 2001/05/07 22:27:58 vhardy Exp $                                                          -->
  <!-- ====================================================================== -->
  
  <xsl:stylesheet version="1.0"
  	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:variable name="height" select="graph/meta/column/height"/>
  <xsl:variable name="width"  select="graph/meta/column/width"/>
  
  <xsl:variable name="margin">10</xsl:variable>
  <xsl:variable name="labelWidthY">50</xsl:variable>
  <xsl:variable name="labelWidthX">20</xsl:variable>
  
  <xsl:variable name="chartHeight" select="$height - (2 * $margin) - $labelWidthX"/>
  <xsl:variable name="chartWidth"  select="$width  - (2 * $margin) - $labelWidthY"/>
  
  <xsl:variable name="barWidth" select="1 div count(//set)"/>
  
  <xsl:variable name="verticalRangeMax">
    <xsl:for-each select="//y">
      <xsl:sort order="descending" data-type="number" select="text()"/>
      <xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:variable name="horizontalRangeMax">
    <xsl:for-each select="//x">
      <xsl:sort order="descending" data-type="number" select="text()"/>
      <xsl:if test="position() = 1"><xsl:value-of select="text() + 1"/></xsl:if>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:variable name="verticalScale">
    <xsl:value-of select="$chartHeight div $verticalRangeMax"/>
  </xsl:variable>
  
  <xsl:variable name="horizontalScale">
    <xsl:value-of select="$chartWidth  div $horizontalRangeMax"/>
  </xsl:variable>
  
  <xsl:template match="graph">
    <svg
      width="{$width}"
      height="{$height}">
  
  	  <g transform="matrix(1 0 0 -1 {$margin + $labelWidthY} {$height - ($margin + $labelWidthX)})">
        <xsl:call-template name="verticalGridlines">
          <xsl:with-param name="value">0</xsl:with-param>
          <xsl:with-param name="step">1</xsl:with-param>
        </xsl:call-template>
  
        <xsl:call-template name="horizontalGridlines">
          <xsl:with-param name="value">0</xsl:with-param>
          <xsl:with-param name="step">20000</xsl:with-param>
        </xsl:call-template>
  
        <xsl:apply-templates select="data/*"/>
  	  </g>
    </svg>
  </xsl:template>
  
  <xsl:template name="verticalGridlines">
    <xsl:param name="value">0</xsl:param>
    <xsl:param name="step"/>
  
  	<line
  	  id="verticalGridline_{$value}"
  	  x1="{$value * $horizontalScale}"
  	  y1="-2"
  	  x2="{$value * $horizontalScale}"
  	  y2="{$verticalRangeMax * $verticalScale}"
  	  style="fill:none;stroke:black;stroke-width:1"/>
  
  	<text
  	  transform="matrix(1 0 0 -1 0 -{$labelWidthX})"
  	  x="{$value * $horizontalScale}"
  	  y="0"
  	  style="font-size:12;fill:black">
      <xsl:value-of select="//meta/axis/x/value[position() = $value + 1]"/>
  	</text>
  
    <xsl:if test="$value + $step &lt;= $horizontalRangeMax">
      <xsl:call-template name="verticalGridlines">
        <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
        <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="horizontalGridlines">
    <xsl:param name="value">0</xsl:param>
    <xsl:param name="step"/>
  
  	<line
  	  id="horizontalGridline_{$value}"
  	  x1="-2"
  	  y1="{$value * $verticalScale}"
  	  x2="{$horizontalRangeMax * $horizontalScale}"
  	  y2="{$value * $verticalScale}"
  	  style="fill:none;stroke:black;stroke-width:1"/>
  
  	<text
  	  transform="matrix(1 0 0 -1 -{$margin} -5)"
  	  x="0"
  	  y="-{$value * $verticalScale}"
  	  style="text-anchor:end;font-size:12;fill:black">
      <xsl:value-of select="$value"/>
  	</text>
  
    <xsl:if test="$value + $step &lt;= $verticalRangeMax">
      <xsl:call-template name="horizontalGridlines">
        <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
        <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="set">
    <xsl:variable name="red"   select="colour/red/text()"/>
    <xsl:variable name="green" select="colour/green/text()"/>
    <xsl:variable name="blue"  select="colour/blue/text()"/>
  
    <xsl:variable name="label" select="label"/>
    <xsl:variable name="col"   select="position() - 1"/>
  
    <xsl:for-each select="values/*">
      <rect
        id="{$label}-{generate-id(.)}"
        x="{(x + ($barWidth * $col)) * $horizontalScale}"
        y="0"
        width="{$barWidth * $horizontalScale}"
        height="{y * $verticalScale}"
        rx="0"
        ry="0"
        style="stroke-width:1;stroke:rgb(0,0,0);fill:rgb({$red},{$green},{$blue})"/>
    </xsl:for-each>
  
  </xsl:template>
  
  </xsl:stylesheet>
  
  
  1.1                  xml-batik/contrib/charts/xsl/line.xsl
  
  Index: line.xsl
  ===================================================================
  <?xml version="1.0"?>
  
  <!--
   *****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   *                                                                           *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************
  -->
  <!-- ====================================================================== -->
  <!-- Generate a simple line graph.                                          -->
  <!-- Need to correct incorrect assumption about x axis steps                -->
  <!--                                                                        -->
  <!-- @author john.r.morrison@ntworld.com                                    -->
  <!-- @version $Id: line.xsl,v 1.1 2001/05/07 22:27:59 vhardy Exp $                                                          -->
  <!-- ====================================================================== -->
  
  <xsl:stylesheet version="1.0"
  	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:variable name="height" select="graph/meta/line/height"/>
  <xsl:variable name="width"  select="graph/meta/line/width"/>
  
  <xsl:variable name="margin">10</xsl:variable>
  <xsl:variable name="labelY">50</xsl:variable>
  <xsl:variable name="labelX">20</xsl:variable>
  
  <xsl:variable name="chartHeight" select="$height - (2 * $margin) - $labelX"/>
  <xsl:variable name="chartWidth"  select="$width  - (4 * $margin) - $labelY"/>
  
  <xsl:variable name="verticalRangeMax">
    <xsl:for-each select="//y">
      <xsl:sort order="descending" data-type="number" select="text()"/>
      <xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:variable name="horizontalRangeMax">
    <xsl:for-each select="//x">
      <xsl:sort order="descending" data-type="number" select="text()"/>
      <xsl:if test="position() = 1"><xsl:value-of select="."/></xsl:if>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:variable name="verticalScale">
    <xsl:value-of select="$chartHeight div $verticalRangeMax"/>
  </xsl:variable>
  
  <xsl:variable name="horizontalScale">
    <xsl:value-of select="$chartWidth  div $horizontalRangeMax"/>
  </xsl:variable>
  
  <xsl:template match="graph">
    <svg
      width="{$width}"
      height="{$height}">
  
  	  <g transform="matrix(1 0 0 -1 {$margin + $labelY} {$height - ($margin + $labelX)})">
        <xsl:call-template name="verticalGridlines">
          <xsl:with-param name="value">0</xsl:with-param>
          <xsl:with-param name="step">1</xsl:with-param>
        </xsl:call-template>
  
        <xsl:call-template name="horizontalGridlines">
          <xsl:with-param name="value">0</xsl:with-param>
          <xsl:with-param name="step">20000</xsl:with-param>
        </xsl:call-template>
  
        <xsl:apply-templates select="data/*"/>
  	  </g>
    </svg>
  </xsl:template>
  
  <xsl:template name="verticalGridlines">
    <xsl:param name="value">0</xsl:param>
    <xsl:param name="step"/>
  
  	<line
  	  id="verticalGridline_{$value}"
  	  x1="{$value * $horizontalScale}"
  	  y1="-2"
  	  x2="{$value * $horizontalScale}"
  	  y2="{$verticalRangeMax * $verticalScale}"
  	  style="fill:none;stroke:black;stroke-width:1"/>
  
  	<text
  	  transform="matrix(1 0 0 -1 0 -{$labelX})"
  	  x="{$value * $horizontalScale}"
  	  y="0"
  	  style="text-anchor:middle;font-size:12;fill:black">
      <xsl:value-of select="//meta/axis/x/value[position() = $value + 1]"/>
  	</text>
  
    <xsl:if test="$value + $step &lt; $horizontalRangeMax">
      <xsl:call-template name="verticalGridlines">
        <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
        <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="horizontalGridlines">
    <xsl:param name="value">0</xsl:param>
    <xsl:param name="step"/>
  
  	<line
  	  id="horizontalGridline_{$value}"
  	  x1="-2"
  	  y1="{$value * $verticalScale}"
  	  x2="{$horizontalRangeMax * $horizontalScale}"
  	  y2="{$value * $verticalScale}"
  	  style="fill:none;stroke:black;stroke-width:1"/>
  
  	<text
  	  transform="matrix(1 0 0 -1 -{$margin} -5)"
  	  x="0"
  	  y="-{$value * $verticalScale}"
  	  style="text-anchor:end;font-size:12;fill:black">
      <xsl:value-of select="$value"/>
  	</text>
  
    <xsl:if test="$value + $step &lt; $verticalRangeMax">
      <xsl:call-template name="horizontalGridlines">
        <xsl:with-param name="value"><xsl:value-of select="$value + $step"/></xsl:with-param>
        <xsl:with-param name="step"><xsl:value-of select="$step"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="set">
     <xsl:variable name="red"   select="colour/red/text()"/>
     <xsl:variable name="green" select="colour/green/text()"/>
     <xsl:variable name="blue"  select="colour/blue/text()"/>
  
     <xsl:variable name="line">
       <xsl:for-each select="values/*">
        <xsl:choose>
          <xsl:when test="position() = 1"><xsl:text>M </xsl:text></xsl:when>
          <xsl:otherwise><xsl:text>L </xsl:text></xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="x * $horizontalScale"/><xsl:text> </xsl:text><xsl:value-of select="y * $verticalScale"/>
       </xsl:for-each>
     </xsl:variable>
  
     <path
      id="{label}"
      d="{$line}"
      style="fill:none;stroke:rgb({$red},{$green},{$blue});stroke-width:2"/>
  
  </xsl:template>
  
  </xsl:stylesheet>
  
  
  1.1                  xml-batik/contrib/charts/xsl/pie.xsl
  
  Index: pie.xsl
  ===================================================================
  <?xml version="1.0"?>
  
  <!--
   *****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   *                                                                           *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************
  -->
  <!-- ====================================================================== -->
  <!-- Generate a simple pie chart.  Requires parser extensions for sin and   -->
  <!-- cos.                                                                   -->
  <!--                                                                        -->
  <!-- @author john.r.morrison@ntworld.com                                    -->
  <!-- @version $Id: pie.xsl,v 1.1 2001/05/07 22:27:59 vhardy Exp $                                                          -->
  <!-- ====================================================================== -->
  
  <xsl:stylesheet version="1.0"
  	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  	xmlns:math="http://xsl.lotus.com/java">
  
  <xsl:variable name="height" select="graph/meta/pie/height"/>
  <xsl:variable name="width"  select="graph/meta/pie/width"/>
  
  <xsl:variable name="radius">
    <xsl:choose>
      <xsl:when test="$height &lt; $width"><xsl:value-of select="2 * $height div 5"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="2 * $width div 5"/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <xsl:variable name="360degrees" select="sum(/graph/data//y)"/>
  <xsl:variable name="1degree"    select="360 div $360degrees"/>
  
  <xsl:template match="graph">
    <svg
      width="{$width}"
      height="{$height}">
  
      <xsl:comment>radius      = <xsl:value-of select="$radius"/></xsl:comment>
      <xsl:comment>360 degrees = <xsl:value-of select="$360degrees"/></xsl:comment>
      <xsl:comment>1 degree    = <xsl:value-of select="$1degree"/></xsl:comment>
  
  	  <g transform="matrix(1 0 0 1 {$width div 2} {$height div 2})">
        <xsl:apply-templates select="data/set"/>
  	  </g>
    </svg>
  </xsl:template>
  
  <xsl:template match="set">
    
    <xsl:variable name="curpos" select="position()"/>    
    <xsl:variable name="angleStart" select="$1degree * sum(//set[position() &lt; $curpos]/values/*/y)"/>
    <xsl:variable name="angle" select="sum(descendant::y) * $1degree"/>
  
    <xsl:comment>angle start = <xsl:value-of select="$angleStart"/></xsl:comment>
    <xsl:comment>angle       = <xsl:value-of select="$angle"/></xsl:comment>
    <xsl:comment>angle end   = <xsl:value-of select="$angle + $angleStart"/></xsl:comment>
  
    <xsl:variable name="xystart">
      <xsl:call-template name="xy">
        <xsl:with-param name="angle" select="$angleStart"/>
      </xsl:call-template>
    </xsl:variable>
  
    <xsl:variable name="xyend">
      <xsl:call-template name="xy">
        <xsl:with-param name="angle" select="$angle + $angleStart"/>
      </xsl:call-template>
    </xsl:variable>
  
    <path
      d="M0, 0 L {$xystart} A {$radius}, {$radius} 0 0 1 {$xyend} z"
      style="fill:rgb({colour/red/text()},{colour/green/text()},{colour/blue/text()}); stroke-width:1"/>
  
  </xsl:template>
  
  <xsl:template name="xy">
    <xsl:param name="angle"/>
  
    <xsl:variable name="rad" select="math:java.lang.Math.toRadians($angle)"/>
  
    <xsl:value-of select="$radius * math:java.lang.Math.cos($rad)"/><xsl:text>, </xsl:text>
    <xsl:value-of select="$radius * math:java.lang.Math.sin($rad)"/>
  </xsl:template>
  
  </xsl:stylesheet>
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org