package org.bromon.fiona.curve.dao;
import java.util.*;
public class CurveVo
{
private int year;
private int month;
private int day;
private double cent;
private String note;
private String owner;
//get/set方法
…………
}
package org.bromon.fiona.curve.util;
//一大堆import
public class DrawPic
{
/**
*根据时间查询数据,将所得到的数据封装成TimeSeriesCollection对象
*TimeSeriesCollection是JfreeChart中定义的类的对象
*本方法将被后面的draw方法调用
*@param int 要查询哪一年
*@param int 要查询哪一月
*@return TimeSeriesCollection
*/
public TimeSeriesCollection getData(int year,int month)
{
//fiona的数据集合,其中存放的是上面定义过的CurveVo值对象
//这些值对象的值来自于数据库,相关代码是最常见的jdbc内容,请自行完成
List dataOfFiona=new ArrayList();
//Bromon的数据集合
List dataOfBromon=new ArrayList();
//构造要返回的对象
TimeSeriesCollection result=new TimeSeriesCollection();
//这两个对象,分别对应一条曲线,构造函数的参数,将显示在图片的下方
TimeSeries fiona=new TimeSeries("fiona");
TimeSeries bromon=new TimeSeries("bromon");
CurveVo cv=null;
//遍历Fiona的数据集,填充fiona对象
for(int i=0;i<dataOfFiona.size();i++)
{
cv=(CurveVo)dataOfFiona.get(i);
fiona.add(new TimeSeriesDataItem(new Day(cv.getDay(),cv.getMonth(),cv.getYear()), new Double(cv.getCent())));
}
result.addSeries(fiona);
for(int i=0;i<dataOfBromon.size();i++)
{
cv=(CurveVo)dataOfBromon.get(i);
bromon.add(new TimeSeriesDataItem(new Day(cv.getDay(),cv.getMonth(),cv.getYear()),new Double(cv.getCent())));
}
result.addSeries(bromon);
}
//生成图表的方法,图表数据来自上面的getData方法
public String draw(int y,int m)
{
String result=null;
String title="Bromon & Fiona的爱情走势图";
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));
c.setTime(new Date());
String fileName="c://pics//"+y+""+m+".jpg";//保存为位置
try
{
File f=new File("c://pics");
if(!f.exists())
{
f.mkdir();
}
ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 1000, 600);//宽1000,高600
} catch (Exception e)
{
e.printStackTrace();
}
result=fileName;
return result;
}
//ok,测试一下
public static void main(String args[])
{
new DrawPic().draw(2005,3);
}
}
新闻热点
疑难解答