JasperReports newbie here. I have read the tutorial and the quick reference and read up on a number of articles regarding JR, and have now been playing around with the iReport report designer for a day or so.
I think the last major set of concepts I am choking on have to do with the relationship between chart components and their data. Although it is easy to find definitions for each of these, there seems to be very little practicle documentation showing how they relate to one another in a meaningful application.
Report Fields
Report Prameters
Report Variables
Datasets
By playing around with iReport it seems that Fields, Parameters and Variables can exist at the report-level, as well as being placed inside of Datasets. But my understanding of when something is a Field vs. Parameter vs. Variable is very fuzzy, and my understanding of how they relate to Datasets is also very shaky.
Using Datasets as a slight segue, I'm having a tough time seeing the "forest through the trees" with how chart components (such as pie charts, tabls, etc.) get "fed" or "injected with" their data.
Soo... I thought of an example that, if answered, would tie everything together for me (I believe!). Let's say I had two chart components, a text field and a pie chart. I want the pie chart to appear below the text field like so:
The author of this report is: <value supplied by the data source>
<pie chart here>
So, at "fill time" (I think I'm using that correctly...), the report will be "filled" with the name of the report's author (a String), as well as a pie chart comprised of 2 pie slices: 1 slice with a value of 75 with a label/key of "Eloi" and a 2nd slice with a value of 25 and a label/key of "Morlocks". If I am not using the correct JR terminology here, what I am trying to achieve is a fill-time pie chart with two slices: an "Eloi" slice consuming 75% of the chart, and a "Morlocks" slice consuming 25% of the chart.
If someone can explain or give code (Java/JRXML) snippets for how to set this kind of chart up, I think it will help connect all the dots and help me understand how components get filled with data. Thanks in advance for any and all help!
Think of parameters as things that the end user supplies to the report at runtime. For example, you supply a StartDate and an EndDate that will get used in a query. The start date that you are interested in is something you know, it's not provided by the data source. (There are variations on this idea: maybe your application knows things about you based on your login, and it supplies these as parameters. But again, these are things known before the report is executed.)
Think of the fields as the data that comes back from your data source. This is the stuff that you want to learn. For example, you run a query like this:
select political_group, gullibility from mytable where the_date > $P{StartDate}
Presumably you would input a value of '802701' for the StartDate and then get results like this:
$F{political_group} $F{gullibility}
Eloi 75
Morlock 25
Think of variables as a way to manipulate this raw data. They can calculate totals and subtotals as well as line-by-line calculations like string manipulation or more complex things like running totals.
Take a look at this pie chart report I posted a couple of years ago: http://mdahlman.wordpress.com/2009/05/02/limiting-pie-pieces/
It has the main ideas you want. I put the title directly into the chart rather than as a separate field. That would be a very simple change. Likewise, you could change the title to "The author of this report is: $P{TheAuthor}" and then pass that param to the report at runtime.
Using a field in the report title rather than a parameter is possible also. But typically it doesn't make sense. The fields will have many values in the data set. Which one belongs in the title? In the case above "Eloi" and "Morlock" are fields, and they really don't make sense in the report title. (You can imagine special cases, of course. You could concatenate all of the political_group values into a single string and put that in the report title. But in an overwhelming majority of cases this won't be reasonable.)
Good luck.
Related
I am planning to develop an adventure-like game.
For that I am going to have a lot of instances of classes with different texts (basicly strings).
I dont want to hardcode this many texts, so i am looking for a way to do it better.
The guy in this video ( https://www.youtube.com/watch?v=8CDePunJlck ) is using json to write text files for each class instance manually and parse them automatically into instances. That goes into the right direction.
I´m looking for more information on that, so how is this procedure called?
Its said in the video that this also works with databases?
Is there a way to design a little bit more complex stuff with things like this?
E.g. I have the case that I would like to output different texts if e.g. a local or global variable is over a treshold etc. Can I do this without hardcoding and write an own class for each of my proposed instances?
Thank you!
Your question is quite broad, and it is hard to give a definitive answer. Here are some thoughts - hope you find it helpful.
You are right that you don't want to hardcode strings. The alternative to this is storing strings as external resources, and loading them into your game at start. There are numerous ways the resource can be organized; the choice depends on your programming platform, game architecture etc. For example, you can use simple name-value approach:
AREA_1_DESCRIPTION: You stand next o a small white house.
ITEM_22_DESTRUCTION: The nasty snake disappears with a loud "Bang!"
Using JSON or XML will give you more structured storage, which can be of great help, since you can organize your texts so that it is easier to use them in the code:
<item id="375" name="Great Sword">
<short_description>A Great Sword of Darkness</short_description>
<long_description>The sword has almost black blade with some unknown runes engraved</long_description>
</item>
If your programming system can access a database, then you can do something similar and store texts in the tables; this, however, might make it more difficult to edit texts later. If you want to go this way, I would still recommend using XML or JSON to store the texts, and making the game import texts in DB on the first run.
You probably will also need some sort of simple template-handling engine to be able to re-use some strings. You can start with creating your version of Java String.format() method. Your method might take as a first argument an ID of a string in your string catalog, and use some simple placeholders for the parameters. Suppose you have the following entry in your catalog:
FIRE_GEM_ACTION: "The Fire Gem touches %% and in %% seconds it turns into ashes."
Then you can write a method that will do something like this:
int delaySeconds = 5;
String message = MyTemplateProcessor.process(FIRE_GEM_ACTION, "old map", delaySeconds);
The function will take the string from the catalog, search for the occurrences of the placeholders (%%) and replace them sequentially with the parameters, so in the message you will get: The Fire Gem touches old map and in 5 seconds it turns into ashes.
In general, I would recommend you to have a look at some systems specially designed for creation of adventure games. Inform 7 will be a good starting place: http://inform7.com/learn/
I was reading through a few active SO questions and I came across this one that reminded me of a question that I have had for a while.
In a few applications that I have done, there came a point when I had to use a view that uses category totals to display some easy to read values, such as cost, or counts, or whatever. Up until now, I have always had to find workarounds for these totals because I could not get them to show up in the dynamicViewPanels, or anything else of the like. My solutions have always been, (as David Leedy suggests in the linked question) static HTML tables displaying the counts with the category, View Navigators which display the information in repeaters, and building complex dialogs which allow the user to pick certain information to get a calculation table with the appropriate values and formulas etc.
My question is, did I overlook something in the controls that actually allow these column totals to show up in the existing view panel controls?
EDIT
Just for the sake of clarity, I am not talking about a column with a calculated value, but really the totals for categories.
There is a ready baked function that uses a ViewNavigator to get to values from category totals. That just might do the trick for you.
I have been working on an app and have encountered some limitations relating to my lack of experience in Java IO and data persistence. Basically I need to store information on a few Spinner objects. So far I have saved information on each Spinner into a text file using the format of:
//Blank Line
Name //the first drop-down entry of the spinner
Type //an enum value
Entries //a semicolon-separated list of the drop-down entry String values
//Blank line
And then, assuming this rigid syntax is followed always, I've extracted this information from the saved .txt whenever the app is started. But things such as editing these entries and working with certain aspects of the Scanner have been an absolute nightmare. If anything is off by even one line or space of blankness BAM! everything is ruined. There must be a better way to store information for easy access, something with some search-eability, something that won't be erased the moment the app closes and that isn't completely laxed in its layout to the extent that the most minor of changes destroys everything.
Any recommendations for how to save a simple String, a simple int, and an array of String outside the app? I am looking for a recommendation from an experienced developer here. I have seen the storage options, but am unsure which would be best for just a few simple things. Everything I need could be represented in a 3 X n table wherein n is the number of spinners.
Since your requirements are so minimal, I think the shared preferences approach is probably the best option. If your requirements were more complicated, then a using a database would start to make more sense.
Using shared preferences for simple data like yours really is as simple as the example shown on the storage options page.
I am currently working on a program that preforms techniques such as image segmentation along with a few others. However I have the task ahead of me of filling a segmented area (this will be a blank area) based on its surrounding pixels.
This is a lot like what photoshop likes to call content aware fill, however me being only one person am wondering the best way I could approach this type task. Also how I should start to think about getting something, obviously not as technical and robust, but similar in some sense to work.
I am not currently aware of any classes that may help with something like this but any help on this would be greatly appreciated.
You are after Inpainting. There are many ways to do it, and an Inpainting survey by Bertalmío, Caselles, Masnou, Sapiro - 2011, presents lots of results and references about them.
Around here you will also find sample results using the technique, for instance at https://stackoverflow.com/a/13666082/1832154 you will see one together with http://www.cc.gatech.edu/~sooraj/inpainting/ that gives a complete implementation. At https://stackoverflow.com/a/14295915/1832154 you can see another sample result, together with a simplistic reference to a different inpainting method.
I figured this would be something that would be fairly-well documented as its a central theme to JasperReports, however I can't find an answer for this anywhere.
What is the purpose/function/intention of a details band? Is it supposed to just be the central or core part of a report?
From another question it was pointed out to me that there is a 1:1 relationship between a details band and a record/bean provided by the JRDatasource. This revelation brings to light a few tangential questions:
It is possible to add detail bands programmatically in Java; what happens if you specify more/less detail bands (programmatically) than there are records/beans returned by your JRDataSource?
What is the relationship between a details band, and say, a page inside an exported PDF document? Does 1 details bands translate to 1 page?
What happens if you pass the JasperFillManager a null data source? Is it possible to still have detail bands?
I don't like to ask multiple questions at a time, but these are so similarly-related I'd rather do it all at once than clutter SO with multiple nearly-identical questions. Thanks in advance.
The details band is indeed the band where each element of the data source is reported. The report engine automatically iterates over the data source and inserts data into the template of the detail band with respective element of the data source.
You may of course have several elements on a single page. According to the properties of the band (split allowed, height, etc.), the paging will be handled by Jasper Reports automatically, and it's the engine that will thus decide how many elements are printed on each page, when to go to the following page, etc.
The details band is not printed if you have nothing in the data source. The printing of the other bands depend on the parameters of the report.
The purpose of the detail band is to provide you with a model where you place and configure report elements. I don't think the relationship between a bean and detail band is one to one, since you can place many bean property elements in one bean. So, i would say the detail band is tightly tied to a collection of report bean elements.
One difference though, is the fact that detail band's functionality is irrelevant to how many beans/records you provide through a JRDataSource. The detail band will iterate through all of them until the data source is "consumed".
Furthermore, i personally find it very useful that the detail band allow you to iterate through a collection of bean properties. So, placing several properties in a detail band will iterate through all of them, before proceeding forward.
If you pass a null as DataSource you wont get any data on the report, and only static text will show up.
As I remember it, you can see the details band as the "model" for a row/record in the report. All elements you put inside the detail band will be repeated for each record provided by the JRDatasource.
For example, your details band might have two text fields, one with the value ${companyName}, and one with the value value ${revenue}.
If you now pass your report three rows, then "companyName" and "revenue" will be evaluated for each of them, and you may get something like:
|Apple | $1000,000,000|
|Microsoft | $500,000,000|
|My amazing company | $12|
I.e., the detail band contents has been repeated three times.
So:
- As you see you probably only need one details band configured. Not sure if it's allowed in the JRXML to have multiple ones.
- There is no relationship between the details band and a page. Pagination is handled separately.
- Not sure, to be honest. My best guess is either an exception or you get no rows where the detail band is supposed to be rendered.
You misunderstand what a detail band is.
The detail band of your report will be printed for each of the elements in your datasource. If you use a database datasource, the detail will be printed as many times as rows has your resultset. If you use a JRBeanCollection datasource, the detail band will be printed as many times as items has your collection.
To answer your questions:
You can't specify more than one detailband, it's illegal. You can
use subreports for that purpose.
The report will output as many detail bands as it can per page. On the otherhand, you can make the detail band the same height your desired report
output will be (minus header/footer/etc height). If you want to print the report in an A4 paper you
can make your detail band 297 mm high (considering you have no other bands). Then each detail band will be
printed in a separate page.
If you pass an empty datasource, the report will be generated with
no pages.
The information you have is correct, you've just had a slight misunderstanding of what it means.
The 1:1 relationship is at fill-time; Once the report has been filled there will be exactly 1 detail band for every record in the dataset. When you are designing the report you only add the band once, but that one band is repeated over and over again when the report is filled.
You can add as many detail bands as you wish. Each of the bands have a 1:1 relationship with the dataset records, and all of the detail bands are filled before moving on to the next record (i.e. If you have three bands A, B, and C; Their order in the report will be ABCABC... not AAA...BBB...CCC...).
It is possible to add bands programmatically, but the important point to note is that you are adding bands to the report design, not to the completed report. So just as with a jrxml design, you add the band once and it gets repeated for each record. Check this example.
There isn't a relationship between report pages and detail bands. The report filler will try to put as much onto a page as possible while respecting the splitType of each band as well as other report properties.
Is passing a null datasource is allowed? In any case the 1:1 relationship is still valid: A dataset with 0 records produces 0 detail bands in the report.