SSH框架通过JFreeChart实现柱状图和获取项目路径

获取项目路径:String url= ServletActionContext.getRequest().getRealPath("/upload");

一.直接生成的图片输出到jsp页面

1.jsp页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <%    
  5. String path = request.getContextPath();    
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";    
  7. %>   
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  9. <html>  
  10. <head>  
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  12. <base href="<%=basePath%>">  
  13. <title>Insert title here</title>  
  14. </head>  
  15. <body>  
  16.  <!--img src='D: empchart.jpg' width="680" height="700" onload="alert('图片存在');"  onerror="alert('无此图片');"/-->  
  17.  <img alt="统计图" src="<%=basePath%>/chart2.action">     
  18. </body>  
  19. </html>  

2.action类

  1. public String execute() throws Exception{  
  2.  //添加数据  
  3.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  4.         dataset.addValue(42000"月份" , "1月");  
  5.         dataset.addValue(40000 , "月份" , "2月");  
  6.         dataset.addValue(34000 , "月份" , "3月");  
  7.         dataset.addValue(18000 , "月份" , "4月");  
  8.         dataset.addValue(26000 , "月份" , "5月");  
  9.         dataset.addValue(42000 , "月份" , "6月");  
  10.         dataset.addValue(40000 , "月份" , "7月");  
  11.         dataset.addValue(34000 , "月份" , "8月");  
  12.         dataset.addValue(18000 , "月份" , "9月");  
  13.         dataset.addValue(26000 , "月份" , "10月");  
  14.         dataset.addValue(45000 , "月份" , "11月");  
  15.         dataset.addValue(38000 , "月份" , "12月");  
  16.           
  17.         //创建一个柱状图  
  18.         JFreeChart chart = ChartFactory.createBarChart3D("年度人口统计图""人口""数量",dataset,PlotOrientation.VERTICAL, truefalsefalse);  
  19.         chart.setTitle(new TextTitle("年度人口统计图"new Font("黑体", Font.ITALIC,22)));  
  20.         LegendTitle legend = chart.getLegend(0);  
  21.         legend.setItemFont(new Font("宋体", Font.BOLD, 14));  
  22.         CategoryPlot plot = (CategoryPlot) chart.getPlot();  
  23.         CategoryAxis categoryAxis = plot.getDomainAxis();  
  24.         categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));  
  25.         categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);  
  26.         categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18));  
  27.         NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();  
  28.         numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));  
  29.   
  30.         HttpServletResponse response=ServletActionContext.getResponse();  
  31.         response.setContentType("image/");  
  32.         ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1, chart, 900600null);  
  33.         response.getOutputStream().close();  
  34.           
  35.         return null;  
  36. }  
原文地址:https://www.cnblogs.com/jpfss/p/7404946.html