Hello all,
I am creating bar charts using jfreechart-1.0.13.jar and jasperreports-3.0.0.
Also I am using iReport 3.0.0 for designing the report. I have the following
jar files in the lib folder of the application
[i]jcommon-1.0.16.jar
jfreechart-1.0.13.jar
jfreechart-1.0.13-experimental.jar
jfreechart-1.0.13-swt.jar
itext-1.3.1.jar
jasperreports-3.0.0.jar[/i]
I am using JFreeChart.createBufferedImage for getting a java.awt.Image file and
then using JasperRunManager.runReportToPdf(String, Map, Connection) to write
the image to a PDF file. I could successfully generate and store the chart
image file in my local machine. But when I create a PDF with chart, I am
getting a
black square. I tried to edit it using MS paint by inverting the color, the
image is shown as a not~so~clear chart. Is it a problem with background color?
Using iReport, I have set white as background and black as foreground for the
image element. The codes for my java file and jasper is shown below. Please
tell where I am wrong. I have already spent two days on this. It is very
urgent. Thanks.
public byte[] generateGraphicalBarReport(long institutionId, String
finYear) throws Exception{
byte[] report = null;
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
JasperReport jasperReport;
JasperPrint jasperPrint;
Map<Object,Object> params=new HashMap<Object,Object>();
String chartborder = "";
path=servlet.getServletContext().getRealPath(separator+"WEB-INF"+separator+"jasper"+separator+"GraphicalReport.jasper");
try {
connection=DatabaseLookup.getInstance().getConnection("MYCONNECTION");
DefaultCategoryDataset data = new DefaultCategoryDataset();
preparedStatement = connection.prepareStatement
("select * from hmis.\"MY_DB_FUN\"(?,?)");
preparedStatement.setLong(1, institutionId);
preparedStatement.setString(2, finYear);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
data.addValue(new Integer(resultSet.getInt("TOTAL_NO")),
resultSet.getString("FINANCIAL_YEAR"), "TOTAL_NO");
data.addValue(new Integer(resultSet.getInt("FILTERED_NO")),
resultSet.getString("FINANCIAL_YEAR"),
"FILTERED_NO");
}
JFreeChart chart
=ChartFactory.createBarChart3D(chartborder,"Financial Year","No of persons",
(org.jfree.data.category.CategoryDataset)data,PlotOrientation.VERTICAL,true,true,true);
//The following code creates the chart image chart.jpg at d:\
ChartUtilities.saveChartAsJPEG(new java.io.File("d:\\chart.jpg"),
chart, 500, 300);
java.awt.Image img=chart.createBufferedImage(700,325);
//The following code creates the chart image chart1.jpg at d:\
try {
BufferedImage bi = (BufferedImage) img; // retrieve image
File outputfile = new File("d:\\chart1.jpg");
ImageIO.write(bi, "jpg", outputfile);
}
catch (IOException e){
}
//The following code opens a PDF file with a black colored
square. But when the image was copied to
//MS Paint and color inverted, it showed a not so clear bar chart.
params.put("CHART_IMAGE",img);
report = JasperRunManager.runReportToPdf(path,params,connection);
response.setContentType("application/pdf");
response.setHeader("content-disposition","inline");
response.setContentLength(report.length);
ServletOutputStream servletStream = response.getOutputStream();
servletStream.write(report,0,report.length);
servletStream.flush();
servletStream.close();
//The following code creates a PDF file C:\simple_report.pdf with
a black colored square.
//But when the image was copied to MS Paint and color inverted,
it showed a not so clear bar chart.
jasperReport =
JasperCompileManager.compileReport("C:\\GraphicalReport.xml");
jasperPrint = JasperFillManager.fillReport(jasperReport, params,
connection);
JasperExportManager.exportReportToPdfFile(
jasperPrint, "C:\\simple_report.pdf");
}
catch (Exception e){
System.out.println("Error-->generateGraphicalBarReport "+e);
throw new Exception(e);
}
finally{
DatabaseLookup.closeConnection(connection);
}
return report;
}
[b]/WEB-INF/jasper/GraphicalReport.jasper[/b]
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
name="GraphicalReport"
columnCount="1"
printOrder="Vertical"
orientation="Portrait"
pageWidth="595"
pageHeight="842"
columnWidth="535"
columnSpacing="0"
leftMargin="30"
rightMargin="30"
topMargin="20"
bottomMargin="20"
whenNoDataType="NoPages"
isTitleNewPage="false"
isSummaryNewPage="false">
<property name="ireport.scriptlethandling" value="0" />
<property name="ireport.encoding" value="UTF-8" />
<import value="java.util.*" />
<import value="net.sf.jasperreports.engine.*" />
<import value="net.sf.jasperreports.engine.data.*" />
<parameter name="CHART_IMAGE" isForPrompting="false" class="java.awt.Image"/>
<queryString><![CDATA[select CURRENT_DATE]]></queryString>
<field name="date" class="java.sql.Date"/>
<background>
<band height="0" isSplitAllowed="true" >
</band>
</background>
<title>
<band height="50" isSplitAllowed="true" >
<staticText>
<reportElement
x="50"
y="7"
width="440"
height="40"
key="staticText-1"/>
<box></box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="Helvetica-Bold" size="14" isBold="true"/>
</textElement>
<text><![CDATA[GRAPHICAL REPORT]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="0" isSplitAllowed="true" >
</band>
</pageHeader>
<columnHeader>
<band height="0" isSplitAllowed="true" >
</band>
</columnHeader>
<detail>
<band height="300" isSplitAllowed="true" >
<image evaluationTime="Now" hyperlinkType="None"
hyperlinkTarget="Self" >
<reportElement
x="30"
y="10"
width="470"
height="280"
key="image-1"/>
<box></box>
<graphicElement stretchType="NoStretch"/>
<imageExpression
class="java.awt.Image"><![CDATA[$P{CHART_IMAGE}]]></imageExpression>
</image>
</band>
</detail>
<columnFooter>
<band height="0" isSplitAllowed="true" >
</band>
</columnFooter>
<pageFooter>
<band height="0" isSplitAllowed="true" >
</band>
</pageFooter>
<summary>
<band height="0" isSplitAllowed="true" >
</band>
</summary>
</jasperReport>
[Message sent by forum member 'roshni79' (roshni79)]
http://forums.java.net/jive/thread.jspa?messageID=343259
===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[email protected] and include in the body of the message "help".