2012-09-21 76 views
4

我建立一個圖表中iReports,當我在Eclipse編譯我收到以下錯誤:碧玉報告:JRBeanCollectionDataSource不能被解析爲一個類型

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 
1. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type 
      value = new  net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getValue())); //$JR_EXPR_ID=11$ 
         <----------------------------------------------------> 
2. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type 
      value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getOldValue())); //$JR_EXPR_ID=11$ 
         <----------------------------------------------------> 
3. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type 
      value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getValue())); //$JR_EXPR_ID=11$ 
         <---------------------------------------------------->3 errors 

at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:204) 
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240) 
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:173) 
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448) 
at org.reportprotojava.protosheet.Program.main(Program.java:122) 

我傳遞一個的ArrayListProtoReport (只有一個,現在當我測試),以碧玉編譯器。 ProtoReport類包含一個ChartData類,該類又有兩個類型爲Double的ArrayList,一個用於X軸,另一個用於Y軸。
如下(略有刪節)的所述ProtoReportChartData類的定義,和主程序:

ProtoReport類別:

package org.reportprotojava.protosheet; 

import java.util.ArrayList; 

public class ProtoReport { 


private String outputFileName; 
private String title; 
private String logoLocation; 
private String paragraphText; 
private ArrayList<String> tableData; 
private String picLocation; 
private int[][] graphData; //TODO decide how to store chart data 
private ChartData chartData; 
private String path; 




//default constructor 
public ProtoReport() { 

    // Initialize object fields 
    outputFileName = "PrototypeReport"; 
    title = "Prototype Report"; 
    paragraphText = "Default text"; 

    tableData = new ArrayList<String>(); 
    chartData = new ChartData(); 

    //set path to working directory 
    path = System.getProperty("user.dir"); 

    //default to assumed report location 
    //(ie same folder as .jrxml and .jasper files) 
    logoLocation = path + "\\reports\\logo.jpg"; 
    picLocation = path + "\\reports\\pic.jpg"; 

} 

ChartData類別:

package org.reportprotojava.protosheet; 

import java.util.ArrayList; 

public class ChartData { 

private ArrayList<Double> xAxis; 
private ArrayList<Double> yAxis; 
/** 
* @param xAxis 
* @param yAxis 
*/ 

//default constructor 
public ChartData(){ 
    xAxis = new ArrayList<Double>(); 
    yAxis = new ArrayList<Double>(); 
} 



//constructor 
public ChartData(ArrayList<Double> xAxis, ArrayList<Double> yAxis) { 
    super(); 
    this.xAxis = xAxis; 
    this.yAxis = yAxis; 
} 

主程序

public class Program { 

/** 
* @param args 
* 
*Program runs our ProtoReport class and its supporting classes 
* In the end we will have generated a .pdf from the 
* previously defined .jrxml file 
*/ 

//Generate some random data for the chart 
public static ArrayList<Double> randomData(int size) { 
    ArrayList<Double> arrayList = new ArrayList<Double>(); 
    double randNumber; 

    for (int i = 0; i < size; i++) { 
     randNumber = Math.random(); 
     arrayList.add(randNumber); 
    } 

    return arrayList; 
} 

public static void main(String[] args) { 

    ArrayList<ProtoReport> listOfReports = new ArrayList<ProtoReport>(); 

    ProtoReport protoReport1 = new ProtoReport(); 
    ProtoReport protoReport2 = new ProtoReport(); 

//Simple Fields and text 
    protoReport1.setTitle("Example<br/>Fact Sheet"); 

    protoReport1.setLogoLocation(protoReport1.getPath() + "\\reports\\logo.gif"); 

ChartData chartData = new ChartData(); 
    chartData.setYAxis(randomData(20)); 
    for (Double i = (double) 0; i < chartData.getYAxis().size(); i++) { 
     chartData.getXAxis().add(i); 
    } 

     protoReport1.setChartData(chartData); 
String jrxmlLocation = protoReport1.getPath() 
        + "\\reports\\ReportPrototype.jrxml"; 
    String outputFileName = protoReport1.getPath() 
      + "\\reports\\generated\\" + protoReport1.getOutputFileName() + ".pdf"; 

    listOfReports.add(protoReport1); 

//and wrap the ArrayList in a JRBeanCollectionDataSource 
    JRBeanCollectionDataSource beanBurritoWrap = new JRBeanCollectionDataSource(listOfReports); 

    //build the jasper report 
    JasperReport jasperReport; 
    JasperPrint jasperPrint; 
    HashMap<String, Object> hashMap = new HashMap<>(); 
    boolean reportCreated; 

    try { 

     jasperReport = JasperCompileManager.compileReport(jrxmlLocation); 
     jasperPrint = JasperFillManager.fillReport(jasperReport, hashMap, beanBurritoWrap); 
     JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName); 
     reportCreated=true; 
    } 
    catch (JRException e) { 
      e.printStackTrace(); 
      reportCreated=false; 
    } 

} 

我GOOGLE了這個問題,並拿出thisthis,並宣讀了datasource section on jasper sourceforge,但這些都沒有幫助解決這個問題,而且我確信,我「VE在我的圖表數據源表達用於

new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{chartData.xAxis}) 

(和當我改變字段名稱更新它)和字段被設置爲在iReport的p中列表類型roperties。

這裏是我的的.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="ReportPrototype.jrxml" pageWidth="595" pageHeight="842" columnWidth="495" leftMargin="57" rightMargin="43" topMargin="43" bottomMargin="43" uuid="10825c57-f953-4166-bf03-8ecabe8a8f47"> 
<property name="ireport.zoom" value="0.75"/> 
<property name="ireport.x" value="0"/> 
<property name="ireport.y" value="232"/> 
<subDataset name="ChartData" uuid="fc9ec0af-3e1a-40a7-8eb4-9ad30a266dee"> 
    <field name="chartData.xAxis" class="java.lang.String"/> 
</subDataset> 
<queryString language="SQL"> 
    <![CDATA[]]> 
</queryString> 
<field name="title" class="java.lang.String"/> 
<field name="logoLocation" class="java.lang.String"/> 
<field name="picLocation" class="java.lang.String"/> 
<field name="chartData.xAxis" class="java.lang.String"/> 
<detail> 
    <band height="740" splitType="Stretch"> 
     <textField isStretchWithOverflow="true" pattern=""> 
      <reportElement uuid="519c6bb5-72f9-4c25-8e91-47865ae0c9df" mode="Opaque" x="39" y="75" width="378" height="45" forecolor="#000099"/> 
      <textElement textAlignment="Center" verticalAlignment="Middle" markup="html"> 
       <font size="26"/> 
      </textElement> 
      <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression> 
     </textField> 
     <image onErrorType="Icon"> 
      <reportElement uuid="3759a707-32a4-49ef-a9c6-b0ad7136f738" x="216" y="264" width="279" height="246"/> 
      <imageExpression><![CDATA[$F{picLocation}]]></imageExpression> 
     </image> 
     <image onErrorType="Icon"> 
      <reportElement uuid="f989f871-32ea-4f13-ae3f-3f487cde76dd" x="295" y="0" width="200" height="42"/> 
      <imageExpression><![CDATA[$F{logoLocation}]]></imageExpression> 
     </image> 
     <xyLineChart> 
      <chart> 
       <reportElement uuid="ae87fc13-b92e-4a2a-b218-d395343f6028" x="0" y="537" width="495" height="203"/> 
       <chartTitle/> 
       <chartSubtitle/> 
       <chartLegend/> 
      </chart> 
      <xyDataset> 
       <dataset> 
        <datasetRun subDataset="ChartData" uuid="de7fb84d-17ea-4e5e-82bf-2015e72e4982"> 
         <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{chartData.xAxis})]]></dataSourceExpression> 
        </datasetRun> 
       </dataset> 
      </xyDataset> 
      <linePlot> 
       <plot/> 
      </linePlot> 
     </xyLineChart> 
    </band> 
