7

我在 iReports 中构建图表,当我在 Eclipse 中编译时出现以下错误:

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)

我将 ProtoReport 的ArrayList 现在测试时只有一个)传递给 jasper 编译器。ProtoReport类包含一个ChartData类,该类又具有两个Double 类型的ArrayList,一个用于 X 轴,一个用于 Y 轴。ProtoReportChartData类和主程序的 定义如下(略删):

原型报告类:

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";

}

图表数据类:

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;
    }

}

我已经用谷歌搜索了这个问题并提出了这个这个,并阅读了jasper sourceforge 上的数据源部分,但这些都没有帮助解决这个问题,我确定我已经使用过

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

在我的图表数据源表达式中(并在我更改字段名称时更新它)并且字段设置为 iReport 属性中的列表类型。

这是我的.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 中: -在字段下
创建和命名字段ProtoReport.ChartData.xAxisProtoReport.ChartData.yAxis以及我添加的ChartData源下的字段-将字段重命名
ChartData.xAxisChartData.yAxis
-重 命名字段到chartData.xAxischartData.yAxis
- 仅 使用 Field 下的字段- 仅
使用 ChartData 下的字段 - Fields

都给我一个错误。任何想法我做错了什么?

其他问题:
-就目前而言,我的图表可能只会产生 xAxis 数据点。如何将一个ArrayList的内容用于 X 轴,另一个用于 Y 轴?即我在ChartData对象中的xAxisyAxis字段。 -我在main之前声明的 randomData() 方法在我声明它为static之前不会运行,这是为什么呢?

编辑
更清楚地说明我的问题:如何命名我的字段、设置我的数据源和配置我的数据集以解决这个问题?
我正在关注评论中列出的教程(对不起非超链接;作为一个新用户,我已经使用了我所有的超链接),并根据我的需要对其进行了更改,但他的数据结构比我的更简单我想学习如何处理 jasper 报告中更复杂的对象和数据集。

4

1 回答 1

17

在 .jrxml 中,图表的数据源定义为:

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

这来自 iReports 4.7.0 自动填充,当您在详细信息窗格中的Connection/Datasource exp下选择Use datasource expression时,iReports 的默认表达式为:

 new net.sf.jasperreports.engine.JREmptyDataSource(1)

但是这个表达式是错误的,因为.data丢失了。它应该是:

 new net.sf.jasperreports.engine.data.JREmptyDataSource(1)

然后我当然将JREmptyDataSource (1)更改为JRBeanCollectionDataSource($F{chartData})

这修复了编译时错误,然后我必须重新组织和重新定义我的列表和 POJO,以便我可以轻松访问我的ChartData对象列表中的数据点对。从本质上讲,这意味着我现在有一个 ChartData 对象列表,而不是具有两个双精度列表(一个用于 X 轴,一个用于 Y 轴)的单个 ChartData 对象,每个对象只有一个 XY 点。目前这似乎运作良好。

于 2012-09-24T11:25:00.667 回答