zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    链表和数组的区别在哪里 【微软面试100题 第七十八题】
    关于链表问题的面试题目 【微软面试100题 第七十七题】
    复杂链表的复制 【微软面试100题 第七十六题】
    二叉树两个结点的最低公共父结点 【微软面试100题 第七十五题】
    数组中超过出现次数一半的数字 【微软面试100题 第七十四题】
    对称字符串的最大长度 【微软面试100题 第七十三题】
    Singleton模式类 【微软面试100题 第七十二题】
    数值的整数次方 【微软面试100题 第七十一题】
    旋转数组中的最小元素 【微软面试100题 第六十九题】
    把数组排成最小的数 【微软面试100题 第六十八题】
  • 原文地址:https://www.cnblogs.com/jpfss/p/7404946.html
Copyright © 2011-2022 走看看