最近的项目需要作图形报表,打算用jfreechart实现,在网上查了些资料,做了一个简单的例子.运行后图表效果。
首先建立数据库,sql语句:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[KRM]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[KRM]
GO
CREATE TABLE [dbo].[KRM] (
[curve_id] [varchar] (60) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[curve_owner] [varchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,
[curve_cent] [decimal](18, 0) NULL ,
[curve_year] [int] NULL ,
[curve_month] [int] NULL ,
[curve_day] [int] NULL ,
[note] [text] COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
下面是两个数据持久化类:AbstractKrm.java
package com.longxin.chart.test;
import java.io.Serializable;
/**
* A class that represents a row in the KRM table.
* You can customize the behavior of this class by editing the class, {@link Krm()}.
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*/
public abstract class AbstractKrm
implements Serializable
{
/** The value of the simple curveId property. */
private java.lang.String curveId;
/** The value of the simple curveOwner property. */
private java.lang.String curveOwner;
/** The value of the simple curveCent property. */
private java.lang.Long curveCent;
/** The value of the simple curveYear property. */
private java.lang.Integer curveYear;
/** The value of the simple curveMonth property. */
private java.lang.Integer curveMonth;
/** The value of the simple curveDay property. */
private java.lang.Integer curveDay;
/** The value of the simple note property. */
private java.lang.String note;
/**
* Simple constructor of AbstractKrm instances.
*/
public AbstractKrm()
{
}
/**
* Return the value of the curve_id column.
* @return java.lang.String
*/
public java.lang.String getCurveId()
{
return this.curveId;
}
/**
* Set the value of the curve_id column.
* @param curveId
*/
public void setCurveId(java.lang.String curveId)
{
this.curveId = curveId;
}
/**
* Return the value of the curve_owner column.
* @return java.lang.String
*/
public java.lang.String getCurveOwner()
{
return this.curveOwner;
}
/**
* Set the value of the curve_owner column.
* @param curveOwner
*/
public void setCurveOwner(java.lang.String curveOwner)
{
this.curveOwner = curveOwner;
}
/**
* Return the value of the curve_cent column.
* @return java.lang.Long
*/
public java.lang.Long getCurveCent()
{
return this.curveCent;
}
/**
* Set the value of the curve_cent column.
* @param curveCent
*/
public void setCurveCent(java.lang.Long curveCent)
{
this.curveCent = curveCent;
}
/**
* Return the value of the curve_year column.
* @return java.lang.Integer
*/
public java.lang.Integer getCurveYear()
{
return this.curveYear;
}
/**
* Set the value of the curve_year column.
* @param curveYear
*/
public void setCurveYear(java.lang.Integer curveYear)
{
this.curveYear = curveYear;
}
/**
* Return the value of the curve_month column.
* @return java.lang.Integer
*/
public java.lang.Integer getCurveMonth()
{
return this.curveMonth;
}
/**
* Set the value of the curve_month column.
* @param curveMonth
*/
public void setCurveMonth(java.lang.Integer curveMonth)
{
this.curveMonth = curveMonth;
}
/**
* Return the value of the curve_day column.
* @return java.lang.Integer
*/
public java.lang.Integer getCurveDay()
{
return this.curveDay;
}
/**
* Set the value of the curve_day column.
* @param curveDay
*/
public void setCurveDay(java.lang.Integer curveDay)
{
this.curveDay = curveDay;
}
/**
* Return the value of the note column.
* @return java.lang.String
*/
public java.lang.String getNote()
{
return this.note;
}
/**
* Set the value of the note column.
* @param note
*/
public void setNote(java.lang.String note)
{
this.note = note;
}
}
类Krm.java
package com.longxin.chart.test;
import java.io.Serializable;
/**
* A class that represents a row in the 'KRM' table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Krm
extends AbstractKrm
implements Serializable
{
/**
* Simple constructor of Krm instances.
*/
public Krm()
{
}
/* Add customized code below */
}
hibernate映射文件:Krm.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Mon Sep 18 22:42:56 CST 2006 -->
<hibernate-mapping package="com.longxin.chart.test">
<class name="Krm" table="KRM">
<id name="curveId" column="curve_id" type="string">
<generator class="uuid.hex" />
</id>
<property name="curveOwner" column="curve_owner" type="string" />
<property name="curveCent" column="curve_cent" type="long" />
<property name="curveYear" column="curve_year" type="integer" />
<property name="curveMonth" column="curve_month" type="integer" />
<property name="curveDay" column="curve_day" type="integer" />
<property name="note" column="note" type="serializable" />
</class>
</hibernate-mapping>
hibernate的配置文件就省去了>>>>>>>>>>>
以下是生成图形的主要操作类:DrawPic.java
package com.longxin.chart.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import com.longxin.util.HibernateSessionFactory;
public class DrawPic {
/**
* 根据时间查询数据,将所得到的数据封装成TimeSeriesCollection对象
* TimeSeriesCollection是JfreeChart中定义的类的对象 本方法将被后面的draw方法调用
*
* @param int
* 要查询哪一年
* @param int
* 要查询哪一月
* @return TimeSeriesCollection
*/
public TimeSeriesCollection getData(int year, int month) {
//白瓷数据集合
List dataOfBC = new ArrayList();
// 彩瓷数据集合
List dataOfCC = new ArrayList();
try {
Session session = HibernateSessionFactory.currentSession();
Query query = session.createQuery(
"from Krm as krm where krm.curveOwner=:p").setParameter(
"p", "白瓷");
dataOfBC=query.list();
Query q = session.createQuery(
"from Krm as krm where krm.curveOwner=:p").setParameter(
"p", "彩瓷");
dataOfCC = q.list();
} catch (Exception error) {
System.out.print(error.getMessage());
} finally {
HibernateSessionFactory.closeSession();
}
// 构造要返回的对象
TimeSeriesCollection result = new TimeSeriesCollection();
// 这两个对象,分别对应一条曲线,构造函数的参数,将显示在图片的下方
TimeSeries fiona = new TimeSeries("白瓷");
TimeSeries bromon = new TimeSeries("彩瓷");
Krm cv = null;
// 遍历Fiona的数据集,填充fiona对象
for (int i = 0; i < dataOfBC.size(); i++) {
cv = (Krm) dataOfBC.get(i);
fiona.add(new TimeSeriesDataItem(new Day(cv.getCurveDay()
.intValue(), cv.getCurveMonth().intValue(), cv
.getCurveYear().intValue()), new Double(cv.getCurveCent()
.doubleValue())));
}
result.addSeries(fiona);
for (int i = 0; i < dataOfCC.size(); i++) {
cv = (Krm) dataOfCC.get(i);
bromon.add(new TimeSeriesDataItem(new Day(cv.getCurveDay()
.intValue(), cv.getCurveMonth().intValue(), cv
.getCurveYear().intValue()), new Double(cv.getCurveCent()
.doubleValue())));
}
result.addSeries(bromon);
return result;
}
// 生成图表的方法,图表数据来自上面的getData方法
public String draw(int y, int m) {
String result = null;
String title = "白瓷 & 彩瓷的产量分析图";
String subTitle = y + "年" + m + "月";
String domain = "时间";// x轴
String range = "产量";// y轴
TimeSeriesCollection data = this.getData(y, m);
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,
range, data, true, true, false);
TextTitle subtitle = new TextTitle(subTitle, new Font("宋体", Font.BOLD,
20));
chart.addSubtitle(subtitle);
chart.setTitle(new TextTitle(title, new Font("宋体", Font.ITALIC, 20)));
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000,
Color.white));
String fileName = "c:\\pics\\" + y + "" + m + ".jpg";
// 保存为位置
try {
File f = new File("c:\\pics");
if (!f.exists()) {
f.mkdir();
}
ChartUtilities.saveChartAsJPEG(new File(fileName), 1, chart,
500, 300);// 宽1000,高600
} catch (Exception e) {
e.printStackTrace();
}
result = fileName;
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new DrawPic().draw(2006, 3);
}
}