Jump to content
We've recently updated our Privacy Statement, available here ×

How to calculate % over total calculations?


gowthamsenmca

Recommended Posts

Hi,

I have a requirement where I would need to perform calculation as % over total.

Here is the sample data format I am looking for. 

ProductTypeSales% over Type Sales
P1G11025%
P1G22050%
P1G31025%
  40100%
P2G43030%
P2G52020%
P2G65050%
  100100%

I tried multiple ways to reach this layout. But I am not succedded. I tried using Grouping over details band. I also tried using Cross tab.

Any suggestions or help is very much appreicated.

Best Regards

Gowtham

Link to comment
Share on other sites

  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

Hi, 

What I suggest is that create a scriptlet which runs the query same as in the report but just gathers the total based upon the same grouping , now the grouping can be tackled in the scriptlet using the Product code. So in the scriptlet you would sum the sales for all the types of  a particular product and returm it to the report.

So this will allow you to use, the totals in th detail band and so in turn you can calculate percent over sales. If you have doubts using scriptlet you can go through 
http://ankurthetechie.blogspot.in/2013/12/jasperScriptletIntegration.html

Let me know if some clarifications are required.

Thanks,
Ankur Gupta

Link to comment
Share on other sites

Thank you Ankur for your response. I have couple of questions on the way scriptlets work.

 

1. There are some methods beforeDetailAvail, BeforeReportInit(), AfterReportInit() and so on. I have defined system.out.println(). But when I run the report, the output is not being deisplayed in the report by default. My understanding is that, these default methods will be called whenever we define a scriptlet and assign to the report.

 

2. After using scriptlet function, we run the report first time. Assuming that the values appear correctly. When we referesh the report to fetch latest data, does scriptlet uses previously saved values?

 

Please share your thoughts on this. Thanks a lot!

Link to comment
Share on other sites

Hi,

First of all let me start with your first doubt, the methods beforeDetailAvail, BeforeReportInit(), AfterReportInit() etc. are bascically the function which could be included in any scriptlet.These functions are fired when the Jasper Print object is created, and it is created when a JRXML runs.

You can use the System.out.print inside any of these functions or user defined function but remember that the System.out.print prints the value in the console, so if you do use System.out.print in the scriptlet then it would definitely print the values in the REPORT OUTPUT window.

For the second query if you refresh the report and you assuming that you have changes in the database, and the data passed to the scriptlet changes then the values are refreshed as well. For the override functions of the scriptlet, as I have stated above that it fires only when the JRXML runs, so refreshing the report causes this scenario to occur and hence the scriptlet value do refresh when report is refreshed.

Hope this explains your doubts.

I will create a video using the same sample I have included, as well as provide the java file, for your better understanding.Will place the link to video here.

Thanks,
Ankur Gupta

Link to comment
Share on other sites

Hi,

if you use the System.out.print in the scriptlet it will not print it in the Report anywhere, it will show up in the Report Output window, even if the report runs and says No pages to display .

The Report Output can be seen by Window --> Report Output 

Doing this you can see the window and if you have use the following function in the  scriptlet :

@Override
    public void beforeReportInit() throws JRScriptletException {
        System.out.println("This is the test Of sysout");
    }
    
Then when  you run the report including the scriptlet  then whether or not the report produces any values, but you can see the text This is the test Of sysout  in the Report Output window as soon as the report is initiated.
Hope that this explanation helps.
 
Regards,
Ankur Gupta
Link to comment
Share on other sites

I looked at the Report State. Somehow its not displaying the information in the Console section in ReportState Pane. I raised a issue with Jasper Soft Support Team on the same.

 

One more last quesiton I would like to check with you. Can a script returns data collection and multi dimensional array? I would like to know if I can use the result of scriptlet as a source dataset for Charts. This would help in dealing with complex calcuations.

 

Best Regards

Gowtham

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...