Jasper Reports: showing images dynamically depending on field values

一曲冷凌霜 提交于 2019-12-01 10:33:52

问题


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

  1. 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...
  2. 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.
  3. Done, each design you made for each case will only appear when the Layer's print when expression yields true.

Subreport (dynamic) Solution

  1. 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'
  2. Add N parameters for the N field that needs to be associated with an image.
  3. Remove all the bands in the page except for the detail band.
  4. 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 and union all the select statements.
  5. In the detail section, add an Image element and set the value to be the path to your picture using the image field from the query above. For example, "C:/WINDOWS/Web/Wallpaper/"+$F{IMAGE}.
  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!