You are on page 1of 5

Integrate Chart Image into Jasper Report Part - 2 | Ramki Java Blog

http://www.ramkitech.com/2012/09/integrate-chart-image-into-jasper.html

Discuss Java Concepts


Home All Posts Resume Contact Me About Me

Google

In my previous post we discussed how to Integrate the Charts into Jasper Reports using JFreeChart API in Jasper Report. In this post there is another way to embeds the charts into Jasper Reports. This method, we need to generate/create the chart using any 3rd party Java Lib and convert into Image Object or stored into File-system, then just insert the Image into Jasper Report. here no need to use chart functionality in Jasper Report. because these chart are limited functionality.

179674

I'm on

Steps: 1. Create the chart using any Java libraries 2. Convert the Chart into BufferedImage or Stored into files 3. pass the Image into Jasper Report. ramkicse@gmail.com 1. Create the chart using any Java libraries In this step we going to create the chart. here i m using jopenchart Java library to create the chart. In their website tutorial section contain some sample codes. so i used these codes. and download the jopenchart lib from here. 2. Convert the chart into BufferedImage or Stored into files In jopenchart have the in-build function for store chart into the file. ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home") + "/chat.png"), c); and if u want into in-memory Image object then use render() method.
Rama krishnnan E P
view plain print ?

Mobile : +918605429395 Skype Id: ramkicse18

01. 02. 03. 04.

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); DefaultChart chart = ....; Graphics2D gd=bi.createGraphics(); chart.render(gd);

In above code we create empty BufferedImage object. the get the Graphics2D object from the BufferedImage and pass the Graphics2D into chart render() method. so when render() method is called, the chart is write into BufferedImage object. 3. Pass the Image into Jasper Report We need to create the Jasper Report. this time we are not using chart function in Jasper reports. we using Image component of jasperreport. We have 2 options are there, pass the Image Object or Pass the Path String object where image is stored in File-system. here first i m using Image object. we use parameter to pass into jasper report. so first create the new Parameter in Jasper Report and named "chartImage" and Parameter Class "java.lang.Object". drag the Image Component and set the properties. Image Expression and Expression Class like Image Expression == > $P{chartImage} Expression Class == > java.awt.Image

195 have me in circles

View all

Rama krishnnan E P
29 vdeos | 573 suscriptores

Subscribe

Martin Murciego

1 de 5

06/03/2013 03:12 a.m.

Integrate Chart Image into Jasper Report Part - 2 | Ramki Java Blog

http://www.ramkitech.com/2012/09/integrate-chart-image-into-jasper.html

Rama krishnnan E P If we use stored Image File then change the Parameter class to java.lang.String and Image Expression Class to java.lang.String. here Image Expression class is decided the Jasper Report. how we add the Image. If u put "java.awt.Image" as a Expression class then we are passing the Image object. and jasper report can understand. If u mention "java.lang.String" then we pass as String that pointing to Image file. (png,jpg,gif,..). Code: DemoBean.java
view plain print ?

View my complete profile

01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.

@ManagedBean @SessionScoped public class DemoBean { int width = 640; int height = 480; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); public DemoBean() { int[] quadr = {0, 1, 4, 9, 16, 25, 36}; int[] exp = {1, 2, 4, 8, 16, 32, 64}; double[] columns = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; // Creating a data set array DefaultDataSet[] ds = new DefaultDataSet[3]; // Filling all DataSets ds[0] = new DefaultDataSet(ChartUtilities.transformArray(new int[]{0, 6}), ChartUtilities.transformArray(new double[]{0.0, 6.0}), CoordSystem.FIRST_YAXIS, "Linear Growth"); ds[1] = new DefaultDataSet(ChartUtilities.transformArray(quadr), ChartUtilities.transformArray(columns), CoordSystem.FIRST_YAXIS, "Quadratic Growth"); ds[2] = new DefaultDataSet(ChartUtilities.transformArray(exp), ChartUtilities.transformArray(columns), CoordSystem.FIRST_YAXIS, "Exponential Growth"); String title = "Growth Factor Comparison";

JSF + JPA + JasperReports (iReport) Part 1 Hi in this post we will see the overview of JasperReports and how to integrate into JSF application. JasperReports is the world's mo... Virtual Host + Apache httpd server + Tomcat + mod_jk connector In my last post ( Virtual Host in Tomcat ) we discussed about how setup the virtual host in Tomcat. Its cost effective technique because on... JSF + JPA + JasperReports (iReport) Part 2 In this post is a continuation of Jasper Report Part 1 . here we will discuss about some advanced jasper report concepts like passing com... How to do SSH Tunneling (Port Forwarding) Screen-cast In this post we will see how ssh works?, what is SSH tunneling? what is important of ssh tunnels and how to setup the ssh tunnel. When SS... Running Multiple Tomcat Instances on Single Machine In this post we will see how to run multiple tomcat instances on single machine and under single user account. We first see the tomcat di...

DefaultChartDataModel data = new DefaultChartDataModel(ds); data.setAutoScale(true); DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); c.addChartRenderer(new LineChartRenderer(c.getCoordSystem(),data), 1); c.setBounds(new Rectangle(0, 0, width, height)); Graphics2D gd=bi.createGraphics(); c.render(gd); // // // // // // /* if ur using Image file then use these code */ try { ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home") + "/chart.png"), c); } catch (Exception e) { e.printStackTrace(); }

} public String pdf() throws JRException, IOException {

String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports /Charts.jasper"); Map parameter = new HashMap(); parameter.put("chartImage", bi); JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, parameter, new JREmptyDataSource(1) ); HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getE xternalContext().getResponse(); httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf"); ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream); FacesContext.getCurrentInstance().responseComplete();

