Jump to content
Changes to the Jaspersoft community edition download ×

Dynamic column generation


HBK32

Recommended Posts

Hello,

 

Is there anyone who has idea how to generate columns dynamically? Means if result set has 10 records/columns than report will create 10 columns. If result set has 5 OR 7 records/columns then it will generate 7 columns.

 

In other way if record returns 7 OR 10 columns then it will generate columns(7 or 10) accordingly for that perticular record.

 

In short number of column is not fixed...

 

Reply as soon as possible....

 

Thanks in advance :)

Badrish.

Link to comment
Share on other sites

  • 1 month later...
  • Replies 19
  • Created
  • Last Reply

Top Posters In This Topic

hi there, i got the same probs. what i know that it is possible to generate dynamic columns using crosstab element of jasper reports. but i dont know how to use it. instead i used a templating language callde 'velocity' and i used it to generate the jrxml file dynamically and it works. the main proc is you have to genereate/write a static jrxml and then put some velocity on it. then render the jrxml usign velocity complier. you can do it using java code. the next procs are as usual for jasper. but i want to use crosstab. so if you got any tutorial/sample on that dont forget to post it here.

 

 

thanks.

tfr

Link to comment
Share on other sites

  • 6 months later...

Or you can use the JasperReports Design API and get a list of the column names from your application, iterate through them and create the fields, column header and detail elements before you compile the report.

 

I used a CSV datasource, which can be just a string with the data delimited by a comma and "/r/n" character for new lines, and set the flag useFirstLineAsColumnHeader to be true. This uses the first line as the column names, so be sure the field names match your datasource column headers, or else there will be errors.

 

Hope this helps. I did have a problem with column headers with large names being cut off, and I havent tried having more columns than can fit on a page yet. If anyone has please let me know how it went.

 

Cheers,

Jim

Link to comment
Share on other sites

  • 1 month later...

Hello tfr,

From your post i come to know that you have used velocity to generate dynamic columns in jrxml.

Can you post your sample code of it?

As i have to generate reports with dynamic columns.Also i have search on google about velocity and jaspers.But failed in getting proper way for reports.

Can you post your sample code with bit explanation so that i can get some guidance from it.

Or can you suggest me that how i can combine velocity and jrxml so that i can create dynamic reports from it.

 

Thanks in advance,

 

by,

thevoice.

Link to comment
Share on other sites

  • 2 months later...
Link to comment
Share on other sites

  • 2 years later...
  • 2 months later...

It's possible to generate a report with dynamic columns using the CrossTab element!

As a simple example, check the attachments. They are written in Portuguese, so for your understanding, the translations are:

linha = row

coluna = column

valor = value

Important to say I provide data to the report using a JRBeanCollectionDataSource.

Link to comment
Share on other sites

  • 2 months later...

All the proposed methods sounds good, I would even skip the need to use DynamicJasper, but just because I know well the JasperReports APIs.

Whatever is your trick (dynamic change of the JasperDesign with JR or DynamicJasper or a special data source to fill a crosstab (which by default has a dynamic number of columns), the result can be exported in any format without problems.

A more "recent" approach whould be to play with a table component, instead of placing dynamic elements in the detail band... even easier.

 

Giulio

Link to comment
Share on other sites

  • 5 months later...
  • 2 months later...

 Hi  djmamana  ,

 

I am using DynamicJasper-3.0.13 and jasperreports-3.5.1 . i 've written your code in my application 

FastReportBuilder drb = new FastReportBuilder();
DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
.addColumn("Branch", "branch", String.class.getName(),30) // title, property to show, class of the property, width
.addColumn("Product Line", "productLine", String.class.getName(),50)
.addColumn("Item", "item", String.class.getName(),50)
.addColumn("Item Code", "id", Long.class.getName(),20)
.addColumn("Quantity", "quantity", Long.class.getName(),30)
.addColumn("Amount", "amount", Float.class.getName(),30)
.addGroups(2) // Group by the first two columns
.addTitle("November 2006 sales report"«»)
.addSubtitle("This report was generateed at" + new Date())
.addUseFullPageWidth(true) //make colums to fill the page width
.build();

JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JasperViewer.viewReport(jp); //finally display the report report[/code]

 

 and i got this error  

 

I wrote other codes with dynamicreports but the same error persist !!!!!  i can't found out a solution in the net .

any suggestions ?!!!!!!! thanks in advance 

Code:
2012-05-08 10:50:01,578 INFO  [ar.com.fdvs.dj.core.DynamicJasperHelper] (http-127.0.0.1-8080-2) generating JasperPrint2012-05-08 10:50:01,578 INFO  [ar.com.fdvs.dj.core.DynamicJasperHelper] (http-127.0.0.1-8080-2) generating JasperReport2012-05-08 10:50:01,578 INFO  [ar.com.fdvs.dj.core.DJJRDesignHelper] (http-127.0.0.1-8080-2) obtaining DynamicJasperDesign instance2012-05-08 10:50:01,578 WARN  [ar.com.fdvs.dj.core.DynamicJasperHelper] (http-127.0.0.1-8080-2) Can't find bundle for base name dj-messages, locale it_IT, usign default (dj-messages)2012-05-08 10:50:01,859 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http-127.0.0.1-8080-2) java.lang.reflect.InvocationTargetExceptionjavax.faces.el.EvaluationException: java.lang.reflect.InvocationTargetException	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)	at javax.faces.component.UICommand.broadcast(UICommand.java:387)	at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)	at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)	at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)	at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)	at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)	at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:86)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)	at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)	at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)	at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)	at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)	at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)	at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)	at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)	at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)	at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)	at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)	at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)	at java.lang.Thread.run(Thread.java:619)Caused by: java.lang.reflect.InvocationTargetException	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)	at java.lang.reflect.Method.invoke(Method.java:597)	at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)	at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)	at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)	at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)	at org.jboss.seam.bpm.BusinessProcessInterceptor.aroundInvoke(BusinessProcessInterceptor.java:51)	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)	at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)	at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)	at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)	at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)	at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)	at com.mydomain.pkg.action.DynamicJasper_$$_javassist_seam_9.downloadPDF(DynamicJasper_$$_javassist_seam_9.java)	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)	at java.lang.reflect.Method.invoke(Method.java:597)	at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335)	at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:280)	at org.jboss.el.parser.AstMethodSuffix.getValue(AstMethodSuffix.java:59)	at org.jboss.el.parser.AstMethodSuffix.invoke(AstMethodSuffix.java:65)	at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)	at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)	at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)	... 53 more
Link to comment
Share on other sites

 Hi, guys.

As I don't wanna start a new topic to talk about the same thing, I'm posting it here.

I got the same problem. I have a report in which I got 5 fixed columns, and from the 3rd column to before the third last column I have dynamic columns...

Here it is (using excel):

http://img824.imageshack.us/img824/1691/semttulojkl.pnghttp://img824.imageshack.us/img824/1691/semttulojkl.png

The columns betwenn the red line are the dynamic columns.

A would be the group header.

B would be the detail band, which I want to reset, group by, the service number.

C would be the footer.

 

I now how to do this using groups using iReport, but without being able to dynamically add or remove the columns.

I already tried DynamicJasper, but I'm having some issues grouping the data (the first column data is shown but the others just appear in the other row, and i've tested all the GroupLayouts)..

I'm sure you guys can help me. Thanks!



Post Edited by andreem at 05/11/2012 13:25
Link to comment
Share on other sites

  • 8 months later...
  • 3 months later...

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...