JasperReports Ultimate Guide - Samples - Schema - Configuration - Functions - FAQ - API (Javadoc)
|
|
|
|
JasperReports - XChart Sample (version 6.21.0) | |
|
|
|
Main Features in This Sample | |
| Rendering Images Using Third Party APIs (XChart Library) |
|
|
||||||
top | |||||||
|
|||||||
Rendering Images Using Third Party APIs (XChart Library) | Documented by Sanda Zaharia | ||||||
|
|||||||
| Description / Goal |
| How to render images using the XChart library. | ||||
| Since |
| 6.4.3 | ||||
| Other Samples |
|
|
||||
|
|||||||
|
The XChart Sample
This sample illustrates an interesting example of report scriptlets working in collaboration with third-party APIs, in order to output a chart image generated with the XChart library. First let's see the XChartReport.jrxml template in the samples/xcharts/reports directory. It provides a
scriptletClass="XChartScriptlet" attribute, and a parametrized image element:
<image scaleImage="Clip" hAlign="Center"> <reportElement x="0" y="70" width="400" height="400"/> <graphicElement/> <imageExpression class="java.awt.Image"><![CDATA[$V{ChartImage}]] ></imageExpression> </image>The java.awt.Image object is stored in the ChartImage report variable:
<variable name="ChartImage" class="java.awt.Image" calculation="System"/>To see how the ChartImage variable was "calculated", let's dig a little into the XChartScriptlet.java file in the
src directory:
public void afterReportInit() throws JRScriptletException { try { XYChart xyChart = new XYChartBuilder() .width(400) .height(400) .title("Fruits Order") .xAxisTitle("Day of Week") .yAxisTitle("Quantity (t)") .build(); xyChart.addSeries("Apples", new double[] { 1, 3, 5}, new double[] { 4, 10, 7}); xyChart.addSeries("Bananas", new double[] { 1, 2, 3, 4, 5}, new double[] { 6, 8, 4, 4, 6}); xyChart.addSeries("Cherries", new double[] { 1, 3, 4, 5}, new double[] { 2, 6, 1, 9}); XYStyler styler = xyChart.getStyler(); styler.setLegendPosition(Styler.LegendPosition.InsideNW); styler.setAxisTitlesVisible(true); styler.setDefaultSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Area); BufferedImage bufferedImage = BitmapEncoder.getBufferedImage(xyChart); super.setVariableValue("ChartImage", bufferedImage); } catch(Exception e) { throw new JRScriptletException(e); } }Here an area chart is created after the report initialization, using APIs in the XChart library. The chart is rendered as java.awt.Image and
stored in the ChartImage variable. From now on, the chart image is ready to be used by the report filler when needed.
And that's all the story here. With only a report scriptlet and a third-party library, one could embed interesting, complex, spectacular objects in a given report. Running the Sample Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/xcharts within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/xcharts/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||||
|
|
© 2001- Cloud Software Group, Inc. www.jaspersoft.com |