68. 69. 70. 71. 72. 73. 74. 75. 76.

CDI (5)
return null; } }

JasperReports

Java (9) Java EE 6 (10)


(5) JSF (4)

JSF 2.0 (7)


Download the Sample Project from my GitHub

load balancer (5) mod_jk (6) multiple_instances (6)

2 de 5

06/03/2013 03:12 a.m.

Integrate Chart Image into Jasper Report Part - 2 | Ramki Java Blog

http://www.ramkitech.com/2012/09/integrate-chart-image-into-jasper.html

Check the screen-cast for better understanding. Screen cast:

screencast (22)
Tomcat (6) clustering (5) tomcat

2013 (2) 2012 (13) December (1) November (1) October (2) September (2) Integrate Chart Image into Jasper Report Part - 2 Integrate Charts in Jasper Reports +JSF 2.0 June (2)
0

Comments (3)
Sort by: Date Rating Last Activity intellectsoft 24 weeks ago Hi! Thanx for this great step-by-step guide. The video you posted at the end of the article helps to understand it even better.
Reply

Login

April (2) March (1) February (1)


Report

January (1)
Reddy Sekhar Reddy 18 weeks ago hi this is Sekhar can u please help me how to get jaspercharts from javaprogram? thank u in advance
Reply

2011 (9) 2010 (7)

Report

Ramki 53p 18 weeks ago Hello Sekhar, download the Jasper Report Lib. there contain enough lib to integrate. there are JRViewer. u can use with Swing
Reply

+1

Report

Post a new comment


Enter text right here!

Comment as a Guest, or login: Name


Displayed next to your comments.

Email
Not displayed publicly.

Website (optional)
If you have a website, link to it here.

Subscribe to

Submit Comment

Posted by Rama krishnnan E P at Wednesday, September 12, 2012


+3 Recommend this on Google

Tags : charts, iReport, JasperReports, screencast

3 de 5

06/03/2013 03:12 a.m.

Integrate Chart Image into Jasper Report Part - 2 | Ramki Java Blog

http://www.ramkitech.com/2012/09/integrate-chart-image-into-jasper.html

Newer Post

Home

Older Post

The last comments for

JSF JPA JasperReports (iReport) Part 2 ian Hi, yes i have getters and setters, the error from the firstName is now working but when i added the...
4 hours ago The last comments for

JSF JPA JasperReports (iReport) Gabriel Instalacion rapida y sencilla sudo apt-get install libapache2-mod-jk
6 hours ago The last comments for

JSF JPA JasperReports (iReport) Part 2 Ramki 53p Hello Ian, Thanks, yes i ll help. So what is ur bean variable 'firstName'? and r u...
23 hours ago

ian Good Day! Great Tutorial! I watched your video on youtube. Anyway can you help me? I followed your tutorial...
23 hours ago The last comments for

Running Multiple Tomcat Instances on Single Machine Ramki 53p Thanks. mani i ll update the blog
1 day ago

Comments by IntenseDebate

Posts Comments

Enter your email address:

Delivered by FeedBurner

with Google Friend Connect

Members (21) More

Already a member? Sign in

4 de 5

06/03/2013 03:12 a.m.

Integrate Chart Image into Jasper Report Part - 2 | Ramki Java Blog

http://www.ramkitech.com/2012/09/integrate-chart-image-into-jasper.html

Template images by Jason Morrow. Powered by Blogger.

5 de 5

06/03/2013 03:12 a.m.

You might also like