科研项目管理系统之funsionchartsFree使用心得

使用funsionchartsFree实现可视化数据统计,关键代码和笔记如下:

1、dom4j解析文件,写(创建xml数据文件)和直接用j2se IO的方法读出来:

public class Pie3DXML {
	public static Boolean createXML(Map<String, String> map, String fileName) {
		Boolean flag = false;
		try {
			/** 定义创建时间 */
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String date = s.format(new Date());

			/** 建立document对象 */
			Document document = DocumentHelper.createDocument();

			/** 建立XML文档的根graph */
			Element graphElement = document.addElement("graph");

			/** 加入一行注释 */
			graphElement.addComment("This is a new XML file,创建日期:" + date);

			/** 创建根节点属性 */
			graphElement.addAttribute("showNames", "1");
			graphElement.addAttribute("decimalPrecision", "0");

			/** 创建set节点 */
			Set<String> keys = map.keySet();
			Iterator<String> it = keys.iterator();
			while (it.hasNext()) {
				String name = it.next();
				String value = map.get(name);
				Element setElement = graphElement.addElement("set");
				setElement.addAttribute("name", name);
				setElement.addAttribute("value", value);
			}

			/** 将document中的内容写入文件中 */
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");
			XMLWriter writer = new XMLWriter(new OutputStreamWriter(
					new FileOutputStream(fileName), "UTF-8"));
			writer.write(document);
			writer.close();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("生成出错啦!");
		}
		return flag;
	}
	public static String readXML(String srcURL) throws Exception{
		Reader reader=new FileReader(new File(srcURL));
		String value="";
		int temp;
		while((temp=reader.read())!=-1){
			value+=(char)temp;
		}
		value=value.replace("\n", "").replace("<?xml version=\"1.0\" encoding=\"GBK\"?>", "");
		return value;
	}
	

}

2、经过Action的处理后定向到的jsp视图元素:

<script type="text/javascript">
function updateChart(chartURL){
var xmlData=document.myForm.xmlValue.value;
var chart = new FusionCharts(chartURL, "ChartId", "700", "500");
chart.setDataXML(xmlData);
chart.render(
"chartdiv");
}
function displayTag(ex){
if(ex==0){
document.all.grap0.style.display
="none";
document.all.grap1.style.display
="";
document.all.grap2.style.display
="";
}
if(ex==1){
document.all.grap0.style.display
="";
document.all.grap1.style.display
="none";
document.all.grap2.style.display
="";
}
if(ex==2){
document.all.grap0.style.display
="";
document.all.grap1.style.display
="";
document.all.grap2.style.display
="none";
}
}

</script>


<input type="hidden" name="xmlValue" value='${xmlValue}'/><!--xml数据,setDataXML()要取的数据-->


<input type="button" id="grap0" onclick="updateChart('admin/achievementAnalyse/Charts/FCF_Line.swf');displayTag(0)" value="折线图"/>&nbsp;&nbsp;
<input type="button" id="grap1" onclick="updateChart('admin/achievementAnalyse/Charts/FCF_Column3D.swf');displayTag(1)" value="柱状图"/>
<input type="button" id="grap2" onclick="updateChart('admin/achievementAnalyse/Charts/FCF_Pie3D.swf');displayTag(2)" value="饼状图"/>


<td valign="top" class="text" align="center"> <div id="chartdiv" align="center">&nbsp;FusionCharts. </div>
<script type="text/javascript">
displayTag(
2);
var chart = new FusionCharts("admin/achievementAnalyse/Charts/FCF_Pie3D.swf", "ChartId", "700", "500");
chart.setDataURL(
"admin/achievementAnalyse/Gallery/Data/Pie3D.xml");
chart.render(
"chartdiv");
  </script>

附:funsioncharts部分功能是收费的,貌似动态变图类型时setDataURL方法是收费的,未经核实。

posted @ 2011-05-15 16:16  fzx888888  阅读(762)  评论(0编辑  收藏  举报