首页 > 代码库 > java三步 在线生成图形报表-maven-jfreechat

java三步 在线生成图形报表-maven-jfreechat

本篇博客采用  jfreechat 直接生成图形报表。 

首先maven项目需要引入包。

<dependency>

    	<groupId>org.jfree</groupId>
    	<artifactId>jfreechart</artifactId>
    	<version>1.0.19</version>
    </dependency>

然后在web.xml配置:

 
  <servlet> 
      <servlet-name>displayChart</servlet-name> 
      <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> 
  </servlet> 
  <servlet-mapping> 
      <servlet-name>displayChart</servlet-name> 
      <url-pattern>/displayChart</url-pattern> 
  </servlet-mapping>

最后新建一个jsp文件;

<%@page import="java.util.Set"%>
<%@page import="java.awt.Font"%>
<%@page import="org.jfree.chart.StandardChartTheme"%>
<%@page import="java.awt.Color"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.jfree.chart.axis.DateAxis"%>
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.data.time.Month"%>
<%@page import="org.jfree.data.time.TimeSeriesCollection"%>
<%@page import="org.jfree.data.time.TimeSeries"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<%

TimeSeries series2010 = new TimeSeries("2010年度" );
TimeSeries series2011 = new TimeSeries("2011年度" );
TimeSeriesCollection dataset = new TimeSeriesCollection();
series2010.add(new Month(1, 2011), 36);
series2010.add(new Month(2, 2011), 84);
series2010.add(new Month(3, 2011), 202);
series2010.add(new Month(4, 2011), 183);
series2010.add(new Month(5, 2011), 172);
series2010.add(new Month(6, 2011), 107);
series2010.add(new Month(7, 2011), 200);
series2010.add(new Month(8, 2011), 87);
series2010.add(new Month(9, 2011), 250);
series2010.add(new Month(10, 2011), 152);
series2010.add(new Month(11, 2011), 235);
series2010.add(new Month(12, 2011), 266);
series2011.add(new Month(1, 2011), 156);
series2011.add(new Month(2, 2011), 216);
series2011.add(new Month(3, 2011), 118);
series2011.add(new Month(4, 2011), 250);
series2011.add(new Month(5, 2011), 257);
series2011.add(new Month(6, 2011), 253);
series2011.add(new Month(7, 2011), 355);
series2011.add(new Month(8, 2011), 300);
series2011.add(new Month(9, 2011), 103);
series2011.add(new Month(10, 2011), 250);
series2011.add(new Month(11, 2011), 348);
series2011.add(new Month(12, 2011), 325);
dataset.addSeries(series2010);
dataset.addSeries(series2011);
StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  
//设置标题字体  
standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
//设置图例的字体  
standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
//设置轴向的字体  
standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));  
ChartFactory.setChartTheme(standardChartTheme) ; 
JFreeChart chart = ChartFactory.createTimeSeriesChart("A产品销售量", "", "", dataset, true, true, false);
DateAxis dateaxis = (DateAxis)chart.getXYPlot().getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat("MM月")); 
chart.setBackgroundPaint(Color.WHITE);
String fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session );
%>
<img alt="" src=http://www.mamicode.com/"displayChart?filename=">>

生成的图片:



博客地址:http://blog.csdn.net/hfmbook

java三步 在线生成图形报表-maven-jfreechat