通过xslt实现的VML统计饼图(代码共享)

liuruhong 2003-09-03 03:51:08
数据
-----------------------------
<?xml version="1.0" encoding="gb2312" ?>
<root>
<DataItem name="北京" value ="23" title="北京的人口"/>
<DataItem name="上海" value="49" title="上海的人口"/>
<DataItem name="成都" value="89" title="成都的人口"/>
<DataItem name="天津" value="36" title="天津的人口"/>
<DataItem name="武汉" value="15" title="武汉的人口"/>
<DataItem name="西安" value="57" title="西安的人口"/>
<DataItem name="d安" value="57" title="西安的人口"/>
<DataItem name="a安" value="57" title="西安的人口"/>
</root>


HTML文件
------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns:v>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="ProgId" content="VisualStudio.HTML">
<meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
<style> v\:* { BEHAVIOR: url(#default#VML)
</style>
<script>
function getxml(){
var xmlDoc=new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
xmlDoc.load("chartdata.xml");
var xslDoc=new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
xslDoc.load("VMLChart.xslt");

var template=new ActiveXObject("MSXML2.XSLTemplate");
template.stylesheet=xslDoc;
var proc=template.createProcessor();

proc.input=xmlDoc;
proc.addParameter("Caption","测试图表");
proc.addParameter("Width",600);
proc.addParameter("Height",450);
proc.addParameter("BackgroundColor","#e6e6e6");
proc.addParameter("Shadow","on");

proc.transform();
var strResult=proc.output;
result.innerHTML=strResult;
}
</script>
</head>
<body>
<input onclick="getxml()" value="生成" type="button">
<div id="result">

</div>
</body>
</html>
...全文
49 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuruhong 2004-03-09
  • 打赏
  • 举报
回复
恩,我考虑的不完整,你可以使用FreeThreadedXMLDocument替换xmldocument
大可山人 2004-03-05
  • 打赏
  • 举报
回复
上述代码能以本地物理路径方式运行,但如果改为网站相对路径运行就报错!
例如:L:\try\PIE\xsltPie\pie.htm可以运行,但改为:
http://localhost/try/PIE/xsltPie/pie.htm运行就报错:
完成该操作的数据还不可使用。第18行出错,是: template.stylesheet=xslDoc;
为何?
liuruhong 2003-09-06
  • 打赏
  • 举报
回复
msxml支持<msxsl:script>
速马 2003-09-06
  • 打赏
  • 举报
回复
XSLT好像没有支持函数吧?
liuruhong 2003-09-05
  • 打赏
  • 举报
回复
有谁可以将我的JavaScript代码去掉,全部用xslt来实现?
liuruhong 2003-09-03
  • 打赏
  • 举报
回复
xslt文件
------------------------------------------------
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:VMLObject="http://blueswing.vicp.net" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:param name="Caption"/>
<xsl:param name="Width"/>
<xsl:param name="Height"/>
<xsl:param name="Shadow"/>
<xsl:param name="BorderWidth"/>
<xsl:param name="BorderColor"/>
<xsl:param name="BackgroundColor"/>


<msxsl:script language="javascript" implements-prefix="VMLObject">
<![CDATA[
var curValue=0;
var preColor=0;
var mX=Math.pow(2,16) * 360;
function startAngle(vValue){
var preValue=curValue;
curValue+=vValue;
return parseInt(mX * preValue);
}

function endAngle(vValue){
return parseInt(mX * vValue);
}

function randomColor(){
preColor= "rgb("+ parseInt( Math.random() * 255) +"," +parseInt( Math.random() * 255) +"," +parseInt( Math.random() * 255)+")";
return preColor;
}

function curColor(){
return preColor;
}
]]>
</msxsl:script>
<xsl:template match="/">
<xsl:variable name="PieValueTotal" select="sum(//DataItem/@value)"/>
<xsl:variable name="PieCount" select="count(//DataItem)"/>
<v:group coordsize="21600,21600">
<xsl:attribute name="style">
width:<xsl:value-of select="$Width"/>px;height:<xsl:value-of select="$Height"/>px
</xsl:attribute>
<v:rect style="width:21600px;height:21600px">
<xsl:if test="$BorderWidth">
<xsl:attribute name="strokeweight"><xsl:value-of select="$BorderWidth"/></xsl:attribute>
</xsl:if>
<xsl:if test="$BorderColor">
<xsl:attribute name="strokecolor"><xsl:value-of select="$BorderColor"/></xsl:attribute>
</xsl:if>
<xsl:if test="$BackgroundColor">
<xsl:attribute name="fillcolor"><xsl:value-of select="$BackgroundColor"/></xsl:attribute>
</xsl:if>

<v:textbox style="text-Align:center;font-size:24px;font-Weight:bold;height:24px">
<xsl:value-of select="$Caption"/>

</v:textbox>

<xsl:if test="$Shadow">
<v:shadow on="t"/>
</xsl:if>
</v:rect>



<xsl:for-each select="//DataItem">
<v:shape style="width:15000px;height:14000px;top:4000px;left:1000px" adj="0,0" strokecolor="window" coordsize="1500,1400">
<xsl:attribute name="path">
M 750 700 AE 750 700 750 700 <xsl:value-of select="VMLObject:startAngle(@value div $PieValueTotal) "/>  <xsl:value-of select="VMLObject:endAngle(@value div $PieValueTotal)"/> xe
</xsl:attribute>

<xsl:attribute name="fillcolor">
<xsl:value-of select="VMLObject:randomColor()"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@title"/>
</xsl:attribute>


</v:shape>
<!--
开始绘制图例
-->
<v:rect>
<xsl:attribute name="style">
left:17000px;width:600px;height:800px;top:<xsl:value-of select="1200 * position() + 1000"/>px
</xsl:attribute>

<xsl:attribute name="fillcolor">
<xsl:value-of select="VMLObject:curColor()"/>
</xsl:attribute>
</v:rect>
<v:rect strokecolor="black">
<xsl:attribute name="style">
left:17500px;top:<xsl:value-of select="1200 * position() + 1000"/>px
</xsl:attribute>
<v:textbox style="font:caption">
<xsl:value-of select="@name"/>(
<xsl:value-of select="@value"/>)
</v:textbox>
</v:rect>
</xsl:for-each>
</v:group>
</xsl:template>
</xsl:stylesheet>

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