我使用的jfreechar的版本是
先说一下我是如何来做的,以及出现的问题,以及如何解决的
现在做的项目是升级,以前的代码全部写在JSP上,所以我现在改成了自定义标签,的方式生成饼图,和折线图;
以下是之前代码:
生成图的Java类和方法:
public static String getLineURLTemp(HttpSession session,PrintWriter pw,
CategoryDataset barDataset,String title,String xName,String yName,int width,int height)
throws ServletException, IOException {
String filename = "";
try{
// 把生成的文件写入到临时的目录中
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(createLineXYChar(title,xName,yName,barDataset), width, height, info,session);
// 选择存储成png格式的文件,当然你也可以使用saveChartAsJPEG的方法生成jpg图片
// 把image map 写入到 PrintWriter
ChartUtilities.writeImageMap(pw, filename, info, false);//这里一定要设成false
pw.flush();
}catch (Exception e){
System.err.println(e.toString());
}
return filename;
}
在修改代码之前生成图一切正常:(问题就出在map标签的输出上)
之前生成图之后map标签得到了及时的输出,参杂在HTML代码中:(下图)
下面是该后的标签,和对应的标签类:
TLD文件
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<display-name>chart</display-name>
<tlib-version>1.0</tlib-version>
<short-name>chart</short-name>
<uri>http://www.yhc.com/chart</uri>
<tag>
<description>
该标签是判断并输出标签和产生饼图
</description>
<name>createChart</name>
<tag-class>com.blue.himp.his.taglib.CreateChartTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>
饼状图对象,注意饼图和折线图只能设置一个
</description>
<name>pieChart</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
折线图对象,注意饼图和折线图只能设置一个
</description>
<name>lineGraph</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
图的名称
</description>
<name>title</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
xName,创建折线图,需要属性
</description>
<name>xName</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
yName,创建折线图,需要属性
</description>
<name>yName</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
图片的宽度
</description>
<name>width</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
图片的高度
</description>
<name>height</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>
图片的比例,如果设置,那么最后就等于,width*proportion 和height*proportion
</description>
<name>proportion</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
对应的Java类:
package com.blue.himp.his.taglib;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import com.blue.himp.his.bean.OutpatientVo;
import com.blue.himp.util.CreatChart;
import com.blue.summer.core.exception.ActionException;
public class CreateChartTag extends TagSupport{
private Integer width=0;//宽度
private Integer height=0;//高度
private Integer proportion=1;//比例
//折线图对象
private CategoryDataset lineGraph;
//饼状图对象
private DefaultPieDataset pieChart;
//图像名称
private String title;
//
private String xName;
//
private String yName;
//输出流
private Writer out;
/***
* 开始该标签的事件
*/
@Override
public int doStartTag() throws JspException,ActionException{
if(lineGraph==null&&pieChart==null){
return Tag.SKIP_BODY;//不执行该标签
}
//得到请求对象
HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
//得到Session
HttpSession session=request.getSession();
//输出流
//out=pageContext.getOut();
if(width==0){
width=proportion*80;//宽度
}
if(height==0){
height=68*proportion;//高度
}
try {
out= pageContext.getResponse().getWriter();
} catch (IOException e) {
e.printStackTrace();
throw new ActionException("生成图失败.", e);
}
String fileName="";//文件名称
String imageURL="";//该文件的访问路径
if(lineGraph!=null){//折线图
try {
//创建图得到文件名称
fileName=CreatChart.getLineURLTemp(session, new PrintWriter(out),
lineGraph,title,xName,yName,width,height);
//对应生成该图的访问url
imageURL=request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
} catch (Exception e) {
e.printStackTrace();
}
}
if(pieChart!=null){//饼状图
try {
//创建图得到文件名称
fileName=CreatChart.get3DPieURLTemp(session, new PrintWriter(out),
pieChart,title,width,height);
//对应生成该图的访问url
imageURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
} catch (Exception e) {
e.printStackTrace();
throw new ActionException("生成图失败.", e);
}
}
//将对应的值放到请求的作用域中
request.setAttribute("fileName",fileName);//名称
request.setAttribute("imageURL",imageURL);//图片URL
request.setAttribute("width", width);//图片宽度
request.setAttribute("height", height);//图片高度
//System.out.println("执行过....");
return Tag.EVAL_PAGE;//正常的流程继续执行JSP
}
//=============================getter setter
@Override
public void setPageContext(PageContext pageContext) {
super.setPageContext(pageContext);
}
public void setLineGraph(CategoryDataset lineGraph) {
this.lineGraph = lineGraph;
}
public void setPieChart(DefaultPieDataset pieChart) {
this.pieChart = pieChart;
}
public void setTitle(String title) {
this.title = title;
}
public void setxName(String xName) {
this.xName = xName;
}
public void setyName(String yName) {
this.yName = yName;
}
public void setWidth(Integer width) {
this.width = width;
}
public void setHeight(Integer height) {
this.height = height;
}
public void setProportion(Integer proportion) {
this.proportion = proportion;
}
}
该过之后map标签的输出问题,是不能及时输出(输出的map标签参杂在js文件中,由于使用了ExtJs,这时脚本会报错)
以下是JSP改为标签之后的效果:datdaSet是在Action中生成的
以下是输出后的HTML源代码:(没有生成到HTML标签中而是参杂在JS代码中)
以下是自定义标签生成后的内容:(效果图)
那么问题是出在了什么地方了(需要注意的是我的标签类,有一个获得输出流的地方被注释了)(图1)
上面注释的获得流的方法是我修改后的,没有注释获得流的是之前的(图2)
那么现在把(图1)代码解禁,将(图2)代码注释:
在看看自定义标签生成后的HTML源代码:(是不是已经被及时写出去了)
需要注意的是这两个流的类型一个是JSPWriter,另一个是PrintWriter
在来看看该JSP生成的JAVA代码,明白了吧: