javeee Jfreechart做报表

1 篇文章 0 订阅
1 篇文章 0 订阅
 

最近需要做一个报表,在网上没有找到一个合适的,索性自己写了一份,以下是servlet的代码,注意配置一下:

package com.tour.util;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
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.NumberAxis;
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.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

import com.tour.bean.admin.OrderSum;

public class ChartDemoServlet extends HttpServlet {


	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	
			req.setCharacterEncoding("GBK");
			resp.setCharacterEncoding("GBK");
		    resp.setContentType("image/jpeg");
		
		   // DefaultPieDataset data = 
		   DefaultCategoryDataset data = getDataSet();
		   DefaultCategoryDataset dataset=data;
			JFreeChart chart=ChartFactory.createBarChart3D(ListOrder.getTitle(), "日期", ListOrder.getY(), dataset, PlotOrientation.VERTICAL, true, false, false);
			
			CategoryPlot   plot   =   chart.getCategoryPlot();  
			   
			  CategoryAxis   domainAxis   =   plot.getDomainAxis();  
			  //domainAxis.setVerticalCategoryLabels(false);  
			  plot.setDomainAxis(domainAxis);  
			  //设置中文显示
			  NumberAxis numberaxis = (NumberAxis)plot.getRangeAxis();
			  TextTitle textTitle = chart.getTitle();
			  textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
			  domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
			  domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
			  numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
			  numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
			  chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
			  
			  ValueAxis   rangeAxis   =   plot.getRangeAxis();  
			  // 设置最高的一个   Item   与图片顶端的距离  
			  rangeAxis.setUpperMargin(0.15);  
			  // 设置最低的一个   Item   与图片底端的距离  
			  rangeAxis.setLowerMargin(0.30);  
			  plot.setRangeAxis(rangeAxis);  
			   
			  BarRenderer3D   renderer   =   new   BarRenderer3D();  
			  renderer.setBaseOutlinePaint(Color.BLACK);  
			  // 设置   Wall   的颜色  
			  renderer.setWallPaint(Color.gray);  
			  // 设置每个柱子颜色 
			  renderer.setSeriesPaint(0,   new   Color(0,   0,   255));  
			  renderer.setSeriesPaint(1,   new   Color(0,   100,   255));  
			 // renderer.setSeriesPaint(2,   Color.GREEN);  
			  // 设置柱间距			  renderer.setItemMargin(0.1);  
			  // 显示每个柱的数值,并修改该数值的字体属性  
			           //                  renderer.setLabelGenerator(new   StandardCategoryLabelGenerator());  
			  renderer.setItemLabelsVisible(true);  
			  plot.setRenderer(renderer);  
			
	          
	          //显示每个柱的数值,并修改该数值的字体属性  
	          renderer.setBaseItemLabelGenerator(new   StandardCategoryItemLabelGenerator());  
	         // renderer.setItemLabelFont(new   Font("黑体",   Font.PLAIN,   12));  
	          renderer.setItemLabelsVisible(true);  
	          plot.setRenderer(renderer);
		    
	          
		    ChartUtilities.writeChartAsJPEG(resp.getOutputStream(),
		
		              1.0f,chart,600,400,null);
		
		}
		
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	


	private static DefaultCategoryDataset getDataSet() {
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		List<OrderSum> os = ListOrder.getList();
	   //   Map map = StateReportManger.getIntense().getClientLevelCount(); //调用getClientLevelCount()得到所有的数据
		    
		 for(int i = 0; i<os.size();i++){//遍历,用到Map的. entrySet()方法
		
		     dataset.addValue( Double.parseDouble(String.valueOf(os.get(i).getSum()+1)),os.get(i).getDate(),"");//设置数据
		
		 }
		  
	  return dataset;
		
		}

}


这个是显示报表用的,至于数据的赋值是有单独的一个bean进行服务的:


package com.tour.util;

import java.util.ArrayList;
import java.util.List;

import com.tour.bean.admin.OrderSum;

public class ListOrder {
	
	private static String title;
	private static String y;
	
	public static String getTitle() {
		return title;
	}

	public static String getY() {
		return y;
	}

	public static void setTitle(String title) {
		ListOrder.title = title;
	}

	public static void setY(String y) {
		ListOrder.y = y;
	}

	private static List<OrderSum> list = new ArrayList<OrderSum>();

	public static List<OrderSum> getList() {
		return list;
	}

	public static void setList(List<OrderSum> list) {
		ListOrder.list = list;
	}
	
}

在需要调用数据的servlet,只需为这个bean赋值数据就可以了,里面有dao的操作,我想这个对看这个文章的人来说不难吧,注解,BaseServlet是HttpServlet的几个反射类:

package com.tour.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.JDOMException;

import com.tour.bean.admin.OrderSum;
import com.tour.bean.pub.Orders;
import com.tour.bean.pub.Users;
import com.tour.dao.pub.OrdersDAO;
import com.tour.dao.pub.UsersDAO;
import com.tour.util.BaseServlet;
import com.tour.util.ChartDemoServlet;

import com.tour.util.ListOrder;
import com.tour.util.StringUtil;

public class TongServlet extends BaseServlet {

	
	public void showOrder(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Orders order = new Orders();
		
		//获得数据
		List<OrderSum> list = new OrdersDAO().getSum(order);
		ListOrder.setTitle("订单");
		ListOrder.setY("订单数量");
		ListOrder.setList(list);
		
		request.getRequestDispatcher("/admin/tong/orderTong.jsp").forward(request, response);
	}
	
}
然后就可以直接在jsp可以直接以图片加载的形式加载了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值