</detail> 
<pageFooter> 
    <band height="16"> 
     <break> 
      <reportElement uuid="0d30dea4-a6af-4e41-b7be-c288f3188dbf" x="0" y="11" width="100" height="1"/> 
     </break> 
    </band> 
</pageFooter> 

在iReports我已經試過:
-Creating和命名領域ProtoReport.ChartData.xAxisProtoReport.ChartData.yAxis下和ChartData領域源I加入
-Renaming田裏ChartData.xAxisChartData.yAxis
-Renaming田裏chartData.xAxischartData.yAxis
- 只是使用領域
下的字段 - 只使用ChartData下的字段 - 字段

所有的都給我一個錯誤。任何想法我做錯了什麼?

其他問題:
-As它代表我的圖表將可能只產生x軸的數據點。我如何將一個ArrayList的內容用於X軸,另一個用於Y軸?即我的xAxisyAxis字段ChartData對象。
-The randomData()方法我主之前聲明不會跑,直到我宣佈它靜態,這是爲什麼?

編輯
爲了更清楚地說明我的問題:我如何命名我的領域,我的設置數據源,而爲了解決這個問題,我的配置數據集?
我正在關注評論中列出的教程(對於非超鏈接;作爲新用戶,我已經使用了所有超鏈接),並根據我的需要對其進行了更改,但是他的數據結構更簡單比我的,我想學習如何處理碧玉報告中更復雜的對象和數據集。當選擇下在連接/數據源EXP使用數據源表達

<![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{chartData.xAxis})]]> 

這來自iReports 4.7.0自動填充:

+0

本教程是[這一個](http://www.ramkitech.com/2012/09/integrate-charts-in-jasper-reports-jsf.html) – eljaydub

回答