package com.MonKey.dao.impl;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.hibernate.SessionFactory;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.MonKey.dao.ElecDayDao;
import com.MonKey.dao.ElecHourCurveDao;
import com.MonKey.entity.ElecTranhour;
public class ElecHourCurveDaoImpl implements ElecHourCurveDao {
private HibernateTemplate template;
public void setSessionFactory(SessionFactory session) {
this.template = new HibernateTemplate(session);
}
public String getImageUrl(boolean a,boolean b,boolean c,String id) {
String urlstr="";
try {
List list= template.find("from ElecTranhour where id.elecTranid=?",id);
urlstr= this.getImages(a, b, c, list);
} catch (IOException e) {
e.printStackTrace();
}
return urlstr;
}
//产生小时的电流或者电压数据又或者功率
public String getImages(boolean a,boolean b,boolean c, List<ElecTranhour> list) throws IOException{
String title = "小时电流数据走势图";
// 曲线图X轴提示
String domain = "时间段";
// 曲线图Y轴提示
String range = "电流数据";
XYSeries xsa = new XYSeries("A相电流");
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
XYSeries xsb = new XYSeries("B相电流");
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
XYSeries xsc = new XYSeries("C相电流");
for (int j = 0; j < list.size(); j++) {
ElecTranhour t= list.get(j);
Date date= t.getId().getElecHdate();
//电流数据图
xsa.add(date.getHours()+j,t.getElecAcurrent());
xsb.add(date.getHours()+j,t.getElecBcurrent());
xsc.add(date.getHours()+j,t.getElecCcurrent());
}
// 时间曲线数据集合
XYSeriesCollection xysc = new XYSeriesCollection();
xysc.removeAllSeries();
if(a){
xysc.addSeries(xsa);
}
if(b){
xysc.addSeries(xsb);
}
if(c){
xysc.addSeries(xsc);
}
//时间曲线元素
JFreeChart chart = ChartFactory.createXYLineChart(title, domain, range, xysc,PlotOrientation.VERTICAL,true,true,false);
XYPlot xyplot=(XYPlot)chart.getPlot();
XYLineAndShapeRenderer xyl=(XYLineAndShapeRenderer) xyplot.getRenderer();
//判断
if(a){
xyl.setSeriesPaint(0, new Color(255,0,0));
if(b){
xyl.setSeriesPaint(1, new Color(0,0,255));
if(c)
xyl.setSeriesPaint(2, new Color(0,255,0));
}else{
if(c)
xyl.setSeriesPaint(1, new Color(0,255,0));
}
}else{
if(b){
xyl.setSeriesPaint(0, new Color(0,0,255));
if(c)
xyl.setSeriesPaint(1, new Color(0,255,0));
}else{
if(c)
xyl.setSeriesPaint(0, new Color(0,255,0));
}
}
//创建文件并将图片地址传递到页面
String filename = ServletUtilities.saveChartAsPNG(chart,600,300,
null,null);
String graphURL = "../displayChart?filename="+ filename;
return graphURL;
}
}
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.hibernate.SessionFactory;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.MonKey.dao.ElecDayDao;
import com.MonKey.dao.ElecHourCurveDao;
import com.MonKey.entity.ElecTranhour;
public class ElecHourCurveDaoImpl implements ElecHourCurveDao {
private HibernateTemplate template;
public void setSessionFactory(SessionFactory session) {
this.template = new HibernateTemplate(session);
}
public String getImageUrl(boolean a,boolean b,boolean c,String id) {
String urlstr="";
try {
List list= template.find("from ElecTranhour where id.elecTranid=?",id);
urlstr= this.getImages(a, b, c, list);
} catch (IOException e) {
e.printStackTrace();
}
return urlstr;
}
//产生小时的电流或者电压数据又或者功率
public String getImages(boolean a,boolean b,boolean c, List<ElecTranhour> list) throws IOException{
String title = "小时电流数据走势图";
// 曲线图X轴提示
String domain = "时间段";
// 曲线图Y轴提示
String range = "电流数据";
XYSeries xsa = new XYSeries("A相电流");
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
XYSeries xsb = new XYSeries("B相电流");
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
XYSeries xsc = new XYSeries("C相电流");
for (int j = 0; j < list.size(); j++) {
ElecTranhour t= list.get(j);
Date date= t.getId().getElecHdate();
//电流数据图
xsa.add(date.getHours()+j,t.getElecAcurrent());
xsb.add(date.getHours()+j,t.getElecBcurrent());
xsc.add(date.getHours()+j,t.getElecCcurrent());
}
// 时间曲线数据集合
XYSeriesCollection xysc = new XYSeriesCollection();
xysc.removeAllSeries();
if(a){
xysc.addSeries(xsa);
}
if(b){
xysc.addSeries(xsb);
}
if(c){
xysc.addSeries(xsc);
}
//时间曲线元素
JFreeChart chart = ChartFactory.createXYLineChart(title, domain, range, xysc,PlotOrientation.VERTICAL,true,true,false);
XYPlot xyplot=(XYPlot)chart.getPlot();
XYLineAndShapeRenderer xyl=(XYLineAndShapeRenderer) xyplot.getRenderer();
//判断
if(a){
xyl.setSeriesPaint(0, new Color(255,0,0));
if(b){
xyl.setSeriesPaint(1, new Color(0,0,255));
if(c)
xyl.setSeriesPaint(2, new Color(0,255,0));
}else{
if(c)
xyl.setSeriesPaint(1, new Color(0,255,0));
}
}else{
if(b){
xyl.setSeriesPaint(0, new Color(0,0,255));
if(c)
xyl.setSeriesPaint(1, new Color(0,255,0));
}else{
if(c)
xyl.setSeriesPaint(0, new Color(0,255,0));
}
}
//创建文件并将图片地址传递到页面
String filename = ServletUtilities.saveChartAsPNG(chart,600,300,
null,null);
String graphURL = "../displayChart?filename="+ filename;
return graphURL;
}
}