package com.xino.jfreechart;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
public class TimeSeriesDemo {
// private static Log log = LogFactory.getLog(TimeSeriesDemo.class);
// 曲线图标题
private String title = "趋势分析";
// 曲线图X轴提示
private String domain = "月份走势";
// 曲线图Y轴提示
private String range = "应收余额";
// 曲线图自标题
private String subtitleStr = "2005财年分析";
// 创建时间数据源
// 每一个TimeSeries在图上是一条曲线
private TimeSeries yongyou = new TimeSeries("用友");
private TimeSeries jindie = new TimeSeries("金碟");
private TimeSeries dongruan = new TimeSeries("东软");
// JFreeChartchart=ChartFactory.createLineChart(title,domain,range,defaultcategorydataset,PlotOrientation.VERTICAL,true,true,false);
// 时间曲线数据集合
private static TimeSeriesCollection dataset = new TimeSeriesCollection();
/** ********************************自定义走线****************************************************** */
private DefaultCategoryDataset GetDefaultCategoryDataset() {
String[] month = new String[] { "2005-01", "2005-02", "2005-04",
"2005-10" };
int[] data2005 = new int[] { 12, 0, 7, 8 };
int[] data2006 = new int[] { 11, 5, 6, 9 };
int[] data2007 = new int[] { 0, 4, 5, 10 };
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
for (int i = 0; i < month.length; i++) {
defaultcategorydataset.addValue(new Double(data2005[i]), "2005年",
month[i]);
}
for (int i = 0; i < month.length; i++) {
defaultcategorydataset.addValue(new Double(data2006[i]), "2006年",
month[i]);
}
for (int i = 0; i < month.length; i++) {
defaultcategorydataset.addValue(new Double(data2007[i]), "2007年",
month[i]);
}
return defaultcategorydataset;
}
/** ************************************************************************************** */
private void setDataset() {
dataset.removeAllSeries(); // 在javaBean中使用此类这个很重要,不然会不停的加入新的TimeSeries;
dataset.addSeries(this.yongyou);
dataset.addSeries(this.jindie);
dataset.addSeries(this.dongruan);
}
private void setDongruan() {
this.dongruan.clear(); // 很重要,不然会报错
/**
* 报错 org.apache.jasper.JasperException: You are attempting to add an
* observation for the time period 这里是在类属性里声明TimeSeries才用到的
*/
for (int i = 1999; i < 2005; i++) {
for (int mon = 0; mon < 12; mon++) {
this.dongruan.add(new TimeSeriesDataItem(
new Day(1, mon + 1, i), new Double(
300 - Math.random() * 100)));
}
}
}
private void setJindie() {
this.jindie.clear(); // 很重要,不然会报错
for (int i = 1999; i < 2005; i++) {
for (int mon = 0; mon < 12; mon++) {
this.jindie.add(new TimeSeriesDataItem(new Day(1, mon + 1, i),
new Double(400 - Math.random() * 100)));
}
}
}
private void setYongyou() {
this.yongyou.clear(); // 很重要,不然会报错
for (int i = 1999; i < 2005; i++) {
for (int mon = 0; mon < 12; mon++) {
this.yongyou.add(new TimeSeriesDataItem(new Day(1, mon + 1, i),
new Double(500 - Math.random() * 100)));
}
}
}
private TimeSeriesCollection getDataset() {
this.setDongruan();
this.setJindie();
this.setYongyou();
this.setDataset();
return dataset;
}
public String drawPic(HttpSession session) {
String fileName = null;
JFreeChart chart = ChartFactory.createLineChart(title, domain, range,
GetDefaultCategoryDataset(), PlotOrientation.VERTICAL, true,
true, false);
CategoryPlot linePlot = chart.getCategoryPlot();
linePlot.getDomainAxis().setLabelFont(new Font("SimSun", Font.ITALIC, 15));//X轴文字字体
linePlot.getRangeAxis().setLabelFont(new Font("SimSun", Font.ITALIC, 15));// Y轴文字字体
// JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,
// range, getDataset(), true, true, false);
TextTitle subTitle = new TextTitle(subtitleStr, new Font("黑体",
Font.BOLD, 12));// 子标题字体
chart.addSubtitle(subTitle);
chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));// 标题字体
chart.setBorderPaint(new GradientPaint(0, 0, Color.white, 0, 1000,
Color.blue));
chart.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 15));// 底部文字
// XYPlot xyPlot = (XYPlot) chart.getPlot();
// xyPlot.getDomainAxis()
// .setLabelFont(new Font("SimSun", Font.ITALIC, 15));// X轴文字字体
// xyPlot.getRangeAxis().setLabelFont(new Font("SimSun", Font.ITALIC, 15));// Y轴文字字体
// String fileName = "D://fuit2.jpeg";
try {
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
// log.debug("准备创建图片……");
fileName = ServletUtilities.saveChartAsJPEG(chart, 600, 600, info,
session);
// log.debug("图片创建成功!");
/**
* 如果是 Servlet 请求 返回下面这个
*/
// ChartUtilities.writeChartAsJPEG(response.getOutputStream(),
// 100,chart,400,300,null);
} catch (IOException e) {
// log.error("IO异常:",e);
}
// log.debug("返回的文件名为:" + fileName);
return fileName;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// TimeSeriesDemo timeSeriesDemo = new TimeSeriesDemo();
// System.out.println(timeSeriesDemo.drawPic(null));
}
}
TimeSeriesDemo.jsp
<%@ page language="java" contentType="text/html; charset=UTF8" pageEncoding="UTF8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<jsp:useBean id="timeSeries" scope="session" class="com.xino.jfreechart.TimeSeriesDemo"/>
</head>
<body>
<%
String fileName = timeSeries.drawPic(request.getSession());
String graphURL = request.getContextPath()+"/servlet/DisplayChart?filename=" + fileName;
%>
<img src="<%= graphURL %>" width="900" height="500" border="1"/>
</body>
</html>
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>/servlet/DisplayChart</url-pattern>
</servlet-mapping>