2014-10-09 39 views
0

其實我必須使用JasperReports繪製timeSeries。使用multipleDataSet繪製timeSeriex圖表或者在JRBeanCollectionDataSource中傳遞一個Collection對象

如何爲不同的數據集生成時間序列圖?當我有一個數據集在一段時間內繪圖時,我能夠生成時間序列圖。但是,當我有多個數據集來繪製然後我面臨問題。

我正在使用List數據源。

public class KpiReportPoints { 
    private String deviceDn; 
    private List<KpiReportPoint> kpiPoints; 
} 

public class KPiReportPoint{ 
    private Date time; 
    private Double value; 

    public Date getTime() { 
     return time; 
    } 

    public void setTime(Date time) { 
     this.time = time; 
    } 

    public Double getValue() { 
     return value; 
    } 

    public void setValue(Double value) { 
     this.value = value; 
    } 

    @Override 
    public int hashCode() { 
     return time.hashCode(); 
    } 

    @Override 
    public boolean equals(Object obj) { 
     boolean equal = false; 
     if (obj instanceof KPIReportPoint){ 
     KPIReportPoint point = (KPIReportPoint) obj; 
     equal = time.equals(point.getTime())&& value.equals(point.getValue()); 
     } 
     return equal; 
    } 

    @Override 
    public int compareTo(KPIReportPoint o) { 
     return this.time.compareTo(o.getTime()); 
    } 
} 

上述兩個類都有自己的getter和setter方法,所有都是公開的,所以要根據我沒有問題,有沒有在每一個定義。我通過填充的 List<KpiReportPoints>作爲JRBEanCollectionDataSource(kpiReportpointsList);

現在,下面是我的JRXML:

<?xml version="1.0" encoding="UTF-8"?> 
<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="KPI_Reports" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> 
    <subDataset name="Dataset"> 
     <field name="time" class="java.util.Date"/> 
     <field name="value" class="java.lang.Double"/> 
    </subDataset> 
    <parameter name="X_UNIT" class="java.lang.String"> 
     <parameterDescription><![CDATA[]]></parameterDescription> 
     <defaultValueExpression><![CDATA["Date"]]></defaultValueExpression> 
    </parameter> 
    <parameter name="Y_UNIT" class="java.lang.String"> 
     <parameterDescription><![CDATA[]]></parameterDescription> 
     <defaultValueExpression><![CDATA["%"]]></defaultValueExpression> 
    </parameter> 
    <parameter name="REPORT_NAME" class="java.lang.String"> 
     <defaultValueExpression><![CDATA["KPI Report"]]></defaultValueExpression> 
    </parameter> 
    <parameter name="from" class="java.util.Date"/> 
    <parameter name="to" class="java.util.Date"/> 
    <field name="deviceDn" class="java.lang.String"/> 
    <field name="kpiPoints" class="java.util.List"/> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <title> 
     <band height="20" splitType="Stretch"> 
      <textField pattern="EEE, d MMM yyyy HH:mm:ss Z" isBlankWhenNull="true"> 
       <reportElement x="513" y="0" width="253" height="20"/> 
       <textElement/> 
       <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression> 
      </textField> 
     </band> 
    </title> 
    <summary> 
     <band height="443"> 
      <timeSeriesChart> 
       <chart isShowLegend="true" customizerClass="com.ipaccess.nos.business.pm.impl.KPIChartCustomizer"> 
        <reportElement mode="Opaque" x="0" y="0" width="766" height="443" isPrintInFirstWholeBand="true"/> 
        <chartTitle position="Top"> 
         <font size="14" isBold="true" pdfFontName="Courier-Bold" isPdfEmbedded="true"/> 
         <titleExpression><![CDATA[$P{REPORT_NAME}]]></titleExpression> 
        </chartTitle> 
        <chartSubtitle/> 
        <chartLegend position="Bottom"/> 
       </chart> 
       <timeSeriesDataset timePeriod="Second"> 
        <datasetRun subDataset="Dataset"> 
         <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{kpiPoints})]]></dataSourceExpression> 
        </datasetRun> 
        <timeSeries> 
         <seriesExpression><![CDATA[$F{deviceDn}]]></seriesExpression> 
         <timePeriodExpression><![CDATA[$F{time}]]></timePeriodExpression> 
         <valueExpression><![CDATA[$F{value}]]></valueExpression> 
        </timeSeries> 
       </timeSeriesDataset> 
       <timeSeriesPlot isShowLines="true" isShowShapes="true"> 
        <plot labelRotation="90.0"/> 
        <timeAxisLabelExpression><![CDATA[$P{X_UNIT}]]></timeAxisLabelExpression> 
        <valueAxisLabelExpression><![CDATA[$P{Y_UNIT}]]></valueAxisLabelExpression> 
        <valueAxisFormat> 
         <axisFormat> 
          <labelFont> 
           <font size="10"/> 
          </labelFont> 
         </axisFormat> 
        </valueAxisFormat> 
        <domainAxisMinValueExpression><![CDATA[$P{from}]]></domainAxisMinValueExpression> 
        <domainAxisMaxValueExpression><![CDATA[$P{to}]]></domainAxisMaxValueExpression> 
       </timeSeriesPlot> 
      </timeSeriesChart> 
     </band> 
    </summary> 
