简体   繁体   English

如何将JRBeanCollectionDataSource传递给iReport?

[英]How To Pass a JRBeanCollectionDataSource to iReport?

I'm currently trying to use jasper to help me create reports. 我目前正在尝试使用jasper来帮助我创建报告。 I have the information and data that I want displayed in this method: 我有这个方法中显示的信息和数据:

private void writeToFile(final List<ScenarioLoadModel> sceneLoadModel) throws Exception {
  final BufferedWriter bw = new BufferedWriter(new FileWriter("/Uma/nft/result.psv"));

  for (final ScenarioLoadModel slm : sceneLoadModel) {
    bw.write(slm.getScenarioId() + PSP + slm.getScenarioId() + PSP + slm.getScenarioConfig().getName() + PSP + slm.getLoad() + PSP + "" + EOL);
    if (!slm.getScenarios().isEmpty()) {
      final int tempCount = slm.getScenarios().get(0).getTemplates().size();
      final int sceneCount = slm.getScenarios().size();
      for (int tempIdx = 0; tempIdx < tempCount; tempIdx++) {
        String id = null;
        int pass = 0;
        int fail = 0;
        final Map<String, BigDecimal> metricMap = new HashMap<String, BigDecimal>();
        final DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
        for (int sceneIdx = 0; sceneIdx < sceneCount; sceneIdx++) {
          final Template temp = slm.getScenarios().get(sceneIdx).getTemplates().get(tempIdx);
          if (temp.isError()) {
            fail++;
          } else {
            pass++;
          }
          if (sceneIdx == 0) {
            id = temp.getId();
          }
          final MetricGroupModel mgm = slm.getScenarios().get(sceneIdx).getMetricGroupModel().get(tempIdx);
          if (mgm != null) {
            for (final MetricModel mm : mgm.getMetricModel()) {
              for (final MetricValue mv : mm.getMetricValue()) {
                dataset.add(mv.getValue(), new BigDecimal(0.0), mv.getType(), id);
              }
            }
          }
        }
        final TemplateConfig tc = TemplateManager.getTemplateConfig(id);

        bw.write(slm.getScenarioId() + PSP);
        bw.write(id + PSP + tc.getName() + PSP + 1 + PSP + pass + "/" + fail);
        for (final Object row : dataset.getRowKeys()) {
          final Number mean = dataset.getValue((String) row, id);
          bw.write(PSP + row + PSP + mean);
        }
        bw.write(EOL);
      }
    }
  }

  bw.close();
}

From my understanding I create Beans and then put them all in a Bean Factory, to create my object that will be ready to be passed to iReport. 根据我的理解,我创建了Beans,然后将它们全部放在Bean Factory中,以创建我的对象,该对象将准备传递给iReport。

How can I put all this information into a Bean? 如何将所有这些信息放入Bean? I essentially want the bean to include the scenario/test case and whether or not it passed. 我基本上希望bean包含场景/测试用例以及它是否通过。 (This is for test automation) (这是用于测试自动化)

I tried to read your code to make aa best guess at what columns you would want, but with no context, I have no clue. 我试着阅读你的代码,以便最好地猜测你想要的列,但没有上下文,我不知道。 All the bean is a pojo, with private fields and public getters and setters. 所有的豆子都是pojo,有私人田地和公共吸气者和二传手。

Assuming there is no grouping and essentially each ScenarioLoadModel will correspond to one row in the report you would end up with a bean like this: 假设没有分组,并且基本上每个ScenarioLoadModel将对应于报告中的一行,您最终会得到一个像这样的bean:

public class ScenariaResults {

    private String id;

    private String name;

    private String load;

    private int passCount;

    private int failCount;

    public ScenariaResults(String id, String name, String load, int passCount,
            int failCount) {
        super();
        this.id = id;
        this.name = name;
        this.load = load;
        this.passCount = passCount;
        this.failCount = failCount;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLoad() {
        return load;
    }

    public void setLoad(String load) {
        this.load = load;
    }

    public int getPassCount() {
        return passCount;
    }

    public void setPassCount(int passCount) {
        this.passCount = passCount;
    }

    public int getFailCount() {
        return failCount;
    }

    public void setFailCount(int failCount) {
        this.failCount = failCount;
    }

    @Override
    public String toString() {
        return "ScenariaResults [id=" + id + ", name=" + name + ", load="
                + load + ", passCount=" + passCount + ", failCount="
                + failCount + "]";
    }

}

So basically in the code you have above you build instances of ScenarioResults and add them to a list. 因此,基本上在上面的代码中,您构建了ScenarioResults实例并将它们添加到列表中。 Once you have the list, all you need to do is create a JRDataSource: 获得列表后,您需要做的就是创建一个JRDataSource:

List<ScenarioResults> dataBeanList = ...call your method to get the list of results
//create the datasource
JRDataSource dataSource = new JRBeanCollectionDataSource(dataBeanList);

Now when designing the report in iReport it can be a little tricky to get the fields imported automatically. 现在,在iReport中设计报表时,自动导入字段可能有点棘手。 Basically first add your project with the bean to the classpath in iReports (could just point it to the bin folder or jar file`): Tools -> options -> classpath tab. 基本上首先将你的项目与bean一起添加到iReports中的类路径中(可以将它指向bin文件夹或jar文件`):工具 - >选项 - >类路径选项卡。 Now follow these steps to add the fields. 现在,请按照以下步骤添加字段。

