我完成了所有Reportstuff的工作,生成了PDF,并将其重定向为servlet响应。
报告打开,并且我通过的数据在那里(使用文本字段测试过...数据在那里)。当我尝试在折线图中显示数据时,它仅显示一个数据点。我已将图表按“天”数据分组,因为我希望每天都有一个新图表(这也可以工作,因为testdata中31天有31张图表。
加上我要显示的多行内容受“类型”数据的约束。所以我想在每张图上显示四行:数据本身(类型= 1),最小值(类型= 2),平均值(类型= 3)和最大值(类型= 3)。但这根本行不通...我的错误在哪里?
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="jasper_report_template" pageWidth="595" pageHeight="842" whenNoDataType="NoPages" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50" uuid="c5395651-075a-4d6a-a971-d283cab77f63">
<parameter name="ReportTitle" class="java.lang.String"/>
<parameter name="Author" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="day" class="java.lang.Integer">
<fieldDescription><![CDATA[day]]></fieldDescription>
</field>
<field name="type" class="java.lang.Integer">
<fieldDescription><![CDATA[type]]></fieldDescription>
</field>
<field name="time" class="java.lang.String">
<fieldDescription><![CDATA[time]]></fieldDescription>
</field>
<field name="kwh" class="java.lang.Double">
<fieldDescription><![CDATA[kwh]]></fieldDescription>
</field>
<sortField name="day"/>
<sortField name="type"/>
<group name="DayGroup" minHeightToStartNewPage="60">
<groupExpression><![CDATA[$F{day}]]></groupExpression>
<groupHeader>
<band height="210" splitType="Stretch">
<lineChart>
<chart evaluationTime="Group" evaluationGroup="DayGroup">
<reportElement x="-20" y="10" width="560" height="200" uuid="9abe6b47-1c6f-4bd0-a684-abf9e91b4adc"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<dataset resetType="Group" resetGroup="DayGroup" incrementType="Group" incrementGroup="DayGroup"/>
<categorySeries>
<seriesExpression><![CDATA[$F{type}]]></seriesExpression>
<categoryExpression><![CDATA[$F{time}]]></categoryExpression>
<valueExpression><![CDATA[$F{kwh}]]></valueExpression>
</categorySeries>
</categoryDataset>
<linePlot>
<plot/>
<categoryAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</valueAxisFormat>
</linePlot>
</lineChart>
</band>
</groupHeader>
</group>
</jasperReport>
JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(BasicDataHandler.getInstance().getThatBeanList());
String path = getServletContext().getRealPath("/reports/");
JasperReport jasReport = JasperCompileManager.compileReport(path+ "/template_all_month_line_graph.jrxml");
JasperPrint jasPrint;
jasPrint = JasperFillManager.fillReport(jasReport, null, jrbcds);
ServletOutputStream sos = response.getOutputStream();
response.setContentType("application/pdf");
JasperExportManager.exportReportToPdfStream(jasPrint, sos);
package sc2pdf.datahandlers;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import sc2pdf.datasets.DailyDataSet;
import sc2pdf.javabeans.DataObject;
public class BasicDataHandler {
private static BasicDataHandler myself=null;
private Map<Integer, DailyDataSet> data = null;
private Map<Integer, DailyDataSet> min_data = null;
private Map<Integer, DailyDataSet> avg_data = null;
private Map<Integer, DailyDataSet> max_data = null;
public BasicDataHandler() {
data = new HashMap<Integer, DailyDataSet>(); //holds all the real data values; key: DAY_OF_MONTH; value DailyDataSet for every day of month
min_data = new HashMap<Integer, DailyDataSet>(); //holds all the min data values; key: DAY_OF_WEEK; value DailyDataSet for every day of week
avg_data = new HashMap<Integer, DailyDataSet>(); //holds all the average data values; key: DAY_OF_WEEK; value DailyDataSet for every day of week
max_data = new HashMap<Integer, DailyDataSet>(); //holds all the max data values; key: DAY_OF_WEEK; value DailyDataSet for every day of week
}
public static BasicDataHandler getInstance() {
if (myself == null) {
myself = new BasicDataHandler();
}
return myself;
}
private void registerDailyDataSet(DailyDataSet ds) {
Calendar c = Calendar.getInstance();
c.setTime(ds.getDay());
Integer tmp = new Integer(c.get(Calendar.DAY_OF_MONTH));
data.put(tmp, ds);
}
private void registerCalcValueMinDailyDataSet(DailyDataSet ds) {
Calendar c = Calendar.getInstance();
c.setTime(ds.getDay());
Integer tmp = new Integer(c.get(Calendar.DAY_OF_WEEK));
min_data.put(tmp, ds);
}
private void registerCalcValueAvgDailyDataSet(DailyDataSet ds) {
Calendar c = Calendar.getInstance();
c.setTime(ds.getDay());
Integer tmp = new Integer(c.get(Calendar.DAY_OF_WEEK));
avg_data.put(tmp, ds);
}
private void registerCalcValueMaxDailyDataSet(DailyDataSet ds) {
Calendar c = Calendar.getInstance();
c.setTime(ds.getDay());
Integer tmp = new Integer(c.get(Calendar.DAY_OF_WEEK));
max_data.put(tmp, ds);
}
public void registerDataObject(DataObject dob) {
Calendar c = Calendar.getInstance();
c.setTime(dob.getTimeAsDate());
Integer key = c.get(Calendar.DAY_OF_MONTH);
DailyDataSet ds = data.get(key);
if(ds == null) {
registerDailyDataSet(new DailyDataSet(dob.getTimeAsDate()));
ds = data.get(key);
}
ds.addDataObject(dob);
Integer keyCalcValues = c.get(Calendar.DAY_OF_WEEK);
ds = min_data.get(keyCalcValues);
DataObject t;
if(ds == null) {
registerCalcValueMinDailyDataSet(new DailyDataSet(dob.getTimeAsDate()));
ds = min_data.get(keyCalcValues);
ds.addDataObject(new DataObject(dob.getTimeAsDate(),dob.getKwh(),DataObject.TYPE_MIN));
}else{
t = ds.getDataObjectByWeekday(dob.getTimeAsDate());
if(t==null) {
t=new DataObject(dob.getTimeAsDate(),dob.getKwh(),DataObject.TYPE_MIN);
ds.addDataObject(t);
}
if(t.getKwh()>dob.getKwh())
t.setKwh(dob.getKwh());
}
ds = avg_data.get(keyCalcValues);
if(ds == null) {
registerCalcValueAvgDailyDataSet(new DailyDataSet(dob.getTimeAsDate()));
ds = avg_data.get(keyCalcValues);
ds.addDataObject(new DataObject(dob.getTimeAsDate(),dob.getKwh(),DataObject.TYPE_AVG));
}else{
t = ds.getDataObjectByWeekday(dob.getTimeAsDate());
if(t==null) {
t=new DataObject(dob.getTimeAsDate(),dob.getKwh(),DataObject.TYPE_AVG);
ds.addDataObject(t);
}
if(t.getKwh()>dob.getKwh())
t.setKwh(dob.getKwh());
}
ds = max_data.get(keyCalcValues);
if(ds == null) {
registerCalcValueMaxDailyDataSet(new DailyDataSet(dob.getTimeAsDate()));
ds = max_data.get(keyCalcValues);
ds.addDataObject(new DataObject(dob.getTimeAsDate(),dob.getKwh(),DataObject.TYPE_MAX));
}else{
t = ds.getDataObjectByWeekday(dob.getTimeAsDate());
if(t==null) {
t=new DataObject(dob.getTimeAsDate(),dob.getKwh(),DataObject.TYPE_MAX);
ds.addDataObject(t);
}
if(t.getKwh()<dob.getKwh())
t.setKwh(dob.getKwh());
}
}
public List<DataObject> getThatBeanList() {
List<DataObject> rl = new ArrayList<DataObject>();
Iterator<Integer> iter = data.keySet().iterator();
Calendar c = Calendar.getInstance();
while (iter.hasNext()) {
Integer keyToDo = iter.next();
DailyDataSet dds = data.get(keyToDo);
rl.addAll(dds.getData());
c.setTime(dds.getDay());
Integer dayKey = c.get(Calendar.DAY_OF_WEEK);
DailyDataSet dsmin = min_data.get(dayKey);
rl.addAll(dsmin.getData());
DailyDataSet dsavg = avg_data.get(dayKey);
rl.addAll(dsavg.getData());
DailyDataSet dsmax = max_data.get(dayKey);
rl.addAll(dsmax.getData());
}
return rl;
}
}
package sc2pdf.datasets;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import sc2pdf.javabeans.DataObject;
public class DailyDataSet {
private Date day;
private Collection<DataObject> data;
public DailyDataSet() {
this(new Date());
}
public DailyDataSet(Date d) {
this.day=d;
this.data = new ArrayList<DataObject>();
}
public void addDataObject(Date d, double k) {
this.data.add(new DataObject(d,k));
}
public void addDataObject(DataObject d) {
this.data.add(d);
}
public Date getDay() {
return day;
}
public Collection<DataObject> getData(){
return data;
}
public DataObject getDataObject(Date toSearch) {
Iterator<DataObject> iter = data.iterator();
Calendar c = Calendar.getInstance();
c.setTime(toSearch);
while (iter.hasNext()) {
DataObject dob = iter.next();
Calendar c2 = Calendar.getInstance();
c2.setTime(dob.getTimeAsDate());
if(c.equals(c2)) {
return dob;
}
}
return null;
}
public DataObject getDataObjectByWeekday(Date toSearch) {
Iterator<DataObject> iter = data.iterator();
Calendar c = Calendar.getInstance();
c.setTime(toSearch);
while (iter.hasNext()) {
DataObject dob = iter.next();
Calendar c2 = Calendar.getInstance();
c2.setTime(dob.getTimeAsDate());
if(c.get(Calendar.DAY_OF_WEEK) == c2.get(Calendar.DAY_OF_WEEK)) {
if(c.get(Calendar.HOUR_OF_DAY) == c2.get(Calendar.HOUR_OF_DAY)) {
if(c.get(Calendar.MINUTE) == c2.get(Calendar.MINUTE)) {
return dob;
}
}
}
}
return null;
}
}
package sc2pdf.javabeans;
import java.util.Calendar;
import java.util.Date;
public class DataObject {
private Date timeAsDate;
private Integer day;
private String timeAsString;
private double kwh;
private Integer type;
public static final Integer TYPE_DATA = 1;
public static final Integer TYPE_MIN = 2;
public static final Integer TYPE_AVG = 3;
public static final Integer TYPE_MAX = 4;
/**
* Constructor
*/
public DataObject() {
this(new Date(),0);
}
public DataObject(int day, int month, int year, int hour, int minute, double k) {
this(day,month,year,hour,minute,k,TYPE_DATA);
}
public DataObject(int day, int month, int year, int hour, int minute, double k, Integer typ) {
Calendar c = Calendar.getInstance();
c.set(year,month,day,hour,minute);
this.day=new Integer(c.get(Calendar.DAY_OF_MONTH));
this.timeAsDate=c.getTime();
this.kwh=k;
this.type=typ;
generateTimeAsString();
}
public DataObject(Date t, double p) {
this(t,p,TYPE_DATA);
}
public DataObject(Date t, double p, Integer ty) {
this.timeAsDate=t;
this.kwh=p;
this.type=ty;
Calendar c = Calendar.getInstance();
c.setTime(t);
this.day=new Integer(c.get(Calendar.DAY_OF_MONTH));
generateTimeAsString();
}
public void generateTimeAsString() {
Calendar c = Calendar.getInstance();
c.setTime(timeAsDate);
timeAsString = c.get(Calendar.HOUR_OF_DAY) +
":"+
c.get(Calendar.MINUTE);
}
public String getTime() {
return timeAsString != null?timeAsString:"WRONG STRING";
}
public void setTime(String s) {
this.timeAsString = s;
}
public Integer getDay() {
return day;
}
public void setDay(Integer day) {
this.day = day;
}
public double getKwh() {
return kwh;
}
public void setKwh(double kwh) {
this.kwh = kwh;
}
public Date getTimeAsDate() {
return timeAsDate;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String toString() {
return (type.equals(DataObject.TYPE_DATA)?"DATA: ":(type.equals(DataObject.TYPE_MIN)?" MIN: ":(type.equals(DataObject.TYPE_AVG)?" AVG: ":" MAX: ")))+this.timeAsDate.toString()+": "+this.kwh;
}
}
DATA: Tue Jan 01 00:00:07 CET 2019: 2.501
DATA: Tue Jan 01 00:15:07 CET 2019: 2.679
DATA: Tue Jan 01 00:30:07 CET 2019: 2.93
DATA: Tue Jan 01 00:45:07 CET 2019: 2.363
DATA: Tue Jan 01 01:00:07 CET 2019: 2.589
DATA: Tue Jan 01 01:15:07 CET 2019: 2.423
DATA: Tue Jan 01 01:30:07 CET 2019: 2.531
DATA: Tue Jan 01 01:45:07 CET 2019: 2.976
DATA: Tue Jan 01 02:00:07 CET 2019: 2.369
DATA: Tue Jan 01 02:15:07 CET 2019: 2.636
DATA: Tue Jan 01 02:30:07 CET 2019: 2.391
DATA: Tue Jan 01 02:45:07 CET 2019: 2.667
DATA: Tue Jan 01 03:00:07 CET 2019: 2.88
DATA: Tue Jan 01 03:15:07 CET 2019: 2.378
DATA: Tue Jan 01 03:30:07 CET 2019: 2.723
DATA: Tue Jan 01 03:45:07 CET 2019: 2.511
DATA: Tue Jan 01 04:00:07 CET 2019: 2.789
DATA: Tue Jan 01 04:15:07 CET 2019: 2.867
DATA: Tue Jan 01 04:30:07 CET 2019: 3.101
DATA: Tue Jan 01 04:45:07 CET 2019: 3.321
DATA: Tue Jan 01 05:00:07 CET 2019: 2.438
DATA: Tue Jan 01 05:15:07 CET 2019: 2.616
DATA: Tue Jan 01 05:30:07 CET 2019: 3.146
DATA: Tue Jan 01 05:45:07 CET 2019: 5.882
DATA: Tue Jan 01 06:00:07 CET 2019: 4.814
DATA: Tue Jan 01 06:15:07 CET 2019: 4.593
DATA: Tue Jan 01 06:30:07 CET 2019: 5.078
DATA: Tue Jan 01 06:45:07 CET 2019: 5.69
DATA: Tue Jan 01 07:00:07 CET 2019: 9.734
DATA: Tue Jan 01 07:15:07 CET 2019: 12.63
DATA: Tue Jan 01 07:30:07 CET 2019: 14.304
DATA: Tue Jan 01 07:45:07 CET 2019: 14.52
DATA: Tue Jan 01 08:00:07 CET 2019: 14.988
DATA: Tue Jan 01 08:15:07 CET 2019: 14.984
DATA: Tue Jan 01 08:30:07 CET 2019: 14.748
DATA: Tue Jan 01 08:45:07 CET 2019: 13.859
DATA: Tue Jan 01 09:00:07 CET 2019: 12.038
DATA: Tue Jan 01 09:15:07 CET 2019: 15.084
DATA: Tue Jan 01 09:30:07 CET 2019: 14.787
DATA: Tue Jan 01 09:45:07 CET 2019: 15.069
DATA: Tue Jan 01 10:00:07 CET 2019: 15.764
DATA: Tue Jan 01 10:15:07 CET 2019: 16.71
DATA: Tue Jan 01 10:30:07 CET 2019: 15.549
DATA: Tue Jan 01 10:45:07 CET 2019: 14.21
DATA: Tue Jan 01 11:00:07 CET 2019: 14.073
DATA: Tue Jan 01 11:15:07 CET 2019: 12.233
DATA: Tue Jan 01 11:30:07 CET 2019: 13.17
DATA: Tue Jan 01 11:45:07 CET 2019: 11.973
DATA: Tue Jan 01 12:00:07 CET 2019: 11.982
DATA: Tue Jan 01 12:15:07 CET 2019: 10.907
DATA: Tue Jan 01 12:30:07 CET 2019: 8.087
DATA: Tue Jan 01 12:45:07 CET 2019: 4.575
DATA: Tue Jan 01 13:00:07 CET 2019: 2.96
DATA: Tue Jan 01 13:15:07 CET 2019: 1.884
DATA: Tue Jan 01 13:30:07 CET 2019: 0.995
DATA: Tue Jan 01 13:45:07 CET 2019: 2.691
DATA: Tue Jan 01 14:00:07 CET 2019: 1.826
DATA: Tue Jan 01 14:15:07 CET 2019: 2.487
DATA: Tue Jan 01 14:30:07 CET 2019: 2.984
DATA: Tue Jan 01 14:45:07 CET 2019: 2.808
DATA: Tue Jan 01 15:00:07 CET 2019: 3.539
DATA: Tue Jan 01 15:15:07 CET 2019: 2.49
DATA: Tue Jan 01 15:30:07 CET 2019: 3.473
DATA: Tue Jan 01 15:45:07 CET 2019: 6.41
DATA: Tue Jan 01 16:00:07 CET 2019: 6.977
DATA: Tue Jan 01 16:15:07 CET 2019: 6.854
DATA: Tue Jan 01 16:30:07 CET 2019: 12.807
DATA: Tue Jan 01 16:45:07 CET 2019: 10.83
DATA: Tue Jan 01 17:00:07 CET 2019: 8.435
DATA: Tue Jan 01 17:15:07 CET 2019: 8.633
DATA: Tue Jan 01 17:30:07 CET 2019: 4.887
DATA: Tue Jan 01 17:45:07 CET 2019: 3.594
DATA: Tue Jan 01 18:00:07 CET 2019: 3.36
DATA: Tue Jan 01 18:15:07 CET 2019: 4.145
DATA: Tue Jan 01 18:30:07 CET 2019: 9.11
DATA: Tue Jan 01 18:45:07 CET 2019: 8.714
DATA: Tue Jan 01 19:00:07 CET 2019: 7.982
DATA: Tue Jan 01 19:15:07 CET 2019: 6.545
DATA: Tue Jan 01 19:30:07 CET 2019: 6.764
DATA: Tue Jan 01 19:45:07 CET 2019: 7.394
DATA: Tue Jan 01 20:00:07 CET 2019: 9.887
DATA: Tue Jan 01 20:15:07 CET 2019: 7.292
DATA: Tue Jan 01 20:30:07 CET 2019: 5.972
DATA: Tue Jan 01 20:45:07 CET 2019: 6.924
DATA: Tue Jan 01 21:00:07 CET 2019: 6.426
DATA: Tue Jan 01 21:15:07 CET 2019: 5.675
DATA: Tue Jan 01 21:30:07 CET 2019: 5.973
DATA: Tue Jan 01 21:45:07 CET 2019: 5.936
DATA: Tue Jan 01 22:00:07 CET 2019: 6.06
DATA: Tue Jan 01 22:15:07 CET 2019: 5.646
DATA: Tue Jan 01 22:30:07 CET 2019: 4.484
DATA: Tue Jan 01 22:45:07 CET 2019: 2.481
DATA: Tue Jan 01 23:00:07 CET 2019: 2.49
DATA: Tue Jan 01 23:15:07 CET 2019: 2.423
DATA: Tue Jan 01 23:30:07 CET 2019: 2.49
DATA: Tue Jan 01 23:45:07 CET 2019: 2.955
MIN: Tue Jan 01 00:00:07 CET 2019: 2.154
MIN: Tue Jan 01 00:15:07 CET 2019: 2.093
MIN: Tue Jan 01 00:30:07 CET 2019: 2.052
MIN: Tue Jan 01 00:45:07 CET 2019: 2.126
MIN: Tue Jan 01 01:00:07 CET 2019: 2.139
MIN: Tue Jan 01 01:15:07 CET 2019: 2.171
MIN: Tue Jan 01 01:30:07 CET 2019: 2.162
MIN: Tue Jan 01 01:45:07 CET 2019: 2.178
MIN: Tue Jan 01 02:00:07 CET 2019: 2.049
MIN: Tue Jan 01 02:15:07 CET 2019: 2.033
MIN: Tue Jan 01 02:30:07 CET 2019: 2.034
MIN: Tue Jan 01 02:45:07 CET 2019: 2.129
MIN: Tue Jan 01 03:00:07 CET 2019: 2.159
MIN: Tue Jan 01 03:15:07 CET 2019: 2.165
MIN: Tue Jan 01 03:30:07 CET 2019: 2.001
MIN: Tue Jan 01 03:45:07 CET 2019: 2.006
MIN: Tue Jan 01 04:00:07 CET 2019: 2.313
MIN: Tue Jan 01 04:15:07 CET 2019: 1.992
MIN: Tue Jan 01 04:30:07 CET 2019: 2.082
MIN: Tue Jan 01 04:45:07 CET 2019: 1.982
MIN: Tue Jan 01 05:00:07 CET 2019: 2.217
MIN: Tue Jan 01 05:15:07 CET 2019: 2.088
MIN: Tue Jan 01 05:30:07 CET 2019: 2.09
MIN: Tue Jan 01 05:45:07 CET 2019: 1.923
MIN: Tue Jan 01 06:00:07 CET 2019: 1.928
MIN: Tue Jan 01 06:15:07 CET 2019: 2.337
MIN: Tue Jan 01 06:30:07 CET 2019: 2.333
MIN: Tue Jan 01 06:45:07 CET 2019: 2.397
MIN: Tue Jan 01 07:00:07 CET 2019: 2.135
MIN: Tue Jan 01 07:15:07 CET 2019: 2.45
MIN: Tue Jan 01 07:30:07 CET 2019: 2.288
MIN: Tue Jan 01 07:45:07 CET 2019: 2.405
MIN: Tue Jan 01 08:00:07 CET 2019: 2.093
MIN: Tue Jan 01 08:15:07 CET 2019: 2.403
MIN: Tue Jan 01 08:30:07 CET 2019: 3.309
MIN: Tue Jan 01 08:45:07 CET 2019: 2.904
day|type|time|kwh
1|1|00:15|0.9
1|1|00:30|1.2
1|2|00:15|0.8
1|2|00:30|1
1|3|00:15|0.8
1|3|00:30|1.1
1|4|00:15|1
1|4|00:30|1.5
2|1|00:15|0.7
2|1|00:30|1
2|2|00:15|0.6
2|2|00:30|0.9
2|3|00:15|0.7
2|3|00:30|1
2|4|00:15|0.8
2|4|00:30|1.1