- 浏览: 203491 次
- 性别:
- 来自: 芜湖
文章分类
- 全部博客 (139)
- 软件 (0)
- Pattern (6)
- CSDN导入 (19)
- Struts (3)
- [网站分类]1.网站首页原创 (27)
- [网站分类]6.转载区 (4)
- Hibernate (10)
- Error (8)
- [网站分类]2.Java新手区 (20)
- Java (8)
- [网站分类]4.其他技术区 (10)
- Web (1)
- C++ (2)
- Algorithm (4)
- Linux (2)
- Skill (1)
- Tech (2)
- Note (2)
- [网站分类]3.非技术区 (1)
- Database (1)
- Winty (7)
- [网站分类]1.网站首页原创Java技术区(对首页文章的要求: 原创、高质量、经过认真思考并精心写作。BlogJava管理团队会对首页的文章进行管理。) (0)
最新评论
-
haohao-xuexi02:
很不错哦。
O'Reilly cos上传组件的使用(1/3) - 上传文件 -
yoin528:
useUnicode=true&charact ...
[原]向MySQL数据库插入Blob数据的问题 -
xiaoqing20:
下载来看看!呵呵
[原]Struts2类型转换 -
xiaoqing20:
[原]Struts2类型转换
[标题]:[原]Struts2-整合JFreeChart
[时间]:2009-9-3
[摘要]:JFreeChart
[关键字]:浪曦视频,Struts2应用开发系列,WebWork,Apache
[环境]:struts-2.1.6、JDK6、MyEclipse7、Tomcat6
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[正文]:
1、JFreeChart入门
JFreeChart网站:www.jfree.org。需要下载jfreechart和jcommon(JFreeChart依赖于jcommon)。JCommon的文档和源代码需要单独下载。使用时将jfreechart.jar和jcommon.jar加到构建路径中。
a. 饼图(Swing)
/StrutsHelloWorld/src/wintys/jfreechart/PieChart.java:
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
/**
* 饼状图
* @author Winty (wintys@gmail.com)
* @version 2009-8-31
* @see http://wintys.blogjava.net
*/
public class PieChart {
public static void main(String[] args) {
ChartFrame frame = new ChartFrame("My JFreeChart Demo",getChart());
frame.setVisible(true);
frame.setSize(600 , 400);
}
public static JFreeChart getChart(){
DefaultPieDataset dpd = new DefaultPieDataset();
dpd.setValue("计算机", 200);
dpd.setValue("电子", 150);
dpd.setValue("经管", 50);
dpd.setValue("外语", 198);
JFreeChart chart
// = ChartFactory.createPieChart("院系人数", dpd, true, true,false);
= ChartFactory.createPieChart3D("院系人数", dpd, true, true,true);
//需要设置字体,否则中文无法显示
Font font = new Font("宋体" , Font.PLAIN , 20);
chart.getTitle().setFont(font);
chart.getLegend().setItemFont(font);
PiePlot piePlot = (PiePlot)chart.getPlot();
piePlot.setLabelFont(font);
return chart;
}
}
b. 柱状图(Swing)
/StrutsHelloWorld/src/wintys/jfreechart/BarChart.java:
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
/**
* 柱状图
*
* @author Winty (wintys@gmail.com)
* @version 2009-8-31
* @see http://wintys.blogjava.net
*/
@SuppressWarnings("serial")
public class BarChart extends ApplicationFrame {
public BarChart(String title) {
super(title);
setContentPane(createPanel());
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new BarChart("水果产量图");
}
private static CategoryDataset createDataset() {
DefaultCategoryDataset dcd = new DefaultCategoryDataset();
dcd.addValue(100, "北京", "苹果");
dcd.addValue(70, "上海", "苹果");
dcd.addValue(120, "广州", "苹果");
dcd.addValue(220, "北京", "梨子");
dcd.addValue(260, "上海", "梨子");
dcd.addValue(300, "广州", "梨子");
dcd.addValue(320, "北京", "葡萄");
dcd.addValue(370, "上海", "葡萄");
dcd.addValue(320, "广州", "葡萄");
dcd.addValue(490, "北京", "香蕉");
dcd.addValue(450, "上海", "香蕉");
dcd.addValue(400, "广州", "香蕉");
dcd.addValue(200, "北京", "荔枝");
dcd.addValue(230, "上海", "荔枝");
dcd.addValue(340, "广州", "荔枝");
return dcd;
}
/**
* default getChart() method using default dataset
* @return
*/
public static JFreeChart getChart() {
return getChart(createDataset());
}
public static JFreeChart getChart(CategoryDataset cd){
JFreeChart chart = ChartFactory.createBarChart3D("", "水果", "产量", cd,
PlotOrientation.VERTICAL, true, true, false);
/*不设置字体,汉字会乱码*/
Font font = new Font("宋体", Font.BOLD, 20);
chart.setTitle(new TextTitle("水果产量图", font));
chart.getTitle().setFont(font);
chart.getLegend().setItemFont(font);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(font);
categoryAxis.setTickLabelFont(font);
ValueAxis valueAxis = plot.getRangeAxis();
valueAxis.setLabelFont(font);
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
return chart;
}
private JPanel createPanel() {
return new ChartPanel(getChart());
}
}
c. 输出图片
/StrutsHelloWorld/src/wintys/jfreechart/OutputChart.java:
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
/**
* 将JFreeChart图片输出到文件
* @author Winty (wintys@gmail.com)
* @version 2009-9-1
* @see http://wintys.blogjava.net
*/
public class OutputChart {
public static void main(String[] args) {
File file = new File("D:/temp.jpg");
JFreeChart chart = BarChart.getChart();
final int width = 400;
final int height = 400;
try {
ChartUtilities.saveChartAsJPEG(file, chart, width, height);
} catch (IOException e) {
e.printStackTrace();
}
}
}
d. 在Servlet中使用JFreeChart
ServletUtilities.saveChartAsPNG();
//生成文件在Tomcat temp下
/StrutsHelloWorld/src/wintys/jfreechart/DisplayChartServlet.java:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
@SuppressWarnings("serial")
public class DisplayChartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
JFreeChart chart;
int width;
int height;
String type = request.getParameter("type");
if ("pie".equalsIgnoreCase(type)) {
chart = PieChart.getChart();// pie
width = 600;
height = 400;
} else {
chart = BarChart.getChart();// bar
width = 800;
height = 600;
}
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
width, height);
/*
//存为临时图片,然后输出
String image = ServletUtilities.saveChartAsPNG(chart,600, 400, request.getSession());
ServletUtilities.sendTempFile(image, response);
System.out.println("Image:" + image);
System.out.println(System.getProperty("java.io.tmpdir"));
*/
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
web.xml中的配置:
<servlet-name>DisplayChartServlet</servlet-name>
<servlet-class>wintys.jfreechart.DisplayChartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChartServlet</servlet-name>
<url-pattern>/jfreechart/DisplayChartServlet</url-pattern>
</servlet-mapping>
/StrutsHelloWorld/WebRoot/jfreechart/JFreeChartDisplayChart.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
这是JFreeChart生成的柱状图:
<br />
<img src="/StrutsHelloWorld/jfreechart/DisplayChartServlet?type=bar" />
<hr/>
<br />
这是饼图:<br/>
<img src="/StrutsHelloWorld/jfreechart/DisplayChartServlet?type=pie" />
</body>
</html>
e. JFreeChart自带的显示图形的Servlet:DisplayChart
org.jfree.chart.servlet.DisplayChart 与 以上编写的wintys.jfreechart.DisplayChartServlet原理相似。
在web.xml中的配置:
<servlet-name>JFreeChartDisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JFreeChartDisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
/StrutsHelloWorld/WebRoot/jfreechart/JFreeChartDisplayChart.jsp:
<%@ page import = "wintys.jfreechart.BarChart" %>
<%@ page import = "org.jfree.chart.JFreeChart" %>
<%@ page import = "org.jfree.chart.servlet.ServletUtilities" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
org.jfree.chart.servlet.DisplayChart使用:
<br />
<%
JFreeChart chart = BarChart.getChart();
String filename = ServletUtilities.saveChartAsPNG (chart , 800 , 600 , session);
String url = request.getContextPath() ;
String imageurl = url + "/servlet/DisplayChart?filename=" + filename;
%>
<img src="<%=imageurl%>" />
</body>
</html>
2、Struts2整合JFreeChart
在Struts2中整合JFreeChart需要把struts中的JFreeChart Plugin.jar复制到构建路径中。其实JFreeChart Plugin只是提供了一个org.apache.struts2.dispatcher.ChartResult,用于在Struts中方便地显示图形。
/StrutsHelloWorld/WebRoot/jfreechart/StrutsJFreeChart.jsp:
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<s:form action="jfreechart" namespace="/mystruts">
<s:checkbox name="interests" fieldValue="football" label="足球" labelposition="left" />
<s:checkbox name="interests" fieldValue="basketball" label="篮球"/>
<s:checkbox name="interests" fieldValue="baseball" label="棒球" />
<s:submit name="submit" value="提交"></s:submit>
</s:form>
<img alt="" src="/StrutsHelloWorld/mystruts/jfreechart.action" />
</body>
</html>
注意:
a). <s:checkbox />中应该使用fieldValue属性而不是value作为值。
b). 还可应用如下方法添加checkbox: <s:checkboxlist list="#{'value' : 'label' , "math":"数学"} "/>
struts.xml配置:
<struts>
......
<include file="struts-myjfreechart.xml" />
......
</struts>
struts-myjfreechart.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="MyStrutsJFreeChart" extends="jfreechart-default " namespace="/mystruts">
<action name="jfreechart" class="wintys.jfreechart.ChartAction">
<result name="success" type="chart ">
<param name="width">800</param>
<param name="height">600</param>
</result>
</action>
</package>
</struts>
/StrutsHelloWorld/src/wintys/jfreechart/ChartAction.java:
import java.awt.Font;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* JFreeChart Struts Plugin
* @author Winty (wintys@gmail.com)
* @version 2009-9-2
* @see http://wintys.blogjava.net
*/
@SuppressWarnings("serial")
public class ChartAction extends ActionSupport {
private List<String> interests;
public List<String> getInterests() {
return interests;
}
public void setInterests(List<String> interests) {
this.interests = interests;
}
/**
* 记录interest信息
*/
private void addInterests(){
ActionContext context = ActionContext.getContext();
Map<String , Object> application = context.getApplication();
if(interests == null)return;
Iterator<String> it = interests.iterator();
while(it.hasNext()){
String interest = it.next();
addValue(application , interest);
}
}
/**
* 在application中保存数据
*/
private void addValue(Map<String , Object> application , String key){
if(key == null)
return;
int value = getValue(application , key) + 1;
application.put(key, value);
}
/**
* 从application中获取数据。如果key不存在,则返回0。
*/
private int getValue(Map<String , Object> application , String key){
int value = 0;
Object obj = application.get(key);
if( obj != null){
value = ((Integer)obj).intValue();
}
return value;
}
/**
* for JFreeChart
* @return org.jfree.chart.JFreeChart
*/
public JFreeChart getChart(){
JFreeChart chart = getChart(createDataset());
return chart;
}
private CategoryDataset createDataset(){
ActionContext context = ActionContext.getContext();
Map<String , Object> application = context.getApplication();
DefaultCategoryDataset dcd = new DefaultCategoryDataset();
dcd.addValue(getValue(application , "football"), "" , "足球");
dcd.addValue(getValue(application , "basketball"), "" , "篮球");
dcd.addValue(getValue(application , "baseball"), "" , "棒球");
return dcd;
}
private static JFreeChart getChart(CategoryDataset cd){
JFreeChart chart = ChartFactory.createBarChart3D("", "球类", "兴趣", cd,
PlotOrientation.VERTICAL, true, true, false);
/*不设置字体,汉字会乱码*/
Font font = new Font("宋体", Font.BOLD, 20);
chart.setTitle(new TextTitle("兴趣统计图", font));
chart.getTitle().setFont(font);
chart.getLegend().setItemFont(font);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(font);
categoryAxis.setTickLabelFont(font);
ValueAxis valueAxis = plot.getRangeAxis();
valueAxis.setLabelFont(font);
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
return chart;
}
@Override
public String execute() throws Exception {
addInterests();//统计interest数据
return SUCCESS;
}
}
结果:
[参考资料]:
[1]《浪曦视频之Struts2应用开发系列》
[2] struts2整合jfreechart : http://zyongking.iteye.com/blog/443980
[3] Struts JFreeChart Plugin : http://struts.apache.org/2.x/docs/jfreechart-plugin.html
[附件]:
源代码 :
StrutsHelloWorld_jfreechart.zip
发表评论
-
[原]MyEclipse location already exists
2009-07-27 00:37 1745[标题]:[原]MyEclipse location alre ... -
[原]Exception starting filter MyStruts2
2009-07-27 19:41 1067[标题]:[原]Exception starting filt ... -
[原]Struts2校验框架
2009-07-27 23:54 1139[标题]:[原]Struts2校验框架 [时间]:2009- ... -
[原]Struts2-拦截器
2009-08-01 00:21 1170[标题]:[原]Struts2-拦截器 [时间]:2009- ... -
[原]Struts2-文件上传与下载
2009-08-08 08:48 2206[标题]:[原]Struts2-文件上 ... -
[原]Struts2-国际化
2009-08-10 18:13 1913[标题]:[原]Struts2-国际化 [时间]:2009- ... -
[原]Struts2-深入探索
2009-08-29 20:32 1083[标题]:[原]Struts2-深入探索 [时间]:2009 ... -
[转]Java 小例子:数据持久化(保存数据到文件)
2009-03-16 11:00 1783说数据持久化,初学者可能还不太懂;但要说把数据保存到文件,这谁 ... -
[原]调试EL函数时,Tomcat提示NullPointerException
2009-04-03 19:02 943[标题]:调试EL函数时,Tomcat提示NullPointe ... -
[原]HelloWorldServlet
2009-05-27 22:16 949编译运行步骤: 1、将HelloWorldServlet.ja ... -
[原]getServletInfo()
2009-05-27 23:16 1405《JSP完全学习手册》学习心得之:getServletInfo ... -
[原]jsp:forward
2009-05-27 23:24 934《JSP完全学习手册》学习心得:jsp:forward Jud ... -
[原]jsp:param
2009-05-27 23:27 1620《JSP完全学习手册》学习心得:jsp:param param ... -
[原]request.getParameterValues()
2009-05-27 23:32 1243《JSP完全学习手册》学习心得:request.getPara ... -
[原]隐含对象config
2009-05-27 23:54 770《JSP完全学习手册》学习心得:隐含对象config < ... -
[原]cookie读写
2009-05-28 00:03 869《JSP完全学习手册》学习心得:cookie读写 <%@ ... -
[原]getHttpHeader
2009-05-28 00:14 860《JSP完全学习手册》学习心得:getHttpHeader g ... -
[原]向MySQL数据库插入Blob数据的问题
2009-06-03 23:45 3999[标题]:向MySQL数据库插 ... -
[原]SQL内外连接
2009-06-17 22:31 930-- Author:Winty(天堂露珠) -- Date: ...
相关推荐
struts2整合jfreechart 的工具jar文件 struts2-jfreechart-plugin-2.3.15.3.jar
"jfreechart_struts2" 和 "jfreechart-2.jar" 是标签,它们强调了这个插件是关于Struts2与JFreeChart的集成,同时提到了JFreeChart的版本可能是2.x系列。这意味着你需要确保你的项目中已经包含了正确的JFreeChart...
struts2整合JFreechart: jcommon-1.0.16.jar jfreechart-1.0.13.jar struts2-jfreechart-plugin-2.0.11.jar 导成图片操作说明
`struts-2.3.20-jfreechart插件`是Struts 2框架的一个扩展,它整合了JFreeChart的功能,使得在Struts 2应用中生成动态图表变得简单易行。 首先,让我们深入了解Struts 2框架的核心特性: 1. **Action类**:在...
首先,整合Struts2和JFreeChart的首要步骤是确保在项目中引入JFreeChart和相关依赖。你需要在你的Maven或Gradle配置文件中添加JFreeChart的依赖,通常为以下形式: ```xml <groupId>org.jfree <artifactId>...
在将Struts2与JFreeChart整合的过程中,主要涉及以下几个关键知识点: 1. **Struts2架构理解**:Struts2框架提供了Action、Interceptor(拦截器)、Result和ValueStack等核心概念,帮助开发者实现业务逻辑和视图的...
修改 `Struts2-jfreechart-plugin-2.0.14.jar` 中的 `struts-plugin.xml` 文件,添加一个新的结果类型 `chart`,指定其类为 `org.apache.struts2.dispatcher.ChartResult`,并设置高度和宽度参数。 4. **Struts....
### Struts2整合Jfreechart实现图表展示案例分析 #### 一、背景介绍 在Web应用开发中,数据可视化是一个非常重要的环节。Struts2作为一款流行的企业级Java Web框架,能够很好地与其他技术栈集成,比如JFreeChart,...
3. **struts2-jfreechart-plugin**:这是一个Struts2的插件,专门用于整合JFreeChart。它提供了一组Action和Result类型,使得在Struts2应用中生成和显示JFreeChart图表变得非常便捷。开发者可以通过配置Struts2的XML...
Struts2整合JFreeChart使得在Web应用中展示数据图表变得更加简单。通过添加必要的库和配置,以及编写Action和数据服务,开发者可以方便地生成各种图表,并在页面上呈现。这种方式有助于提升用户对复杂数据的理解,...
2. **Struts2整合JFreeChart**: - 首先,我们需要在项目中引入JFreeChart和Struts2的相关依赖库,如struts2-core、jfreechart以及jcommon库。 - 创建一个Action类,其中的方法负责生成图表数据和JFreeChart对象。...
总的来说,Struts2与JFreeChart的整合是Java Web开发中常见的需求,它可以帮助我们创建交互式的、数据驱动的图表应用。通过理解JFreeChart的图表生成原理,掌握Struts2的Action和结果类型,以及熟悉Web应用的图像...
整合Struts2和JFreeChart,主要是为了在web应用中展示动态生成的数据图表,例如报表。以下是整合的步骤和关键知识点: 1. **下载和引入JFreeChart库**: 首先,你需要从JFreeChart的官方网站...
Struts2应用开发系列Struts2整合JFreeChart
Struts2应用开发系列Struts2整合JFreeChart
Struts2应用开发系列Struts2整合JFreeChart
Struts2应用开发系列Struts2整合JFreeChart
Struts2应用开发系列Struts2整合JFreeChart
Struts2应用开发系列Struts2整合JFreeChart