欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-09 17:21:09
...

图示:
Spring+Struts+JFreeChart 自动生成统计图
            
    
    博客分类: Spring
 

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>

 

效果图:
Spring+Struts+JFreeChart 自动生成统计图
            
    
    博客分类: Spring
 

 

 

 

  • Spring+Struts+JFreeChart 自动生成统计图
            
    
    博客分类: Spring
  • 大小: 52.8 KB
  • Spring+Struts+JFreeChart 自动生成统计图
            
    
    博客分类: Spring
  • 大小: 41.6 KB