简体   繁体   English

如何设置我的 Hashmap<key, list> 作为 jasper 报告的数据源</key,>

[英]How to setup my Hashmap <key, list> as a datasource for jasper reports

I have requirement to display HashMap object as jasper datasource我需要将 HashMap object 显示为碧玉数据源

Map<String, List<obj>> salesMap= new HashMap<>();    
salesMap.put("10/02/2021", List<Obj>);
salesMap.put("11/02/2021", List<Obj>);
salesMap.put("12/02/2021", List<Obj>);

and my datasource和我的数据源

private JRMapCollectionDataSource fuelSalesDataSource;

public Map<String, Object> getDataSources() {
    Map<String, Object> dataSources = new HashMap<>();
    dataSources.put("fuelSalesDataSource", fuelSalesDataSource);
    return dataSources;
}

and my jasper print和我的碧玉印花

    JRMapArrayDataSource dataSource = new JRMapArrayDataSource(
            new Object[] { fuelSalesReportInputMO.getDataSources() });

    JasperReport jasperReport = JasperCompileManager.compileReport(fuel_sales_report);

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, fuelSalesReportInputMO.getParameters(),
            dataSource);

    reportData = JasperExportManager.exportReportToPdf(jasperPrint);

I have to display the my HashMap in jasper, how to get hash map key and it list of objects in one single row?我必须在碧玉中显示我的 HashMap,如何获得 hash map 键及其单行中的对象列表?

       key column          value column          value column     
       10/02/2021         list[0].getType       list[1].getType
       11/02/2021         list[0].getType       list[1].getType.

I have field in jrmxl我在 jrmxl 中有字段

<field name="fuelSalesDataSource" class="net.sf.jasperreports.engine.data.JRMapCollectionDataSource"/>

 

How to read map in jrxml by key and its values as list?如何通过键读取 jrxml 中的 map 及其值作为列表?

Jasper report is a reporting tool, it would be possibile to use a JRMapCollectionDataSource or any other type and then reference the list, the position of an object in a list in a specific text field, but since you are creating your data in java in my opinion you should keep all this logic in java. Jasper report is a reporting tool, it would be possibile to use a JRMapCollectionDataSource or any other type and then reference the list, the position of an object in a list in a specific text field, but since you are creating your data in java in my认为您应该将所有这些逻辑保留在 java 中。 It has no sense to try to solve your problem in jasper report when you can solve it in a clean way within your java application.当您可以在 java 应用程序中以干净的方式解决问题时,尝试在 jasper 报告中解决问题是没有意义的。

This is what I would have done:这就是我会做的:

Create a class the represent the output in report在报告中创建一个 class 代表 output

@Data // lombok that generates getter/setter (if you don't have it do them in code)
public class ReportData {   
    private String date;
    private String value1;
    private String value2;
}

Then in java setup the the data然后在 java 设置数据

final List<ReportData> data = new ArrayList<>();

//Maybe add some sorting (HashMap is unsorted)?
salesMap.entrySet().stream().forEach(e -> {
    
    ReportData rp = new ReportData();
    rp.setDate(e.getKey());
    //Here you should check size of list etc 
    rp.setValue1(e.getValue().get(0));
    rp.setValue2(e.getValue().get(1));
    data.add(rp)
});

//Then now pass it to report
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, fuelSalesReportInputMO.getParameters(),
            new JRBeanCollectionDataSource(data));

Like this now in jasper report you just field names as in ReportData eg就像现在在 jasper 报告中一样,您只需在ReportData中使用字段名称,例如

<field name="date" class="java.lang.String"/>

After re-reading this, I think you're using the wrong data types for your collections.重新阅读后,我认为您为 collections 使用了错误的数据类型。 You're using:您正在使用:

JRMapCollectionDataSource which expects a Collection<Map> which is not what you have. JRMapCollectionDataSource期望Collection<Map>这不是你所拥有的。 You have a single Map which you'd like to iterate though.你有一个 Map 你想迭代它。

which you're wrapping in你包裹的

JRMapArrayDataSource which expects an Array<Map> which you're just passing in an Array[1] with your single map data collection. JRMapArrayDataSource需要一个Array<Map> ,您只需将 Array[1] 与您的单个 map 数据集合一起传递。

Assuming you want all your dates = row you need to pass in multiple maps.假设您想要所有dates = row ,您需要传入多个地图。 Something that might look like this:可能看起来像这样的东西:

data: [
   { date: "10/02/2021", data: List<Obj>) },
   { date: "11/02/2021", data: List<Obj>) },
   { date: "12/02/2021", data: List<Obj>) },
]

Now you can pass in your JRMapCollectionDataSource directly without wrapping it.现在您可以直接传入您的JRMapCollectionDataSource而无需包装它。 Now you can setup your fields as:现在您可以将字段设置为:

  • THIS = java.util.Map (if you want direct access)这 = java.util.Map(如果你想直接访问)
  • date = java.util.Date日期 = java.util.Date
  • data = java.util.List数据 = java.util.List

which will allow you to setup the report:这将允许您设置报告:

[TextField: $F{date}] [List: new JRBeanCollectionDataSource($F{data}), horizontal] [TextField: $F{date}] [List: new JRBeanCollectionDataSource($F{data}), 水平]

which should give you close to the output you're looking for.这应该让您接近您正在寻找的 output。

If this is way off the mark, sorry.如果这太离谱了,对不起。

i have fixed the issue, and as pointed out by @Petter, i changed my datasource to JRBeanCollectionDataSource from JRMapCollection...我已经解决了这个问题,正如@Petter 所指出的,我将我的数据源从 JRMapCollection 更改为 JRBeanCollectionDataSource ...

code below下面的代码

public JRBeanCollectionDataSource  getFuelSalesReportDataSource() {
    Map<String, List<DailyFuelSalesMO>> sortedMap = new TreeMap<>(dailyFuelSalesMap);
    Set<Entry<String,List<DailyFuelSalesMO>>> set = sortedMap.entrySet();
    fuelSalesReportDataSource = new JRBeanCollectionDataSource(set);
    return fuelSalesReportDataSource;
}

and jrxml, i have fields as below..和 jrxml,我有如下字段..

    <field name="key" class="java.lang.String"/>
    <field name="value[0].fuelObj.type" class="java.lang.Object"/>
    <field name="value[0].fuelObj.priceObj.price" class="java.lang.Double"/>         
    <field name="value[1].fuelObj.type" class="java.lang.Object"/>
    <field name="value[1].fuelObj.priceObj.price" class="java.lang.Double"/>

so i could able to populate in single row, with key, and its list values, in this i have two list object per key, so i could able to get those objects by value[i] for each key object in the hashmap...因此我可以使用键及其列表值填充单行,在此我每个键有两个列表 object,因此我可以通过 ZDDA7806A4847EC61B5940AABD6 中每个键 object 的值 [i] 获取这些对象。 .

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM