情况是这样的,用FusionCharts 做一些统计图,
实现方式有两种,setDataURL 和 setDataXML
用setDataURL可以实现统计图,但问题是不能显示中文,不光是Y轴不能显示,
就连标题和X轴出现中文的时候也报错,提示非法数据;网上的朋友说把setDataURL
改成setDataXML 实现就能使用中文,现在问题来了:
public class Java2XML_CN {
public Document createDoc(){
// 创建根节点 chart;
Element root = new Element("chart");
root.setAttribute("caption", "Monthly Unit Sales");
root.setAttribute("xAxisName", "Month");
root.setAttribute("yAxisName", "Units");
root.setAttribute("showValues", "1");
root.setAttribute("decimals", "0");
root.setAttribute("formatNumberScale", "0");
// 根节点添加到文档中;
Document Doc = new Document(root);
String[] lable = { "Jan", "Fab", "Mar", "Apr", "May", "Jun", "Jul",
"Agu", "Sep", "Otp", "Nov", "Dec" };
String[] value = { "345", "123", "634", "811", "234", "897", "975",
"332", "188", "223", "678", "346" };
// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
for (int i = 0; i < lable.length; i++) {
// 创建节点 set;
Element elements = new Element("set");
// 给 set节点添加属性 id;
elements.setAttribute("label", lable[i]);
elements.setAttribute("value", value[i]);
// 给父节点chart添加set子节点;
root.addContent(elements);
}
return Doc;
}
//把jdom的document转换成string
public String XmlDocumentToString() throws IOException{
Document doc = createDoc();
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");//设置xml文件的字符为gb2312,解决中文问题
XMLOutputter xmlout = new XMLOutputter(format);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(doc,bo);
String xmlStr = bo.toString().replaceAll("\"", "'");
return xmlStr;
}
}
这个在jsp页面调用的时候 确实能返回一个String形式的xml数据:
<?xml version='1.0' encoding='UTF-8'?>
<chart caption='Monthly Unit Sales' xAxisName='Month' yAxisName='Units' showValues='1' decimals='0' formatNumberScale='0'>
<set label='Jan' value='345' />
<set label='Fab' value='123' />
<set label='Mar' value='634' />
<set label='Apr' value='811' />
<set label='May' value='234' />
<set label='Jun' value='897' />
<set label='Jul' value='975' />
<set label='Agu' value='332' />
<set label='Sep' value='188' />
<set label='Otp' value='223' />
<set label='Nov' value='678' />
<set label='Dec' value='346' />
</chart>
但问题是在jsp页面显示的时候出问题:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.test.*;"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
Java2XML_CN jc = new Java2XML_CN();
String xmlData = (String)jc.XmlDocumentToString();
System.out.print(xmlData);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>