kashar Posted January 7, 2008 Share Posted January 7, 2008 I have created a jrxml file that creates a chart in the detail band.when i run it(using empty datasiurce), it shows the chart properly. Now i need to create charts dynamically in the title band through code i.e while the report is getting filled.for that i tried writing a scriptlet which creates charts through code and writes/draws them to a pdf file.this is working properly. but i need to write/draw these charts into the report which is shown in the iReport JasperViewer. i am not finding any way to get the outputstream for the report that is shown in the iReport JasperViewer so that i can write to it. I also thought that built-in parameter XML_DATA_DOCUMENT might be of some use but using it in the code gives error as parameter not found XML_DATA_DOCUMENT.Can you please help me in finding how to draw charts onto the report shown in the iReport JasperViewer through code. the jrxml file and the scriptlet file are attached here. they can be executed eith empty datasource. [file name=report_2.jrxml size=3709]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/report_2.jrxml[/file] Post edited by: kashar, at: 2008/01/07 07:43 Scriptlet code:-import net.sf.jasperreports.engine.*;import org.jfree.chart.JFreeChart;import org.jfree.chart.ChartFactory;import org.jfree.chart.plot.PlotOrientation;import org.jfree.data.category.DefaultCategoryDataset;import com.lowagie.text.*;import com.lowagie.text.pdf.*;import java.io.*;import java.awt.*;import java.awt.geom.*; public class report_2Scriptlet extends it.businesslogic.ireport.IReportScriptlet {// int y = 500;/** Creates a new instance of JRIreportDefaultScriptlet */public report_2Scriptlet() { } /** Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */public void afterColumnInit() throws JRScriptletException{ super.beforeColumnInit();}/** End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */public void afterDetailEval() throws JRScriptletException{ super.afterDetailEval();}/** End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */public void afterGroupInit(String groupName) throws JRScriptletException{ super.afterGroupInit(groupName); showChart();}/** End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */public void afterPageInit() throws JRScriptletException{ super.afterPageInit();}/** End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */public void afterReportInit() throws JRScriptletException{ showChart();}/** End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */public void beforeColumnInit() throws JRScriptletException{ }/** End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */public void beforeDetailEval() throws JRScriptletException{ }/** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment*/ /** End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */public void beforeGroupInit(String groupName) throws JRScriptletException{ }/** End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */public void beforePageInit() throws JRScriptletException{ }/** End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! *//** Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */public void beforeReportInit() throws JRScriptletException{ } public JFreeChart showChart() throws JRScriptletException{JFreeChart barchart = null ;try{ System.out.println("in showchart.."); System.out.println("this object.."+this); Document document = new Document(); PdfWriter writer; writer = PdfWriter.getInstance(document, new FileOutputStream("temp1.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(150, 150); Graphics2D g2d = tp.createGraphics(150,150, new DefaultFontMapper()); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(Integer.valueOf(25), "English", "125"); dataset.setValue(Integer.valueOf(35), "Maths", "125"); dataset.setValue(Integer.valueOf(40), "Science", "125"); barchart = ChartFactory.createBarChart("Marks chart","125", "", dataset,PlotOrientation.VERTICAL, false, false, false); barchart.setBackgroundPaint(java.awt.Color.white); System.out.println("chart created.."); Rectangle2D r2d = new Rectangle2D.Double(0, 0, 150, 150); barchart.draw(g2d, r2d); g2d.dispose(); cb.addTemplate(tp,0, 500); cb.addTemplate(tp,0, 300); System.out.println("chart printed.."); document.close();//ChartUtilities.writeChartAsJPEG(fos,barchart,400,500); }catch(IOException ie){}catch(DocumentException de){}return barchart;}/** End EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */ }Post edited by: kashar, at: 2008/01/07 07:45 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now