【发布时间】:2018-11-12 16:10:30
【问题描述】:
我正在寻找一种使用 Java Apache FOP 在 XSLT 中生成 条形图 的方法。
我正在尝试生成具有以下 XML 和 XSLT 的条形图,但它生成的空 .PDF 文件没有任何图表?
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<sales>
<region>
<title>Eastern Region Quarterly Sales (Second/'04)</title>
<key1 area="New York Area">.95</key1>
<key2 area="Virginia Area">.89</key2>
<key3 area="Maryland Area">.67</key3>
<key4 area="Connecticut Area">.65</key4>
<key5 area="Delaware Area">.45</key5>
</region>
</sales>
bar.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="sales">
<svg width="650" height="500">
<g id="axis" transform="translate(0 500) scale(1 -1)">
<line id="axis-y" x1="30" y1="20" x2="30" y2="450" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
<line id="axis-x" x1="30" y1="20" x2="460" y2="20" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
</g>
<xsl:apply-templates select="region" />
</svg>
</xsl:template>
<xsl:template match="region">
<g id="bars" transform="translate(30 479) scale(1 -430)">
<rect x="30" y="0" width="50" height="{key1}" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="100" y="0" width="50" height="{key2}" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="170" y="0" width="50" height="{key3}" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="240" y="0" width="50" height="{key4}" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="310" y="0" width="50" height="{key5}" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:0" />
</g>
<g id="scale" transform="translate(29 60)">
<text id="scale1" x="0px" y="320px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$25K</text>
<text id="scale2" x="0px" y="215px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$50K</text>
<text id="scale3" x="0px" y="107.5px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$75K</text>
<text id="scale4" x="0px" y="0px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$100K</text>
</g>
<g id="key">
<rect id="key1" x="430" y="80" width="25" height="15" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key2" x="430" y="100" width="25" height="15" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key3" x="430" y="120" width="25" height="15" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key5" x="430" y="140" width="25" height="15" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key4" x="430" y="160" width="25" height="15" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1" />
</g>
<text id="key1-text" x="465px" y="92px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key1/@area" />
</text>
<text id="key2-text" x="465px" y="112px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key2/@area" />
</text>
<text id="key3-text" x="465px" y="132px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key3/@area" />
</text>
<text id="key4-text" x="465px" y="152px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key4/@area" />
</text>
<text id="key5-text" x="465px" y="172px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key5/@area" />
</text>
<g id="title">
<text x="325px" y="20px" style="text-anchor:middle;fill:rgb(0,0,0);font-size:24;font-family:Arial">
<xsl:value-of select="title" />
</text>
</g>
</xsl:template>
</xsl:stylesheet>
我不知道为什么我得到空的 pdf 并且我也尝试生成 svg 文件 - 它也没有显示任何图表。任何帮助将不胜感激。
【问题讨论】:
-
您可以使用
fo:instream-foreign-object在 XSL-FO 中创建和嵌入 SVG。这是一个示例:stackoverflow.com/questions/4121501/a-beginner-question-on-xslt/… 这并不是您想要做的,因为在那个答案中 SVG 已经存在。您必须在 XSLT 中创建 SVG 才能在fo:instream-foreign-object中使用。 -
感谢您的回复!如何使用 xml 或 xslt 创建 SVG 图表?
-
Daniel Haley 的建议很好。在 XSLT 中创建 条形图 条的坐标,将它们输出为 SVG,然后在 Apache FOP 中的
fo:instream-foreign-object中使用它们以将它们嵌入到 PDF 中。这听起来像是一个不错的小练习,如果你在这里发布一个工作示例,我会赞成。 -
@zx485 - 在我的回答中添加了一个工作示例。 :-)
-
@DanielHaley:看起来不错,因此我赞成 :-)
标签: java xml xslt charts apache-fop