Sophie

Sophie

distrib > Fedora > 17 > i386 > by-pkgid > cf79c2d0ed7fa2678c515072ee6f31e4 > files > 66

jasperreports-manual-4.0.2-4.fc17.noarch.rpm

<html xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://jasperreports.sourceforge.net/jasperreports">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JasperReports 4.0.2 - JFreeChart Sample</title>
<style type="text/css">
.title {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 28px;
	font-weight: normal;
}

.toc {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.name {
	font-family: Courier New, Courier, serif;
	font-size: 16px;
	font-weight: bold;
}

.label {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
	font-style: italic;
}

.description {
	font-family: Arial, Verdana, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: normal;
}

.value {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.element {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.attribute {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: bold;
}

.code {
	font-family: Courier New, Courier, serif;
	font-size: 12px;
	font-weight: normal;
}

.copy {
	font-decoration: none;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 8pt;
	font-style: normal;
	color: #000000;
}

.subtitle {
	font-family: inherit;
	font-size: inherit;
	font-style: inherit;
	font-weight: bold;
	text-decoration: none;
	color: inherit;
}

</style>
</head>
<body bgcolor="#FFFFFF">
<a name="top"></a>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td colspan="2" align="right"><span class="element"><a href="../../sample.reference.html">Sample Reference</a> - <a href="../../schema.reference.html">Schema Reference</a> - <a href="../../config.reference.html">Configuration Reference</a> - <a href="http://jasperreports.sourceforge.net/api/index.html">API (Javadoc)</a></span>
<br>
</td>
</tr>
<tr>
<td colspan="2">
<hr size="1">
</td>
</tr>
<tr valign="middle">
<td nowrap="true"><span class="title">JasperReports - JFreeChart Sample (version 4.0.2)</span></td><td align="right"><img src="../../resources/jasperreports.png" border="0"></td>
</tr>
<tr>
<td colspan="2">
<hr size="1">
</td>
</tr>
</table>
<br>
<span class="description"><span class="description">Shows how third-party charting APIs could be used for rendering charts as images.</span></span>
<br>
<br>
<span class="element"><a href="http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%204.0.2/jasperreports-4.0.2-project.zip/download" target="_blank">Download All Sample Source Files</a></span>
<br>
<span class="element"><a href="http://jasperforge.org/scm/viewvc.php/tags/jr-4-0-2/jasperreports/demo/samples/jfreechart/?root=jasperreports" target="_blank">Browse Sample Source Files on SVN</a></span>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 20px;">
<br>
</td><td>
<br>
</td>
</tr>
<tr>
<td colspan="2"><span class="label">Main Features in This Sample</span></td>
</tr>
<tr>
<td>
<br>
</td><td><span class="element"><a href="#jfreechart">Rendering Images Using Third Party APIs (JFreeChart Library)</a></span></td>
</tr>
</table>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td width="80%">
<br>
</td><td width="20%">
<br>
</td>
</tr>
<tr>
<td colspan="6" align="right"><a name="jfreechart"></a><a href="#top" class="toc">top</a></td>
</tr>
<tr>
<td colspan="6">
<hr size="1">
</td>
</tr>
<tr valign="top">
<td><img src="../../resources/jr-16x16.png" border="0"></td><td colspan="4"><span class="name">Rendering Images Using Third Party APIs (JFreeChart Library)</span></td><td align="right"><span class="copy">Documented by 
	<a href="mailto:shertage@users.sourceforge.net" class="copy">Sanda Zaharia</a></span></td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
<tr valign="top">
<td>
<br>
</td><td nowrap="true"><span class="label">Description / Goal</span></td><td>
<br>
</td><td colspan="3"><span class="description">
How to render images using the <a href="http://jfree.org/jfreechart/" target="_blank" class="element">JFreeChart</a> library.
    </span></td>
</tr>
<tr valign="top">
<td>
<br>
</td><td colspan="1"><span class="label">Since</span></td><td>
<br>
</td><td colspan="3"><span class="description">0.6.0</span></td>
</tr>
<tr valign="top">
<td>
<br>
</td><td nowrap="true"><span class="label">Other Samples</span></td><td>
<br>
</td><td colspan="3">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><span class="element"><a href="../jcharts/index.html">/demo/samples/jcharts</a></span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
<tr>
<td>
<br>
</td><td colspan="5"><span class="description">
<b>Rendering Graphic Objects</b>

<br>

<br>
Usually, the mechanism of producing complex images, charts and other graphic objects is part of some more specialized Java libraries, 
and does not represent one of the JasperReports goals. However, one of its important goals is to easily integrate within a 
generated report charts, barcodes and other graphics produced by third party libraries.
<br>
This integration is based on the fact that great majority of graphic objects produced by these libraries can output 
to image files or in-memory Java image objects. Then all these generated image objects can be handled by the JasperReports engine 
using a normal image element, as described in the <a href="../images/index.html#images" target="_blank" class="element">Images</a> sample. 
<br>
One problem is that the content of an image element can come either directly from an image file like a JPG, 
GIF, PNG, or can be a Scalable Vector Graphics (SVG) file that is rendered using some 
business logic or a dedicated graphics API related to a specific charting or a barcode library. In this case,
JasperReports treats all kind of images in a very transparent way because it relies on a special 
interface called <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRRenderable.html" target="_blank">JRRenderable</a></span> to offer a common way to render images.
<br>
The <span class="code"><code>JRRenderable</code></span> interface has a method called 
<span class="code"><code>render(Graphics2D grx, Rectangle2D r)</code></span>, which gets called by the engine each time it needs to draw the image 
on a given device or graphic context. This approach provides the best quality for the 
SVG images when they must be drawn on unknown devices or zoomed into without 
losing sharpness. 
<br>
Other methods specified in this interface can be used to obtain the native size of the 
actual image that the renderer wraps or the binary data for an image that must be stored 
in a separate file during export.
<br>
The library comes with a default implementation for the 
<span class="code"><code>JRRenderable</code></span> interface that 
wraps images that come from files or binary image data in JPG, GIF, or PNG format. 
The <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRImageRenderer.html" target="_blank">JRImageRenderer</a></span> class is actually a container 
for this binary image data, which it uses to load a <span class="code"><code>java.awt.Image</code></span> object from it, 
which it then draws on the supplied <span class="code"><code>java.awt.Graphics2D</code></span> context when the engine 
requires it. 
<br>
Image renderers are serializable because inside the generated document for each image is 
a renderer object kept as reference, which is serialized along with the whole <span class="code"><code>JasperPrint</code></span> object. 
<br>
When a <span class="code"><code>JRImageRenderer</code></span> instance is serialized, so is the binary image data it contains. 
However, if the image element must be lazy loaded (see the <span class="code"><code>isLazy</code></span> attribute in the 
<a href="../images/index.html#images" target="_blank" class="element">Images</a> sample), then the 
engine will not load the binary image data at report-filling time. Rather, it stores inside 
the renderer only the <span class="code"><code>java.lang.String</code></span> location of the image. The actual image data 
is loaded only when needed for rendering at report-export or view time. 
<br>
To simplify the implementation of SVG image renderers, JasperReports ships with an 
abstract renderer: <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRAbstractSvgRenderer.html" target="_blank">JRAbstractSvgRenderer</a></span>. This 
implementation contains the code to produce binary image data from the SVG graphic in 
JPG format. This is needed when the image must be stored in separate files on disk or 
delivered in binary format to a consumer (like a web browser). 
<br>
Specific image renderers are called when the class of the <span class="code"><code>&lt;imageExpression/&gt;</code></span> element 
is <span class="code"><code>net.sf.jasperreports.engine.JRRenderable</code></span>. In this case the image expression value should point 
to a <span class="code"><code>JRRenderable</code></span> object which is designated to render the image.
<br>

<br>

<b>Rendering a JFreeChart Object Example</b>

<br>

<br>
This sample shows how to integrate a <span class="code"><code>JFreeChart</code></span> pie chart into a report, 
letting the JFreeChart engine to draw itself the chart.
<br>
In order to get both the <span class="code"><code>JFreeChart</code></span> chart and the <span class="code"><code>JRRenderable</code></span> renderer, the 
sample contains a java scriptlet class named <span class="code"><code>JFreeChartScriptlet</code></span> in the <span class="code"><code>src</code></span> directory. 
In its <span class="code"><code>afterReportInit()</code></span> method, a chart object is created, with all necessary information to be represented:
<pre>
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue("Java", new Double(43.2));
  dataset.setValue("Visual Basic", new Double(10.0));
  dataset.setValue("C/C++", new Double(17.5));
  dataset.setValue("PHP", new Double(32.5));
  dataset.setValue("Perl", new Double(1.0));
	
  JFreeChart chart = 
    ChartFactory.createPieChart3D(
      "Pie Chart 3D Demo 1",
      dataset,
      true,
      true,
      false
      );
	
  PiePlot3D plot = (PiePlot3D) chart.getPlot();
  plot.setStartAngle(290);
  plot.setDirection(Rotation.CLOCKWISE);
  plot.setForegroundAlpha(0.5f);
  plot.setNoDataMessage("No data to display");
</pre>
The resulting chart is passed to a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/renderers/JCommonDrawableRenderer.html" target="_blank">JCommonDrawableRenderer</a></span> 
class constructor. The <span class="code"><code>JCommonDrawableRenderer</code></span> extends the <span class="code"><code>JRAbstractSvgRenderer</code></span> class nominated above, and its 
constructor needs an <span class="code"><code>org.jfree.ui.Drawable</code></span> object, characterized by its <span class="code"><code>draw()</code></span> method. A <span class="code"><code>JFreeChart</code></span> chart 
represents such a <span class="code"><code>Drawable</code></span> object.
<br>
The <span class="code"><code>render()</code></span> method in the <span class="code"><code>JCommonDrawableRenderer</code></span> class just calls the <span class="code"><code>Drawable</code></span> object's <span class="code"><code>draw()</code></span> method, 
and all the rendering stuff will be done by this dedicated API:
<pre>
  public JCommonDrawableRenderer(Drawable drawable) 
  {
    this.drawable = drawable;
  }

  public void render(Graphics2D grx, Rectangle2D rectangle) 
  {
    if (drawable != null) 
    {
      drawable.draw(grx, rectangle);
    }
  }
</pre>
Once the renderer gets available, one have to instruct the JasperReports engine to use it. The new renderer is passed to the engine as a scriptlet variable:
<pre>
  this.setVariableValue("Chart", new JCommonDrawableRenderer(chart));
</pre>
This variable is referred to in the <span class="code"><code>reports/JFreeChartReport.jrxml</code></span> file:
<br>

<br>
&nbsp;&nbsp;<span class="code"><code>&lt;variable name="Chart" class="net.sf.jasperreports.engine.JRRenderable" calculation="System"&gt;</code></span>

<br>

<br>
Now, let's take a look at the &lt;image/&gt; element itself:
<br>

<br>

<span class="code"><code>
&nbsp;&nbsp;&lt;image scaleImage="Clip" hAlign="Center" hyperlinkType="Reference"&gt;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;reportElement x="0" y="110" width="515" height="300"/&gt;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;graphicElement/&gt;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;imageExpression class="net.sf.jasperreports.engine.JRRenderable"&gt;&lt;![CDATA[$V{Chart}]]&gt;&lt;/imageExpression&gt;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;hyperlinkReferenceExpression&gt;&lt;![CDATA["http://www.jfree.org/jfreechart"]]&gt;&lt;/hyperlinkReferenceExpression&gt;
<br>
&nbsp;&nbsp;&lt;/image&gt;
</code></span>

<br>

<br>
The image expression class is <span class="code"><code>net.sf.jasperreports.engine.JRRenderable</code></span>, and its value points to the <span class="code"><code>Chart</code></span> scriptlet variable 
defined in the &lt;scriptlet/&gt; element.
<br>

<br>
In order to figure out more on images rendering, just test this sample by running from the command line the <span class="code"><code>ant test view</code></span> command. 
It will generate all supported document types containing the sample report in the /build/reports directory, and then the report will be loaded and visualized into the built-in viewer.
    </span></td>
</tr>
<tr>
<td colspan="6">
<br>
</td>
</tr>
</table>
<br>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td>
<hr size="1">
</td>
</tr>
<tr>
<td align="center"><span class="copy">&copy; 2001-2010 Jaspersoft Corporation <a href="http://www.jaspersoft.com" target="_blank" class="copy">www.jaspersoft.com</a></span></td>
</tr>
</table>
</body>
</html>