-- 作者:pertoleum
-- 发布时间:5/18/2005 10:12:00 PM
-- [求助]用dom4j来实现画饼图
我现在正在做一个关于用dom4j来生成xml文件的东西,关于如何把javascript的 <script> <![CDATA …… ]]> </script>块中的内容写进文件中,不知道用哪个函数?我全部用了addCDATA()这个函数。程序运行没有问题,可以生成.svg的文件,但是生成的文件却无法打开。 代码如下,望各位给指导一下。 package dom4j; import java.io.File; import java.io.FileWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Pie { public static int createXMLFile(String filename){ int returnvalue = 0; Document document = DocumentHelper.createDocument(); Element svg=document.addElement("svg"); svg.addAttribute("width","100%" ); svg.addAttribute("height","100%"); svg.addAttribute("onload","init(event)"); Element script=svg.addElement("script"); script.addAttribute("type","text/javascript"); script.addCDATA("var width = 800.;"); script.addCDATA("var height = 500.;"); script.addCDATA("var basePointX = 300.;"); script.addCDATA("var basePointY = 150.;"); script.addCDATA("var currentX = 0.0;"); script.addCDATA("var currentY = 0.0;"); script.addCDATA("var offsetX1 = 0.0;"); script.addCDATA("var offsetY1 = 0.0;"); script.addCDATA("var offsetX2 = 0.0;"); script.addCDATA("var offsetY2 = 0.0;"); script.addCDATA("var radius = 100.;"); script.addCDATA("var angleSum1 = 0.;"); script.addCDATA("var angleSum2 = 0.;"); script.addCDATA("var vertexCount = 8;"); script.addCDATA("var xPts = Array(vertexCount);"); script.addCDATA("var yPts = Array(vertexCount);"); script.addCDATA("var angles = Array(vertexCount);"); script.addCDATA("var pointPath = \"\";"); script.addCDATA("var circleColors = ['red','green','blue','yellow'];"); script.addCDATA("var colorCount = 4.;"); script.addCDATA("var pieNode = null;"); script.addCDATA("var svgDocument = null;"); script.addCDATA("var target = null;"); script.addCDATA("var gcNode = null;"); script.addCDATA("function init(event){"); script.addCDATA("target = event.getTarget();"); script.addCDATA("svgDocument = target.getOwnerDocument();"); script.addCDATA("gcNode = svgDocument.getElementById(\"gc\");"); script.addCDATA("initializeChart();"); script.addCDATA("drawChart();"); script.addCDATA("}"); script.addCDATA("function initializeChart(){"); script.addCDATA("angles[0] = 30.;"); script.addCDATA("angles[1] = 60.;"); script.addCDATA("angles[2] = 45.;"); script.addCDATA("angles[3] = 45.;"); script.addCDATA("angles[4] = 20.;"); script.addCDATA("angles[5] = 30.;"); script.addCDATA("angles[6] = 50.;"); script.addCDATA("angles[7] = 80.;"); script.addCDATA("}"); script.addCDATA("function drawChart(){"); script.addCDATA("for(var v=0;v<angles.length;v++){"); script.addCDATA("angleSum2 = angleSum1 + angles[v];"); script.addCDATA("offsetX1 = radius*Math.cos(angleSum1*Math.PI/180);"); script.addCDATA("offsetY1 = radius*Math.sin(angleSum1*Math.PI/180);"); script.addCDATA("offsetX2 = radius*Math.cos(angleSum2*Math.PI/180);"); script.addCDATA("offsetY2 = radius*Math.sin(angleSum2*Math.PI/180);"); script.addCDATA("currentX = basePointX+offsetX2;"); script.addCDATA("currentY = basePointY-offsetY2;"); //垂直方向坐标需要做减法,所以将offsetY1 取反 script.addCDATA("offsetY1 *= -1;"); script.addCDATA("pointPath = \"M\"+basePointX+\",\"+basePointY;"); script.addCDATA("pointPath += \" l\"+offsetX1+\",\"+offsetY1;"); script.addCDATA("pointPath += \" A\"+radius+\",\"+radius+\" 0 0 0 \";"); //圆是椭圆的特例,前后2个radius分别表示椭圆的长轴和短轴 //前一个0,表画出的圆弧的旋转角度为0,即不旋转; 0,0表示画劣弧,且按逆时针方向画 script.addCDATA("pointPath += currentX+\",\"+currentY;"); script.addCDATA("pointPath += \" L\"+basePointX+\",\"+basePointY+\"z\";"); script.addCDATA("var style = \"\";"); script.addCDATA("var strokeWidth = 1.;"); script.addCDATA("var strokeColor = 'white';"); script.addCDATA("stroke = \"stroke:\"+strokeColor+\";\";"); script.addCDATA("stroke += \"stroke-width:\"+strokeWidth+\";\";"); script.addCDATA("fillColor = \"fill:\"+circleColors[v%colorCount];"); script.addCDATA("style = stroke+fillColor;"); script.addCDATA("pieNode = svgDocument.createElement(\"path\");"); script.addCDATA("pieNode.setAttribute(\"d\", pointPath);"); script.addCDATA("pieNode.setAttribute(\"style\",style);"); script.addCDATA("gcNode.appendChild(pieNode);"); script.addCDATA("angleSum1 = angleSum2;"); script.addCDATA("}"); script.addCDATA("}"); Element g=svg.addElement("g"); g.addAttribute("id","gc"); g.addAttribute("transform","translate(10,10)"); Element rect=g.addElement("rect"); rect.addAttribute("x","0"); rect.addAttribute("y","0"); rect.addAttribute("width","800"); rect.addAttribute("height","500"); rect.addAttribute("fill","lightblue"); rect.addAttribute("stroke","red"); try{ /** 将document中的内容写入文件中 */ XMLWriter writer = new XMLWriter(new FileWriter(new File(filename))); writer.write(document); writer.close(); /** 执行成功,需返回1 */ returnvalue = 1; }catch(Exception ex){ ex.printStackTrace(); } return returnvalue; } public int formatXMLFile(String filename){ int returnvalue = 0; try{ SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File(filename)); XMLWriter writer = null; /** 格式化输出,类型IE浏览一样 */ OutputFormat format = OutputFormat.createPrettyPrint(); /** 指定XML编码 */ format.setEncoding("GBK"); writer= new XMLWriter(new FileWriter(new File(filename)),format); writer.write(document); writer.close(); /** 执行成功,需返回1 */ returnvalue = 1; }catch(Exception ex){ ex.printStackTrace(); } return returnvalue; } public static void main(String[] args) { Pie pie1 = new Pie(); System.out.println(pie1.createXMLFile("d://p.svg")); System.out.println(pie1.formatXMLFile("d://p.svg")); } }
|