  1. Click the following icon: 单击以下图标: 创建数据源
  2. Select the JavaBean Datasource tab. 选择JavaBean Datasource选项卡。
  3. Enter the classname of your bean. 输入bean的类名。 (ex. ScenarioResults ) (例如ScenarioResults
  4. Click Read attributes 单击Read attributes
  5. Highlight the fields you want in the report and click Add Selected Field(s) . 在报告中突出显示所需的字段,然后单击“ Add Selected Field(s)
  6. Click OK . 单击OK

Now if you want to test what the report looks like with data, and not just an empty datasource, this is where the Factory comes in. It is only for testing while using iReport. 现在,如果您想测试报告与数据的相似之处,而不仅仅是空数据源,那么这就是Factory的用武之地。 它仅用于在使用iReport时进行测试。 You need to create a class that will essentially create a dummy data set for you. 您需要创建一个基本上为您创建虚拟数据集的类。 It should look something like: 它应该看起来像:

import java.util.ArrayList;
import java.util.List;

public class ScenarioResultsFactory {

    public static List<ScenarioResults> createBeanCollection() {
        List<ScenarioResults> list = new ArrayList<ScenarioResults>();       
        list.add(new ScenarioResults("1", "test", "load", 10, 5));
        //add as many as you want       
        return list;
    }

}

Now you need to create a Datasource pointing to it in iReport. 现在,您需要在iReport中创建指向它的数据源。

  1. Next to the Datasource dropdown in the toolbar click the icon with the tooltip `Report Datasources. 在工具栏中的“数据源”下拉列表旁边,单击带有工具提示“报表数据源”的图标。
  2. Click New . 单击New
  3. Select JavaBeans set datasource . 选择JavaBeans set datasource Click Next . 单击Next
  4. For name enter ScenarioResultsFactory . 对于name,输入ScenarioResultsFactory
  5. For the Factory class you need to put the classname including package. 对于Factory类,您需要将类名包括在内。 So if the class is in the com package you should have com.ScenarioResultsFactory here. 因此,如果该类在com包中,那么您应该在这里使用com.ScenarioResultsFactory
  6. For the static method put createBeanCollection if not already there. 对于静态方法,如果尚未存在createBeanCollection
  7. Check the Use field description check box. 选中Use field description复选框。 Click Test to make sure it worked. 单击“ Test以确保其有效。
  8. Click Save . 单击Save

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 碧玉中的JRBeanCollectionDataSource如何使用 - JRBeanCollectionDataSource in jasper how to use 在Ireport中,如果我们将map作为参数从java传递给ireport,如何基于ireport字段的键访问映射值 - In Ireport if we pass map as a parameter from java to ireport how to access map values based on key of ireport field 在iReport 5.6.0中:为什么当我使用JRBeanCollectionDataSource时,表元素中缺少第一条记录? - In iReport 5.6.0 : Why is the first record missing from my table element when i use JRBeanCollectionDataSource? 如何在 Table 组件的帮助下显示 JRBeanCollectionDataSource 数据? - How to show JRBeanCollectionDataSource data with help of Table component? 如何通过iReport将数据集从主报表传递到子报表? - How to pass dataset from main report to subreport by iReport? 如何将值从Java传递到ireport mysql语句 - how to pass value from java to ireport mysql statement 如何使用iReport将参数传递给JasperReport - How can I pass a parameter to JasperReport using iReport 使用multipleDataSet绘制timeSeriex图表,或在JRBeanCollectionDataSource中传递Collection对象 - plot timeSeriex chart with multipleDataSet OR pass a Collection Object in JRBeanCollectionDataSource 如何从Java到ireport将2个或更多不同类型的参数传递给jasper? - How to pass 2 or more different type parameters to jasper from java to ireport? 如何将当前连接从 Spring 上下文传递到 iReport? - How to pass current Connection from Spring context to iReport?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM