`
chaoyi
  • 浏览: 291077 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring+Struts+JFreeChart 自动生成统计图

 
阅读更多

图示:

 

LineChartAction 控制层

package cn.action;
import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LineChartAction extends ActionSupport {
	private JFreeChart chart;
	public String execute() throws Exception{
		String title = "2014年股票K线走势";
		//建立一个默认的种类数据集
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(52.3d, "", "1M");
		dataset.addValue(51.3d, "", "2M");
		dataset.addValue(52.3d, "", "3M");
		dataset.addValue(43.3d, "", "4M");
		dataset.addValue(24.3d, "", "5M");
		dataset.addValue(35.3d, "", "6M");
		dataset.addValue(12.3d, "", "7M");
		dataset.addValue(42.3d, "", "8M");
		dataset.addValue(27.3d, "", "9M");
		dataset.addValue(55.3d, "", "10M");
		dataset.addValue(67.3d, "", "11M");
		dataset.addValue(30.3d, "", "12M");
		chart = ChartFactory.createLineChart(title, "月份", "增长率", 
				dataset, PlotOrientation.VERTICAL, false, true, true);
		//设置标题的字体
		chart.setTitle(new TextTitle(title,new Font("微软雅黑", Font.BOLD, 20)));
		//加入一个子标题
		chart.addSubtitle(new TextTitle("财年分析",new Font("宋体", Font.BOLD, 15)));
		CategoryPlot plot = chart.getCategoryPlot();
		//y 方向显示字体
		ValueAxis y = plot.getRangeAxis();
		y.setTickLabelFont(new Font("Arial", Font.BOLD, 12));
		y.setLabelFont(new Font("宋体", Font.BOLD, 14));
		//x 方向
		CategoryAxis x = plot.getDomainAxis();
		x.setTickLabelFont(new Font("Arial", Font.BOLD, 14));
		x.setLabelFont(new Font("宋体", Font.BOLD, 14));
		//线上显示数字
		LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer)plot.getRenderer();
		lineAndShapeRenderer.setShapesVisible(true);
		lineAndShapeRenderer.setItemLabelFont(new Font("宋体", Font.BOLD, 11));
		lineAndShapeRenderer.setDrawOutlines(true);
		lineAndShapeRenderer.setUseFillPaint(true);
		lineAndShapeRenderer.setFillPaint(Color.GRAY);
		lineAndShapeRenderer.setItemLabelsVisible(true);
		lineAndShapeRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		return SUCCESS;
	}
	public JFreeChart getChart() {
		return chart;
	}
	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}
}

 

web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

 

struts.xml 配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="Fav" extends="struts-default" namespace="/image">
		<!-- 自定义结果类型 -->
		<result-types>
			<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
		</result-types>
		<!-- 相当于 1 张图片 -->
		<action name="pieChart" class="cn.action.LineChartAction">
			<result name="success" type="chart">
				<param name="width">700</param>
				<param name="height">450</param>
			</result>
		</action>
	</package>
</struts>    

 

index.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>饼图显示</title>
</head>
<body>
<img alt="饼图显示" src="image/pieChart.action">
</body>
</html>

 

效果图:

 

 

 

 

  • 大小: 52.8 KB
  • 大小: 41.6 KB
分享到:
评论

相关推荐

    struts2整合JFreeChart

    struts2整合jfreechart实现饼图、柱状图、时间顺序图的例子,包括简要的文档说明和代码!

    struts2结合jfreechart

    资源包里包括:代码例子、jar包、说明文档!100%可用!

    web项目常用jar包及说明.zip

    Struts 2需要的jar包: 1.commons-fileupload.jar(commons项目中的关于文件上传的包, struts2.1.6版本后必须加入此文件) 2.commons-io.jar...统计图(JFreechart两个):jcommon-1.0.10.jar,jfreechart-1.0.6.jar

    Java Web开发实例大全(基础卷) 完整pdf扫描版[179MB]

    重点内容有操作XML文件、发送与接收邮件、数据库操作技术、SQL语句应用技术、复杂查询技术、数据库高级应用、JFreeChart绘图基础、基础图表技术、扩展图表技术、基于Cewolf组件的图表编程、Prototype框架、jQuery...

    Java Web开发实例大全

    重点内容有操作XML文件、发送与接收邮件、数据库操作技术、SQL语句应用技术、复杂查询技术、数据库高级应用、JFreeChart绘图基础、基础图表技术、扩展图表技术、基于Cewolf组件的图表编程、Prototype框架、jQuery...

    java开源包1

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包11

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包2

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包3

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包6

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包5

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包10

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包4

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包8

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包7

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

    java开源包9

    JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...

Global site tag (gtag.js) - Google Analytics