How to pass a list of objects from a Java application to the sub-report? - jasper-reports

I have a report with one bar chart and one table. these two elements are filled by the list of objects which is passed from my Java application to the report.
Now I want to put these two elements in two different sub-reports, but I do not know how I can pass the list of objects to the sub-reports. I have added the fields of each object as a field in master report and as a parameter in sub-report, but it does not work.
Can anyone help me to solve this problem?

Something like this:
Pass collection into main report as parameter and use this parameter in DataSource for subreports:
// java-code for main report
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(listOfObject);
Map<String, Object> params = new HashMap<String, Object>();
params.put("myList", listOfObject);
jasperPrint = JasperFillManager.fillReport(jasperReport, params, ds);
// end java-code
(may be
JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource())
if you don't need to display data in main report)
Define in main.jrxml report parameter myList as java.util.Collection
Add subreports into main report (into summary band)
Set in main report for subreports properties:
Connection type = Use a datasource expression
Data Source Expression = new JRBeanCollectionDataSource($P{myList})
Define in subreports desired field (based on listOfObjects).

Related

ActiveReports 8 - Section Report With Main Report and SubReport

I am developing a Section report using ActiveReports 8. I have a Main report with a detail line that has five text boxes bound to data. The detail line also has two unbound text boxes that are defined but are not bound to data. I am attempting to populate these two unbound fields by data coming from the subreport.
I am part way there BUT the unbound fields are getting populated one detail line late. If this makes sense?
Have tried using different events ... cannot seem to find the right combination.
You can get data from the SubReport by creating a property or two in the SubReport and access the property values in the main report's Detail Section's Format event and assign it to the unbound TextBoxes. Something like:
private void detail_Format(object sender, EventArgs e)
{
subReport1.Report = rpt;
rpt.Run();
textBox3.Text = rpt.SubReportValue.ToString();
}
private void MainReport_ReportStart(object sender, EventArgs e)
{
rpt = new ChildReport();
}
We have also replied to you on the ActiveReports forum post with a demo application - http://arhelp.grapecity.com/groups/topic/activereports-8-section-report-with-parent-and-subreport/

Crystal report sub-report links

I am trying to pass a parameter from the main report to a sub-report. I know this can be done from 'change sub-report links', but I assume it only works if the data type are the same for these parameters. Can I still do it if the parameters are of different types? i.e. the main report parameter is of type 'datetime' and sub-report is of type 'string'(which is in fact a string representing datetime). The sub-report calls a store procedure in the DB, which easier to deal with if the parameter is of type string.
I tried create a 'stepping variable'(say call it 'datetimeString') on the main report, to format the datetime parameter into string, then link this 'datetimeString' to the string parameter in the sub-report.
The above does not give me errors when configuring, but the funny thing is that now when i try to preview the main report, the sub-report does not show any data, store procedure in the sub-report is not executed. In the same preview session(without updating the parameters passed in), if i double click on the sub-report to view detail on the sub-report itself, the sub-report opens with data and the store procedure within the sub-report gets call.
It seems like the sub-report is not invoked if its parameter is not linked straight from a main report parameter.
Please let me know if you could suggest anything else to try. Thank you!

JasperReports customize series labels based on a value other than the Category value

I have a JasperReport with a line chart that I need to display labels on, but I want them to display conditionally for each data point. I've created the customizer class to actually display the value, but I want to use a different field than the value field to decide if it should display or not.
Basically in my DataSet I have 3 fields:
Date: (Category Axis)
Value: (Value Axis)
PrintValue: Boolean field
I want to print the Value in the label only when PrintValue=true
One solution would be to override one of the methods implemented by JRDefaultScriptlet in a scriptlet class, then set the value of "PrintValue" in any manner you desire. Then in your chart dataset you should be able to reference $V{PRINTVALUE} as an operand.
I'm going to assume your using iReport for your report design.
Open your report in iReport and click the report name (Top most node in the report Inspector)
Set the Scriplet class to your package name and class, e.g., org.company.scriptlets.MyChartClass
Declare your report variable in iReport. In this case "PRINTVALUE" would be the variable name.
Create a java class that overrides a scriplet method, like beforeDetailEval, e.g.,:
#Override
public void beforeDetailEval() throws JRScriptletException {
super.beforeDetailEval();
...
this.setVariableValue("PRINTVALUE", true);
}
Since you want to display the category label conditionally for each tick mark, you'll probably need to use a Map of key/val pairs. key would be category label, value would be true/false for "PRINTVALUE". Note I did NOT illustrate this in the sample code above but its entirely possible. Just declare your report variable as a Map, e.g., HashMap<String, Boolean> hm.
You'll need to add your new scriplet class to the Classpath in iReport.
Hope this helps or at least gets you started.

how to use parameter in subreport with iReports?

I have this problem. I'm trying to do a report using iReport.
This is my master query:
SELECT r.idreq AS Id, d.denom AS Dependencia, t.denom AS TipoProceso, r.docproceso AS DocProceso
FROM requerimiento r, cotizacion c, dependencia d, tipoproceso t
WHERE r.idreq=c.idreq AND r.iddepen=d.iddepen AND r.idtipoproc=t.idtipoproc
AND c.estado=true AND r.idreq=$P{pIdReq}
As you see, there exists pIdReq parameter in the master report, now in the master detail I've added a subreport, that subreport has this query call SPCuadroComparativo3($P{pIdReq}).
I wanna know how can I get linking both parameters? Or how can I use the parameter from master report?
Create the parameter in main Report pIdReq.Drag and drop a sub Reprot.Go to sup Report properties click on PARAMETERS it will ask for add sup report parameter.So add the parameter name pIdReq then expression map with main Report parameter i.e pIdReq.Go to sub report create the parameter name same as pIdReq. you can use now the sub report parameter in sub report query which is linked with main report parameter pIdReq by $P{pIdReq}
*create the main report parameter by name pIdReq.
*Goto properties of subReport configure Parameters.
*Add the same Parameter Name in sub report.
*Put the sub report parameter in sub report query
.

JasperReports: unrecoverable multipage subreports

For example i have Master report that contains several multipage subreports. I want them to be displayed in order that is set in master report (prints the first subreport and then prints second). When i add these subreports into master datail band, they cover each other.
How can i do such thing?
Thank you
Have you tried putting each subreport in its own detail band? Or, if you're putting the subs in an array, include another variable like "reportName" or "reportNumber" and group the subreport detail bands by that variable, so each time the variable changes a new group is created for the new subreport. Doing it that way would also let you put a group header for each subreport which you could then customize with variables and/parameters. Just a thought.
I found the satisfied solution:
i fill all my reports separately, but export them by JRPdfExporter:
List jasList = new ArrayList();
jasList.add(jp_1);
jasList.add(jp_2);
jasList.add(jp_3);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasList);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "batch.pdf");
exporter.setParameter(JRPdfExporterParameter.IS_CREATING_BATCH_MODE_BOOKMARKS, Boolean.TRUE);
exporter.exportReport();
They will be ordered in list order and bookmarks will be generated