标签:
1.action类,使用注解配置的:
package com.t5.manage.action;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.t5.entity.Statistics;
import com.t5.entity.User;
import com.t5.manage.biz.ManagerLoginBiz;
import com.t5.manage.biz.ManagerOrderBiz;
@Controller
public class Jfreechart {
@Autowired
public ManagerOrderBiz managerOrderBiz;
@Resource
public ManagerLoginBiz manageUserService;
@RequestMapping(value = "/getMajorChart")
public ModelAndView getMajorChart(HttpServletRequest request,
HttpServletResponse response, ModelMap modelMap,String time) throws Exception{
CategoryDataset dataset = getDataSet2(time);//time参数
JFreeChart chart = ChartFactory.createBarChart3D("全年销售报表", // 图表标题
"类别", // 目录轴的显示标签
"销售额", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
//解决乱码问题
getChartByFont(chart);
String fileName = ServletUtilities.saveChartAsJPEG(chart, 700,500, null, request.getSession());
String chartURL=request.getContextPath() + "/chart?filename="+fileName;
modelMap.put("chartURL", chartURL);
return new ModelAndView("test",modelMap);
}
private void getChartByFont(JFreeChart chart) {
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体",Font.BOLD,20));
LegendTitle legend = chart.getLegend();
if (legend!=null) {
legend.setItemFont(new Font("宋体", Font.BOLD, 20));
}
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis axis = plot.getDomainAxis();
//设置X轴坐标上标题的文字
axis.setLabelFont(new Font("宋体",Font.BOLD,22));
//设置X轴坐标上的文字,
axis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
ValueAxis valueAxis = plot.getRangeAxis();
//设置Y轴坐标上标题的文字
valueAxis.setLabelFont(new Font("宋体",Font.BOLD,12));
//设置Y轴坐标上的文字
valueAxis.setTickLabelFont(new Font("sans-serif",Font.BOLD,12));
}
/**
* 获取一个演示用的组合数据集对象
*
* @return
* time 是查询参数
*/
private CategoryDataset getDataSet2(String time) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
List<Map<String, Object>> list=managerOrderBiz.getOrderList(time);
List<Statistics> liststa=new ArrayList<Statistics>();
System.out.println("集合数"+list.size());
for (Map<String, Object> map : list)
{
String typeName=(String) map.get("typeName").toString();//商品类型
String year=(String) map.get("year").toString(); //年
String month=(String) map.get("month").toString(); //月
Integer shoppingId=Integer.valueOf( map.get("shoppingId").toString());
Integer userId=Integer.valueOf( map.get("userId").toString());
Integer orderTypeId=Integer.valueOf((String)map.get("orderTypeId").toString());
double moneyTotal=Double.valueOf( map.get("moneyTotal").toString());
Statistics sta=new Statistics();
sta.setTypeName(typeName);
sta.setYear(year);
sta.setMonth(month);
sta.setMoneyTotal(moneyTotal);//总金额
liststa.add(sta);
}
for(Statistics tics:liststa){
dataset.addValue(tics.getMoneyTotal(), tics.getTypeName(),tics.getMonth());
}
return dataset;
}
}
2.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>jfreechart 演示 </title>
</head>
<body>
jfreechart 演示 <br>
请访问<a href="getMajorChart.do">fffff</a>
<br>
<img src="${chartURL}">
</body>
</html>
spring mvc jfreechar注解配置及数据库访问
标签:
原文地址:http://www.cnblogs.com/AALL/p/5313035.html