日期:2014-05-16 浏览次数:20377 次
package action; import java.awt.Rectangle; import java.awt.Shape; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.entity.ChartEntity; import org.jfree.chart.entity.StandardEntityCollection; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.data.category.DefaultCategoryDataset; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ //页面鼠标事件时需要的参数 private String hy_filename; /** * 显示图片 * @return */ public String show(){ HttpServletRequest req = ServletActionContext.getRequest(); HttpServletResponse res = ServletActionContext.getResponse(); hy_filename = createChartImage(req, res); return SUCCESS; } private String createChartImage(HttpServletRequest req, HttpServletResponse res){ JFreeChart chart = createChart(createData()); String filename = createUseMap(chart, 510, 300, req, res); return filename; } private JFreeChart createChart(DefaultCategoryDataset defaultcategorydataset){ JFreeChart chart = ChartFactory.createLineChart(null, //图形标题名称 null, //domain轴 Lable,横坐标Lable null, //range 轴 Lable,纵坐标Lable defaultcategorydataset, // dataset PlotOrientation.VERTICAL, //垂直显示 true, // legend? true, // tooltips? false); //URLs? return chart; } private String createUseMap(JFreeChart chart, int width, int height, HttpServletRequest req, HttpServletResponse res){ //在矩形框中显示信息 Shape shape = new Rectangle(20, 10); ChartEntity entity = new ChartEntity(shape); StandardEntityCollection coll = new StandardEntityCollection(); coll.add(entity); //该工具类上面没有介绍,在鼠标移动到图片时显示提示信息是用Map实现的,这些Map是用该类生成的。 ChartRenderingInfo info = new ChartRenderingInfo(coll); PrintWriter pw; String filename = null; try { res.setContentType("text/html;charset=utf-8"); res.setCharacterEncoding("utf-8"); pw = res.getWriter();//输出MAP信息 //写入到输出流生成图像文件,同时把图片的具体信息放入ChartRenderingInfo的一个实例为以后生成Map提供信息 //ChartUtilities.writeChartAsPNG(out, chart, width, height, info); filename = ServletUtilities.saveChartAsPNG(chart, width , height, info, req.getSession());//保存图表为文件 //读取info对象,生成Map信息。这些信息写在pw的输出流中,这里的输出流就是Response.out,也就是直接输出到页面了 ChartUtilities.writeImageMap(pw, filename, info, false); pw.flush(); } catch (IOException e) { e.printStackTrace(); } return filename; } private DefaultCategoryDataset createData(){ String series1 = "血糖"; String series2 = "舒张压"; String series3 = "收缩压"; String type1 = "2009-01-01"; String type2 = "2009-02-01"; String type3 = "2009-03-01"; String type4 = "2009-04-01"; String type5 = "2009-05-01"; String type6 = "2009-06-01"; String type7 = "2009-07-01"; String type8 = "2009-08-01"; DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); defaultcategorydataset.addValue(1.0D, series1, type1); defaultcategorydataset.addValue(2D, series1, type2); defaultcategorydataset.addValue(3D, series1, type3); defaultcategorydataset.addValue(5D, series1, type4); defaultcategorydataset.addValue(5D, series1, type5); defaultcategorydataset.addValue(7D, series1, type6); defaultcategorydataset.addValue(7D, series1, type7); defaultcategorydataset.addValue(8D, series1, type8); defaultcatego