SSH框架通过JFreeChart实现柱状图和获取项目路径
获取项目路径:String url= ServletActionContext.getRequest().getRealPath("/upload");
一.直接生成的图片输出到jsp页面
1.jsp页面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <base href="<%=basePath%>">
- <title>Insert title here</title>
- </head>
- <body>
- <!--img src='D:\temp\chart.jpg' width="680" height="700" onload="alert('图片存在');" onerror="alert('无此图片');"/-->
- <img alt="统计图" src="<%=basePath%>/chart2.action">
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>Insert title here</title>
</head>
<body>
<!--img src='D:\temp\chart.jpg' width="680" height="700" onload="alert('图片存在');" onerror="alert('无此图片');"/-->
<img alt="统计图" src="<%=basePath%>/chart2.action">
</body>
</html>
2.action类
- public String execute() throws Exception{
- //添加数据
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- dataset.addValue(42000, "月份" , "1月");
- dataset.addValue(40000 , "月份" , "2月");
- dataset.addValue(34000 , "月份" , "3月");
- dataset.addValue(18000 , "月份" , "4月");
- dataset.addValue(26000 , "月份" , "5月");
- dataset.addValue(42000 , "月份" , "6月");
- dataset.addValue(40000 , "月份" , "7月");
- dataset.addValue(34000 , "月份" , "8月");
- dataset.addValue(18000 , "月份" , "9月");
- dataset.addValue(26000 , "月份" , "10月");
- dataset.addValue(45000 , "月份" , "11月");
- dataset.addValue(38000 , "月份" , "12月");
- //创建一个柱状图
- JFreeChart chart = ChartFactory.createBarChart3D("年度人口统计图", "人口", "数量",dataset,PlotOrientation.VERTICAL, true, false, false);
- chart.setTitle(new TextTitle("年度人口统计图", new Font("黑体", Font.ITALIC,22)));
- LegendTitle legend = chart.getLegend(0);
- legend.setItemFont(new Font("宋体", Font.BOLD, 14));
- CategoryPlot plot = (CategoryPlot) chart.getPlot();
- CategoryAxis categoryAxis = plot.getDomainAxis();
- categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));
- categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
- categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18));
- NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
- numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));
- HttpServletResponse response=ServletActionContext.getResponse();
- response.setContentType("image/");
- ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1, chart, 900, 600, null);
- response.getOutputStream().close();
- return null;
- }
public String execute() throws Exception{
//添加数据
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(42000, "月份" , "1月");
dataset.addValue(40000 , "月份" , "2月");
dataset.addValue(34000 , "月份" , "3月");
dataset.addValue(18000 , "月份" , "4月");
dataset.addValue(26000 , "月份" , "5月");
dataset.addValue(42000 , "月份" , "6月");
dataset.addValue(40000 , "月份" , "7月");
dataset.addValue(34000 , "月份" , "8月");
dataset.addValue(18000 , "月份" , "9月");
dataset.addValue(26000 , "月份" , "10月");
dataset.addValue(45000 , "月份" , "11月");
dataset.addValue(38000 , "月份" , "12月");//创建一个柱状图
JFreeChart chart = ChartFactory.createBarChart3D("年度人口统计图", "人口", "数量",dataset,PlotOrientation.VERTICAL, true, false, false);
chart.setTitle(new TextTitle("年度人口统计图", new Font("黑体", Font.ITALIC,22)));
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18));
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22)); HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("image/");
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1, chart, 900, 600, null);
response.getOutputStream().close(); return null;
}
</pre>3.struts.xml配置<p></p>
- <action name="chart2" class="Char2tAction">
- <result name="success">/Chart.jsp</result>
- </action>
<action name="chart2" class="Char2tAction">
<result name="success">/Chart.jsp</result>
</action>
Tip:参考的别的文章包一定得继承两个类,或者修改源码,实践正确配置如下:<!-- 柱状图 -->
<package name="struts2" extends="struts-default,jfreechart-default">
<action name="chart2" class="com.ssh.app.web.action.CreateChartBar">
<result name="success">/tLiveStatistics.jsp</result>
</action>
</package>4.效果图
二.生成图片保存到本地:
1.生成图片
- public void createChart() {
- // TODO Auto-generated method stub
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- dataset.addValue(1001, "1", "A");
- dataset.addValue(800, "2", "A");
- dataset.addValue(150, "3", "A");
- dataset.addValue(150, "1", "B");
- dataset.addValue(150, "2", "B");
- dataset.addValue(250, "3", "B");
- dataset.addValue(150, "1", "C");
- dataset.addValue(33, "2", "C");
- dataset.addValue(900, "3", "C");
- // JFreeChart chart = ChartFactory.createBarChart3D(
- // "", // 图表标题
- // "", // 目录轴的显示标签
- // "产量", // 数值轴的显示标签
- // dataset, // 数据集
- // PlotOrientation.VERTICAL , // 图表方向:垂直
- // true, // 是否显示图例(对于简单的柱状图必须是false)
- // false, // 是否生成工具
- // false // 是否生成URL链接
- // );
- //JFreeChart chart = ChartFactory.createBarChart3D("","","产量",dataset,PlotOrientation.VERTICAL,true,false,false);
- JFreeChart chart = ChartFactory.createBarChart3D("销量统计图", "种类", "产量", dataset, PlotOrientation.VERTICAL, true, false, false);
- //重新设置图标标题,改变字体
- chart.setTitle(new TextTitle("销量统计图", new Font("黑体", Font.ITALIC , 22)));
- //取得统计图标的第一个图例
- LegendTitle legend = chart.getLegend(0);
- //修改图例的字体
- legend.setItemFont(new Font("宋体", Font.BOLD, 14));
- //legend.setItemFont(new Font("宋体", Font.TRUETYPE_FONT, 14));
- CategoryPlot plot = (CategoryPlot)chart.getPlot();
- chart.setBackgroundPaint(ChartColor.WHITE);
- //设置柱状图到图片上端的距离
- ValueAxis rangeAxis = plot.getRangeAxis();
- rangeAxis.setUpperMargin(0.5);
- //取得横轴
- CategoryAxis categoryAxis = plot.getDomainAxis();
- //设置横轴显示标签的字体
- categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
- //分类标签以45度角倾斜
- // categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
- categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
- //取得纵轴
- NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
- //设置纵轴显示标签的字体
- numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
- //在柱体的上面显示数据
- BarRenderer custombarrenderer3d = new BarRenderer();
- custombarrenderer3d.setBaseItemLabelPaint(Color.BLACK);//数据字体的颜色
- custombarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
- custombarrenderer3d.setBaseItemLabelsVisible(true);
- plot.setRenderer(custombarrenderer3d);
- //HttpServletRequest request = null;
- //request.getSession().getServletContext().getRealPath("/");
- //URL a = Thread.currentThread().getContextClassLoader().getResource("");
- //URL a = Thread.currentThread().getContextClassLoader().getResource("");
- //System.out.println(a);
- FileOutputStream fos = null;
- //String imagePath = "D:\\chart.jpg";
- String imagePath = "D:\\temp\\chart.jpg";
- ChartRenderingInfo info = new ChartRenderingInfo();
- try {
- fos = new FileOutputStream(imagePath);
- //将统计图标输出成JPG文件
- ChartUtilities.writeChartAsJPEG(
- fos, //输出到哪个输出流
- 1, //JPEG图片的质量,0~1之间
- chart, //统计图标对象
- 800, //宽
- 600,//高
- info //ChartRenderingInfo 信息
- );
- fos.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
public void createChart() {
// TODO Auto-generated method stub
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1001, "1", "A");
dataset.addValue(800, "2", "A");
dataset.addValue(150, "3", "A");
dataset.addValue(150, "1", "B");
dataset.addValue(150, "2", "B");
dataset.addValue(250, "3", "B");
dataset.addValue(150, "1", "C");
dataset.addValue(33, "2", "C");
dataset.addValue(900, "3", "C");
// JFreeChart chart = ChartFactory.createBarChart3D(
// "", // 图表标题
// "", // 目录轴的显示标签
// "产量", // 数值轴的显示标签
// dataset, // 数据集
// PlotOrientation.VERTICAL , // 图表方向:垂直
// true, // 是否显示图例(对于简单的柱状图必须是false)
// false, // 是否生成工具
// false // 是否生成URL链接
// );
//JFreeChart chart = ChartFactory.createBarChart3D("","","产量",dataset,PlotOrientation.VERTICAL,true,false,false);
JFreeChart chart = ChartFactory.createBarChart3D("销量统计图", "种类", "产量", dataset, PlotOrientation.VERTICAL, true, false, false);//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("销量统计图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//legend.setItemFont(new Font("宋体", Font.TRUETYPE_FONT, 14));
CategoryPlot plot = (CategoryPlot)chart.getPlot(); chart.setBackgroundPaint(ChartColor.WHITE); //设置柱状图到图片上端的距离
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setUpperMargin(0.5); //取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
//分类标签以45度角倾斜
// categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22)); //在柱体的上面显示数据
BarRenderer custombarrenderer3d = new BarRenderer();
custombarrenderer3d.setBaseItemLabelPaint(Color.BLACK);//数据字体的颜色
custombarrenderer3d.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
custombarrenderer3d.setBaseItemLabelsVisible(true);
plot.setRenderer(custombarrenderer3d);
//HttpServletRequest request = null;
//request.getSession().getServletContext().getRealPath("/");
//URL a = Thread.currentThread().getContextClassLoader().getResource("");
//URL a = Thread.currentThread().getContextClassLoader().getResource("");
//System.out.println(a);
FileOutputStream fos = null;
//String imagePath = "D:\\chart.jpg";
String imagePath = "D:\\temp\\chart.jpg";
ChartRenderingInfo info = new ChartRenderingInfo();
try {
fos = new FileOutputStream(imagePath);
//将统计图标输出成JPG文件
ChartUtilities.writeChartAsJPEG(
fos, //输出到哪个输出流
1, //JPEG图片的质量,0~1之间
chart, //统计图标对象
800, //宽
600,//高
info //ChartRenderingInfo 信息
);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
} }</pre><br>
2.显示到jsp:
- try{
- String impath = "D:\\temp\\chart.jpg";//此处的图路径,是上面所示代码中生成柱状图的存储路径
- File file = new File(impath);
- InputStream inputStream = null;
- if(file.exists()){
- try {
- inputStream = new FileInputStream(file);
- } catch (FileNotFoundException e) {
- }
- }
- int i = inputStream.available();
- byte data[] = new byte[i];
- inputStream.read(data);
- HttpServletResponse response = ServletActionContext.getResponse();
- ServletOutputStream out = response.getOutputStream();
- response.setContentType("image/jpeg");
- out.write(data);
- out.flush();
- out.close();
- }catch(IOException e){
- e.printStackTrace();
- }
try{String impath = "D:\\temp\\chart.jpg";//此处的图路径,是上面所示代码中生成柱状图的存储路径
File file = new File(impath); InputStream inputStream = null;
if(file.exists()){
try {
inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { }
} int i = inputStream.available();
byte data[] = new byte[i];
inputStream.read(data); HttpServletResponse response = ServletActionContext.getResponse();
ServletOutputStream out = response.getOutputStream(); response.setContentType("image/jpeg"); out.write(data);
out.flush();
out.close(); }catch(IOException e){
e.printStackTrace();
}</pre><br>
SSH框架通过JFreeChart实现柱状图和获取项目路径的更多相关文章
- springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息
1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...
- katalon系列十四:执行Windows命令&获取项目路径
Katalon Studio中也可以运行Windows命令执行一些系统操作. 根据官方文档,在test case中输入命令:cmd = 'del E:\\shot\\*.xlsx E:\\shot\\ ...
- 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?
这里的疑问在于pageContext和request都是JSP中的内置对象之一,为什么不直接用${request.contextPath}来获取项目路径? 出现这种疑问,其实是将JSP的内置对象和EL ...
- 项目随笔之springmvc中freemark如何获取项目路径
转载:http://blog.csdn.net/whatlookingfor/article/details/51538995 在SpringMVC框架中使用Freemarker试图时,要获取根路径的 ...
- java获取项目路径,url路径
我的web项目名iamgeModel. 工作空间在D盘 先获取url相关: 需要是HttpServletRequest request; 获取IP: request.getServerName() / ...
- java web项目获取项目路径
注意:有时获取到的项目路径后再+“自定义路径后” 路径不可用,这时要看下项目里自定义路径是不是空文件夹,如果是空文件夹则调试和运行时文件夹不会编译到部署文件里. 1.方法一 调试时只能获取eclips ...
- js中获取项目路径的小插件
//立即执行的js (function() { //获取contextPath var contextPath = getContextPath(); //获取basePath var basePat ...
- java中几种获取项目路径方式
转自http://caodaoxi.iteye.com/blog/1234805 在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录 ...
- JAVA类中获取项目路径
在java web项目中获取项目的src/main/resource下的文件路径 当前类名.class.getClassLoader().getResource("/").getP ...
随机推荐
- Frameset框架集的应用
Frameset框架集常用于写网站后台页面,大多数"T字型"布局后台页面,就是应用Frameset框架集来做的.Franeset框架集的优点是,他可以在同浏览器窗口显示不同页面内容 ...
- SPFA 算法详解
适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便 派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径 ...
- java中变量赋值的理解
1.当赋值的值超出声明变量的范围时候,会报错! byte a =200 //会报错,因超出范围. byte a =(byte)200;//进行一个强制转换,就不会报错,不过会超出范围,超出部分会从头开 ...
- select2 4.0.3 空记录时的处理
使用select2插件,如果后台查找没有记录,不能返回null,要返回一个空数组,不然会报错:data is null 空数组形式为(firefox调试输出):{"items":[ ...
- 3,Spring Boot热部署
问题的提出: 在编写代码的时候,你会发现我们只是简单把打印信息改变了,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来就真的是打几个Hello World就下班了.那么如何解决热部署的问题呢 ...
- 1,入门-Hello Soring Boot
什么是SpringBoot Spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目.大多数SpringBoot项目只需要很少的配置文件. SpringBoo ...
- rsync安装及其配置
服务端配置安装 服务器 第一步: 下载rsync 安装包(在线安装或者线下安装) wget https://download.samba.org/pub/rsync/rsync-3.1 ...
- AJAX 中JSON 和JSONP 的区别 以及请求原理
AJAX 跨域请求 - JSONP获取JSON数据 博客分类: Javascript /Jquery / Bootstrap / Web Asynchronous JavaScript and X ...
- (转)fiddler实现手机抓包的基础设置问题
电脑最好是笔记本,这样能和手机保持统一局域网内:其他不多说,直接说步骤了. 一.对PC(笔记本)参数进行配置 1. 配置fiddler允许监听到https(fiddler默认只抓取http格式的 ...
- POJ 2386 Lake Counting (简单深搜)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...