`

funsionchart利用zoomline现实报表信息,将数据转为XML流--JAVA

阅读更多

1、界面

 

<div id="chart1div"></div>

javascript脚本:

 

var chart1 = new FusionCharts("<%=request.getContextPath()%>/FusionCharts/ZoomLine.swf", "0", "1000", "600", "0", "1");
           chart1.setDataXML(srcHtml);
           chart1.render("chart1div");   
 

 

2、XML类

package com.yihaodian.pis.util;
 import java.io.IOException; 
 import org.dom4j.Document; 
 import org.dom4j.DocumentHelper; 
 import org.dom4j.Element; 
 /**
  * 使用dom4j生成XML工具类
  * 
  * @author 向旗
  * 
  */ 
 public class XMLUtil { 
     private Document document = null; 
  
     public Document getDocument() { 
         return document; 
     } 
     /**
      * 构造方法,初始化Document
      */ 
     public XMLUtil() { 
         document = DocumentHelper.createDocument(); 
     } 
     /**
      * 生成根节点
      * 
      * @param rootName
      * @return
      */ 
     public Element addRoot(String rootName) { 
        Element root = document.addElement(rootName); 
         return root; 
     } 
     /**
      * 生成节点
      * 
      * @param parentElement
      * @param elementName
      * @return
      */ 
     public Element addNode(Element parentElement, String elementName) { 
         Element node = parentElement.addElement(elementName); 
         return node; 
     } 
     /**
      * 为节点增加一个属性
      * 
      * @param thisElement
      * @param attributeName
      * @param attributeValue
      */ 
     public void addAttribute(Element thisElement, String attributeName, 
             String attributeValue) { 
         thisElement.addAttribute(attributeName, attributeValue); 
     } 
     /**
      * 为节点增加多个属性
      * 
      * @param thisElement
      * @param attributeNames
      * @param attributeValues
      */ 
     public void addAttributes(Element thisElement, String[] attributeNames, String[] attributeValues) { 
         for (int i = 0; i < attributeNames.length; i++) { 
             thisElement.addAttribute(attributeNames[i], attributeValues[i]); 
         } 
     } 
     /**
      * 增加节点的值
      * 
      * @param thisElement
      * @param text
      */ 
     public void addText(Element thisElement, String text) { 
         thisElement.addText(text); 
     } 
     /**
      * 获取最终的XML
      * 
      * @return
      * @throws IOException
     */ 
     public String getXML() { 
         return document.asXML().substring(39); 
     } 
 }  
 

 

3、JAVA增加XML节点内容

 

XMLUtil xml = new XMLUtil();
        Element graph = xml.addRoot("graph");
        xml.addAttribute(graph, "caption", "CPI Category Report");
        // xml.addAttribute(graph, "subCaption", "消费者物价指数种类报告");
        xml.addAttribute(graph, "basefontsize", "12");
        xml.addAttribute(graph, "yAxisName", "价格CPI报表");
        xml.addAttribute(graph, "decimalPrecision", "2");// 小数精确度,0为精确到个位
        xml.addAttribute(graph, "showValues", "0");// 在报表上不显示数值
        xml.addAttribute(graph, "hovercapbg", "FFECAA");
        xml.addAttribute(graph, "hovercapborder", "F47E00");
        xml.addAttribute(graph, "animation", "1");
        xml.addAttribute(graph, "numdivlines", "4");
        xml.addAttribute(graph, "numVdivlines", "0");
        xml.addAttribute(graph, "numVdivlines", "0");
        xml.addAttribute(graph, "yaxisminvalue", "1000");
        xml.addAttribute(graph, "yaxismaxvalue", "1800");
        xml.addAttribute(graph, "lineThickness", "3");
        xml.addAttribute(graph, "rotateNames", "1");
        xml.addAttribute(graph, "numberSuffix", "%");

增加种类:

Element categories = xml.addNode(graph, "categories");
        for (int i = 0; i < datelen; i++) {
            Element category = xml.addNode(categories, "category");
            category.addAttribute("name", (new Date(dateMinDate.getTime() + i
                    * 24 * 60 * 60 * 1000)).toString());
        }

增加种类对应的数据:

Element dataset = xml.addNode(graph, "dataset");
                    xml.addAttribute(
                            dataset,
                            "color",
                            Integer.toString(
                                    (int) (Math.random() * 255 * 255 * 255))
                                    .toUpperCase());
                    xml.addAttribute(dataset, "showValue", "1");
                    xml.addAttribute(dataset, "alpha", "100");
                    xml.addAttribute(dataset, "anchorAlpha", "0");
                    xml.addAttribute(dataset, "lineThickness", "2");

                    xml.addAttribute(dataset, "seriesname", catNameStrs[i]);
                  
                    for (int m = 0; m < datelen; m++) {
                        Element  set= xml.addNode(dataset, "set");
                    }
 

4.关于传参的问题

传到JAVASCRIPT一方面利用AJAX传参,另一方面利用文本,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
<html xmlns="http://www.w3.org/1999/xhtml "> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Fusion Chart Test</title> 
<script type="text/javascript" src="FusionCharts.js"></script> 
<script type="text/javascript"> 
function show(){
var xml = document.getElementById("test").value;
alert(xml);
    var chart = new FusionCharts("ZoomLine.swf", "0", "800", "600",0,0); 
    chart.setDataXML(xml); 
    chart.render("chartDiv"); }
    </script>
</head> 
<body> 
<div id="chartDiv"></div> 
<input type="text" size="500" id="test"/>
 <input type="button" onclick="show()" value="show"/>
</body> 
</html>
 
(然后按按钮,发现可以安然运行,之所以你先前不行,是因为"与JAVASCRIPT中"有重叠影响。var xml="<graph><categories><category name="1"/><";那么程序只会得到var xml="<graph><categories><category name="后面的会报错,你可以将后台的参数设置在<input type="text" size="500" id="test"/>中<input type="text" size="500" id="test" value="${xml}"/>)
在文本框内输入<graph><categories><category name="1"/><category name="1"/><category name="1"/></categories><dataset><set value="5"/><set value="5"/><set value="5"/></dataset></graph> 有不懂的联系QQ526151410

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics