新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> XML编辑器,XML解析器,XML开发环境
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 XML工具及XML开发环境 』 → [求助]用dom4j来实现画饼图 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2483 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [求助]用dom4j来实现画饼图 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     pertoleum 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:2
      积分:59
      门派:XML.ORG.CN
      注册:2005/5/2

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给pertoleum发送一个短消息 把pertoleum加入好友 查看pertoleum的个人资料 搜索pertoleum在『 XML工具及XML开发环境 』的所有贴子 引用回复这个贴子 回复这个贴子 查看pertoleum的博客楼主
    发贴心情 [求助]用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"));
      }

    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/5/18 22:12:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 XML工具及XML开发环境 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/11/22 19:58:17

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms