问题
I'm using Jasper Reports and iReport to generate the reports of my app. I need to show images in my report depending on a query on my database. The images are plans and have other fields inside representing measurements, etc.
I.E. With two images: IMAGE1 and IMAGE2 linked to Field_ONE and Field_TWO.
If Field_ONE is != null then i whould like to show IMAGE1 and the fields inside the image in the document;
If Field_TWO is != null and Field_ONE is equal to null then i want to show IMAGE2 in the place where previously showed IMAGE1
If Field_TWO is != null and Field_ONE != null then i want to show IMAGE1 and IMAGE2 side by side.
Note that i could have 5 or 6 images, so it would be really difficult to cover all the possibilities without leaving blank spaces between them only using tag.
In short: i need something like the layout in Android where i can add the plans dynamically depending on the fields and generate the images and their associated fields in the order i added the plans.
I hope you can help, thank you!
Note: I could have more than one image of the same type depending on the result of the report, so it's almost impossible cover it with layers
回答1:
Layers (Static) Solution
- Divide the problem into cases. For example, case 1 show image 1, case 2 show images 2 and 3, case 3 show image 4 etc...
- For i to n (for n cases)
- Create all the elements for case i.
- Add a new layer, Layer i. Go to Window > Layers if the Layers window is hidden.
- Select the elements added, right click and
Send to layer. Choose layer i. - Right click on the layer in the Layers window and update the Print When Expression. For example,
$F{Field_ONE} != null - Repeat. FYI, you can click on the eye on the layer (in the Layers window) to hide the applicable elements to hide the clutter.
- Done, each design you made for each case will only appear when the Layer's print when expression yields true.
Subreport (dynamic) Solution
- Create a report, lets call it subreport.
- Set the width and height of the page to be the height and width of your image section for a given record.
- Set the all of the page margins to 0
- Change the column of the report to the number of images you'd like to have in a single row.
- Change the print order to 'Horizontal'
- Add N parameters for the N field that needs to be associated with an image.
- Remove all the bands in the page except for the detail band.
- Write a dummy query that will return an image name depending on whether a given field is null or not. For example,
select 'Ascent.jpg' as image from dual where $P{FIELD_1} is not null. You need to do this for each field andunion allthe select statements. - In the detail section, add an Image element and set the value to be the path to your picture using the
imagefield from the query above. For example,"C:/WINDOWS/Web/Wallpaper/"+$F{IMAGE}. - Go to your original report and add the subreport to your detail section.
- Link the fields in the original report to the parameters, created in #2, in the subreport.
I didn't want to post this solution until I tested it. I can post my sample, which has many lines, if there is something unclear. The reason you'll need a subreport is because your main report has 1 column and fills vertically but you need the images to fill horizontally across multiple columns. For each field that is not null, the subreport query will return a record in the statement order. The record will then be filled across the columns.
来源:https://stackoverflow.com/questions/11009550/jasper-reports-showing-images-dynamically-depending-on-field-values