</jasperReport> 

這個問題JRXML編譯罰款,但在試圖生成一個報告,它拋出的異常:

[#|2014-10-10T09:59:05.769+0530|SEVERE|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=290;_ThreadName=Thread-3;|net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : time 
     at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:127) 
     at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:100) 
     at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:104) 
    at net.sf.jasperreports.engine.fill.JRFillData 

我一直在這個問題完全瘋了並且無法在上面的代碼中找到可能導致此問題的單個問題。如果你們中的任何一個能夠看到並且有任何時間面對這個錯誤,那麼請給我一些線索,看看有什麼不對。

+0

子數據集的事情是它有自己的領域,並要求其自己的數據源,在你的情況是'列表'。雖然需要更多細節才能找到解決方案,但需要查看您的'JRXML'和您用來填充它的代碼。可能比結論可能是drwan。 – 2014-10-10 05:34:20

+0

以下是我的jrxml – 2014-10-10 09:29:49

回答

0

我想問題在這裏。

public class KpiReportPoints { 
    private String deviceDn; 
    private List<KpiReportPoint> kpiPoints; 
} 

你逝去的KpiReportPoints名單,這是很好的,但名單將futher包含private List<KpiReportPoint> kpiPoints;這是列表本身。

Jasper Report的問題是,它會識別JRBEanCollectionDataSource(kpiReportpointsList),並將獲取deviceDnkpiPoints。現在,由於您有字段$F{deviceDn}deviceDn,因此字符串是完美匹配的。這裏沒有問題。

問題部分: 它何時會獲取kpiPoints有noway Jasper Report會從該列表中獲取價值並填寫報告,因爲它是not the primitive type

解決方案: 我會建議你使用Custom Data Sourceredesign報告,使其識別kpiPoints名單,並進一步將獲取從該列表值和填充報告。

+0

感謝您的回覆。但我猜不會是下面的代碼: <![CDATA [new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ F {kpiPoints})]]>/dataSourceExpression> 正在做那部分。就像迭代這個kpiPoints列表作爲dataSource並獲取我在dataSet中描述的字段。我對碧玉很新,所以不確定這是否正確。你是否也可以在customDataSource上分享任何好的例子 – 2014-10-10 12:34:20

+0

現在,仔細看了你'jrxml',我不認爲你需要自定義數據源。你在評論中提到的代碼是要走的路。但我確實認爲這個設計儘管如你所說,可能是關注的領域。我建議你重新訪問設計,在不同的平臺上編譯它,看看那裏是否有問題。使用jrxml的當前代碼,您不應該遇到任何問題,但我認爲您錯過了某些部分。 – 2014-10-11 05:09:15

+0

任何一個對碧玉有很好的認識的人都可以幫助我......它真的讓我緊張起來,因爲我完全被它困住了。 – 2014-10-20 04:27:02

相關問題