JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - jCharts Sample (version 4.6.0) | ![]() |
|
|
|
Main Features in This Sample | |
| Rendering Images Using Third Party APIs (jCharts Library) |
![]() | ![]() | ![]() | ![]() |
|
|
||
top | |||||||
|
|||||||
![]() | Rendering Images Using Third Party APIs (jCharts Library) | Documented by Sanda Zaharia | |||||
|
|||||||
| Description / Goal |
| How to render images using the jCharts library. | ||||
| Since |
| 0.6.0 | ||||
| Other Samples |
|
|
||||
|
|||||||
|
The JCharts 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 jCharts library. First let's see the JChartsReport.jrxml template in the samples/jcharts/reports directory. It provides a
scriptletClass="JChartsScriptlet" attribute, and a parametrized image element:
<image scaleImage="Clip" hAlign="Center"> <reportElement x="0" y="70" width="515" height="350"/> <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 JChartsScriptlet.java file in the
src directory:
public void afterReportInit() throws JRScriptletException { try { AreaChartProperties areaChartProperties = new AreaChartProperties(); double[][] data = {{10, 15, 30, 53}, {6, 30, 10, 21}, {20, 25, 20, 8}}; Paint[] paints = {new Color( 0, 255, 0, 100 ), new Color( 255, 0, 0, 100 ), new Color( 0, 0, 255, 100 )}; String[] legendLabels = {"Games", "Events", "Players" }; AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data, legendLabels, paints, ChartType.AREA, areaChartProperties); String[] axisLabels = {"January", "March", "May", "June"}; DataSeries dataSeries = new DataSeries(axisLabels, "Months", "People", "Popular Events"); dataSeries.addIAxisPlotDataSet(axisChartDataSet); ChartProperties chartProperties = new ChartProperties(); AxisProperties axisProperties = new AxisProperties(); axisProperties.setYAxisRoundValuesToNearest(0); LegendProperties legendProperties = new LegendProperties(); AxisChart axisChart = new AxisChart(dataSeries, chartProperties, axisProperties, legendProperties, 500, 350); BufferedImage bufferedImage = new BufferedImage(500, 350, BufferedImage.TYPE_INT_RGB); axisChart.setGraphics2D(bufferedImage.createGraphics()); axisChart.render(); super.setVariableValue("ChartImage", bufferedImage); } catch(ChartDataException chartDataException) { throw new JRScriptletException(chartDataException); } }Here an area chart is created after the report initialization, using APIs in the jCharts 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/jcharts 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/jcharts/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||||
|
|
© 2001- Jaspersoft Corporation www.jaspersoft.com |