JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - jCharts Component Sample (version 6.4.1) | |
|
|
|
Main Features in This Sample | |
| Implementing Custom Components to Embed Third Party Visualisation Tools (jCharts Library) |
|
|
|||||
top | ||||||
|
||||||
Implementing Custom Components to Embed Third Party Visualisation Tools (jCharts Library) | Documented by Sanda Zaharia | |||||
|
||||||
| Description / Goal |
| How to implement a custom component to wrap charts rendered by the jCharts library. | |||
| Since |
| 3.1.0 | |||
| Other Samples |
|
|
|||
|
||||||
|
The JCharts Component - Overview
This sample contains an axis chart component implementation based on the jCharts library, that illustrates how charts generated with 3-rd party APIs can be embedded in reports generated with the JasperReports library. To make such an integration possible, the chart component provides its specific XSD schema in the src/jcharts/charts.xsd file.
On the API side, the chart component is represented by the jcharts.AxisChartComponent class, which exposes the members declared in schema. When the report is filled, a JRImageRenderer instance is created in order to generate a chart image to be included into the report. The image representation of the chart was preferred instead of the chart itself because AxisChart objects fail on serialization. The JCharts Component - Schema Here is the JRXML schema for the axis chart component: <element name="axisChart" substitutionGroup="jr:component"> <complexType> <complexContent> <extension base="jr:componentType"> <sequence> <element ref="jc:axisDataset"/> <element name="legendLabelExpression"> <complexType mixed="true"/> </element> </sequence> <attribute name="areaColor" type="string" use="required"/> <attribute name="evaluationTime" type="jr:basicEvaluationTime" use="optional" default="Now"/> <attribute name="evaluationGroup" type="string" use="optional"/> </extension> </complexContent> </complexType> </element> <element name="axisDataset"> <complexType> <sequence> <element ref="jr:dataset" minOccurs="0" maxOccurs="1"/> <element name="labelExpression"> <complexType mixed="true"/> </element> <element name="valueExpression"> <complexType mixed="true"/> </element> </sequence> </complexType> </element>A well defined axis chart configuration contains:
The src/jasperreports_extension.properties file contains the following entries:
src/jcharts/chart_beans.xml :
<bean id="componentsBundle" class="net.sf.jasperreports.engine.component.DefaultComponentsBundle"> <property name="xmlParser"> <ref local="xmlParser"/> </property> <property name="componentManagers"> <map> <entry key="axisChart"> <ref local="axisChartManager"/> </entry> </map> </property> </bean> <bean id="xmlParser" class="net.sf.jasperreports.engine.component.DefaultComponentXmlParser"> <property name="namespace"> <value>http://jasperreports.sourceforge.net/jasperreports/jcharts</value> </property> <property name="publicSchemaLocation"> <value>http://jasperreports.sourceforge.net/dtds/charts.xsd</value> </property> <property name="internalSchemaResource"> <value>jcharts/charts.xsd</value> </property> <property name="digesterConfigurer"> <bean class="jcharts.ChartsDigester"/> </property> </bean> <bean id="axisChartManager" class="net.sf.jasperreports.engine.component.DefaultComponentManager"> <property name="componentCompiler"> <bean class="jcharts.AxisChartCompiler"/> </property> <property name="componentXmlWriter"> <bean class="jcharts.AxisChartXmlWriter"/> </property> <property name="componentFillFactory"> <bean class="jcharts.AxisChartFillFactory"/> </property> </bean>The src/jcharts directory contains all necessary implementation APIs for this component:
An example of how to use the axis chart component is illustrated in the reports/AxisChart.jrxml template:
<variable name="Value" class="java.lang.Double"> <variableExpression> new Double(Math.pow($V{REPORT_COUNT}.doubleValue(), 4 - Math.log($V{REPORT_COUNT}.doubleValue()))) </variableExpression> </variable> ... <componentElement> <reportElement x="0" y="70" width="500" height="300"/> <jc:axisChart xmlns:jc="http://jasperreports.sourceforge.net/jasperreports/jcharts" evaluationTime="Report" areaColor="cyan"> <jc:axisDataset> <dataset/> <jc:labelExpression>$V{REPORT_COUNT}.toString()</jc:labelExpression> <jc:valueExpression>$V{Value}</jc:valueExpression> </jc:axisDataset> <jc:legendLabelExpression>"Data"</jc:legendLabelExpression> </jc:axisChart> </componentElement>The report is filled using an empty data source with 12 records, as shown in src/JChartsApp.java :
public void fill() throws JRException { long start = System.currentTimeMillis(); JasperFillManager.fillReportToFile("build/reports/AxisChart.jasper", null, new JREmptyDataSource(12)); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); }The output in this case will be an area chart with 12 integer values on the x-axis. The related y-values are calculated in the $V{Value}
variable expression. The output area color is cyan and the legend will be labeled "Data" .
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/jchartscomponent 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/jchartscomponent/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
|||||
|
|
© 2001- TIBCO Software Inc. www.jaspersoft.com |