JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - Open Flash Chart Component Sample (version 4.6.0) | ![]() |
|
|
|
Main Features in This Sample | |
| Implementing Custom Components to Embed Third Party Visualisation Tools (Open Flash Chart Library) |
![]() | ![]() | ![]() | ![]() |
|
|
top | |||||
|
|||||
![]() | Implementing Custom Components to Embed Third Party Visualisation Tools (Open Flash Chart Library) | Documented by Sanda Zaharia | |||
|
|||||
| Description / Goal |
| How to implement a custom component to wrap charts rendered by the Open Flash Chart library. | ||
| Since |
| 3.1.2 | ||
|
|||||
|
The Open Flash Chart Component - Overview
This sample illustrates how the pie and bar charts produced with the Open Flash Chart library can be implemented and used as JasperReports components. Charts based on the Open Flash Chart library are converted into single embeddable Flash objects and provide interesting dynamic visual effects that could make them represent the best candidate to display various statistics, especially on interactive web sites. The generated Flash objects are reading their data and configuration parameters in the JSON format. Data can be either read from existing text files, or dynamically passed through JavaScript. An apparent inconvenient is that there are no built-in elements in the basic JasperReports library to handle Flash objects. But the very flexible JasperReports engine can easily integrate such kind of objects, if they come with their own rendering mechanism to be plugged in at report filling time. In other words, if they are embedded into a JasperReports component. Here we have an example. The Open Flash Chart Component - Schema The schema definition for the pie and bar chart components can be found in the src/net/sf/jasperreports/components/ofc/openflashchart.xsd :
<element name="pieChart" substitutionGroup="jr:component"> <complexType> <complexContent> <extension base="jr:componentType"> <sequence> <element ref="ofc:pieDataset"/> <element name="titleExpression"> <complexType mixed="true"/> </element> </sequence> <attribute name="evaluationTime" type="jr:basicEvaluationTime" use="optional" default="Now"/> <attribute name="evaluationGroup" type="string" use="optional"/> </extension> </complexContent> </complexType> </element> <element name="pieDataset"> <complexType> <sequence> <element ref="jr:dataset" minOccurs="0" maxOccurs="1"/> <element name="keyExpression"> <complexType mixed="true"/> </element> <element name="valueExpression"> <complexType mixed="true"/> </element> </sequence> </complexType> </element> <element name="barChart" substitutionGroup="jr:component"> <complexType> <complexContent> <extension base="jr:componentType"> <sequence> <element ref="ofc:barDataset"/> <element name="titleExpression"> <complexType mixed="true"/> </element> </sequence> <attribute name="evaluationTime" type="jr:basicEvaluationTime" use="optional" default="Now"/> <attribute name="evaluationGroup" type="string" use="optional"/> </extension> </complexContent> </complexType> </element> <element name="barDataset"> <complexType> <sequence> <element ref="jr:dataset" minOccurs="0" maxOccurs="1"/> <element name="barSeries" minOccurs="1" maxOccurs="unbounded"> <complexType> <sequence> <element name="seriesExpression"> <complexType mixed="true"/> </element> <element name="categoryExpression"> <complexType mixed="true"/> </element> <element name="valueExpression"> <complexType mixed="true"/> </element> </sequence> </complexType> </element> </sequence> </complexType> </element>According to the schema, the pie chart component resides on a pie dataset and a title expression, along with the evaluationTime and
evaluationGroup attributes. In a similar mode the bar chart is completely characterized by the bar dataset, the title expression
and attributes evaluationTime and evaluationGroup .
Slices in the pie dataset are configured using the keyExpression and valueExpression .
Series in the bar dataset are each one characterized by a seriesExpression , a categoryExpression and a valueExpression .
Embedding The Open Flash Chart Component The src/jasperreports_extension.properties file contains the following entries:
net/sf/jasperreports/components/ofc/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="pieChart"> <ref local="pieChartManager"/> </entry> <entry key="barChart"> <ref local="barChartManager"/> </entry> </map> </property> </bean> <bean id="xmlParser" class="net.sf.jasperreports.engine.component.DefaultComponentXmlParser"> <property name="namespace"> <value>http://jasperreports.sourceforge.net/openflashchart</value> </property> <property name="publicSchemaLocation"> <value>http://jasperreports.sourceforge.net/xsd/openflashchart.xsd</value> </property> <property name="internalSchemaResource"> <value>net/sf/jasperreports/components/ofc/openflashchart.xsd</value> </property> <property name="digesterConfigurer"> <bean class="net.sf.jasperreports.components.ofc.ChartsDigester"/> </property> </bean> <bean id="pieChartManager" class="net.sf.jasperreports.engine.component.DefaultComponentManager"> <property name="componentCompiler"> <bean class="net.sf.jasperreports.components.ofc.PieChartCompiler"/> </property> <property name="componentXmlWriter"> <bean class="net.sf.jasperreports.components.ofc.PieChartXmlWriter"/> </property> <property name="componentFillFactory"> <bean class="net.sf.jasperreports.components.ofc.PieChartFillFactory"/> </property> </bean> <bean id="barChartManager" class="net.sf.jasperreports.engine.component.DefaultComponentManager"> <property name="componentCompiler"> <bean class="net.sf.jasperreports.components.ofc.BarChartCompiler"/> </property> <property name="componentXmlWriter"> <bean class="net.sf.jasperreports.components.ofc.BarChartXmlWriter"/> </property> <property name="componentFillFactory"> <bean class="net.sf.jasperreports.components.ofc.BarChartFillFactory"/> </property> </bean> <bean id="chartsExportHandlerBundle" class="net.sf.jasperreports.engine.export.DefaultElementHandlerBundle"> <property name="namespace" value="http://jasperreports.sourceforge.net/openflashchart"/> <property name="elementHandlers"> <map> <entry key="chart"> <map> <entry key="net.sf.jasperreports.html"> <bean class="net.sf.jasperreports.components.ofc.ChartHtmlHandler"/> </entry> <entry key="net.sf.jasperreports.pdf"> <bean class="net.sf.jasperreports.components.ofc.ChartNoPdfHandler"/> </entry> <entry key="net.sf.jasperreports.xls"> <bean class="net.sf.jasperreports.components.ofc.ChartXlsHandler"/> </entry> <entry key="net.sf.jasperreports.jxl"> <bean class="net.sf.jasperreports.components.ofc.ChartJExcelApiHandler"/> </entry> <entry key="net.sf.jasperreports.rtf"> <bean class="net.sf.jasperreports.components.ofc.ChartRtfHandler"/> </entry> <entry key="net.sf.jasperreports.xhtml"> <bean class="net.sf.jasperreports.components.ofc.ChartXhtmlHandler"/> </entry> <entry key="net.sf.jasperreports.odt"> <bean class="net.sf.jasperreports.components.ofc.ChartOdtHandler"/> </entry> <entry key="net.sf.jasperreports.ods"> <bean class="net.sf.jasperreports.components.ofc.ChartOdsHandler"/> </entry> <entry key="net.sf.jasperreports.docx"> <bean class="net.sf.jasperreports.components.ofc.ChartDocxHandler"/> </entry> <entry key="net.sf.jasperreports.xlsx"> <bean class="net.sf.jasperreports.components.ofc.ChartXlsxHandler"/> </entry> <entry key="net.sf.jasperreports.pptx"> <bean class="net.sf.jasperreports.components.ofc.ChartPptxHandler"/> </entry> <entry key="net.sf.jasperreports.xml4swf"> <bean class="net.sf.jasperreports.components.ofc.ChartXml4SwfHandler"/> </entry> </map> </entry> </map> </property> </bean>One can notice the Chart*Handler classes in the chartsExportHandlerBundle , used to handle the output for
different export formats. Excepting the HTML/XHTML and PDF that provide support for Flash objects, all other output format handlers
extend the net.sf.jasperreports.components.ofc.BaseChartHandler class, ensuring that the generated element will display
the replacement text '[Open Flash Chart Component]' instead.
All necessary APIs to implement these components are found in the src/net/sf/jasperreports/components/ofc directory:
The OpenFlashChartReport.jrxml template in the demo/samples/openflashchart directory contains both the pie chart
and the bar chart components in the summary section:
<componentElement> <reportElement x="0" y="0" width="555" height="350"/> <ofc:pieChart xmlns:ofc="http://jasperreports.sourceforge.net/openflashchart" xsi:schemaLocation="http://jasperreports.sourceforge.net/openflashchart http://jasperreports.sourceforge.net/xsd/openflashchart.xsd" evaluationTime="Report"> <ofc:pieDataset> <dataset incrementType="Group" incrementGroup="Country"/> <ofc:keyExpression>$F{ShipCountry}</ofc:keyExpression> <ofc:valueExpression>$V{CountryFreight}</ofc:valueExpression> </ofc:pieDataset> <ofc:titleExpression>"Pie Chart"</ofc:titleExpression> </ofc:pieChart> </componentElement> <componentElement> <reportElement x="0" y="400" width="555" height="350"/> <ofc:barChart xmlns:ofc="http://jasperreports.sourceforge.net/openflashchart" xsi:schemaLocation="http://jasperreports.sourceforge.net/openflashchart http://jasperreports.sourceforge.net/xsd/openflashchart.xsd" evaluationTime="Report"> <ofc:barDataset> <dataset incrementType="Group" incrementGroup="Year"/> <ofc:barSeries> <ofc:seriesExpression>$F{ShipCountry}</ofc:seriesExpression> <ofc:categoryExpression>$V{OrderYear}</ofc:categoryExpression> <ofc:valueExpression>$V{CountryYearFreight}</ofc:valueExpression> </ofc:barSeries> </ofc:barDataset> <ofc:titleExpression>"Bar Chart"</ofc:titleExpression> </ofc:barChart> </componentElement>One can see the very simple configuration requirements for both Open Flash Charts prefixed with the ofc: namespace prefix. They
only need to know the chart data source and some adequate expressions for series in the dataset, all the remaining TODO stuff being automatized
by the Open Flash Chart and JasperReports libraries working together.
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/hsqldb within the JasperReports source project and run the > ant runServer command.
It will start the HSQLDB server shipped with the JasperReports distribution package. Let this terminal running the HSQLDB server.
Open a new command prompt/terminal window and set the current folder to demo/samples/openflashchart within the JasperReports source project and run the > ant
test command.
It will generate all supported document types containing the sample report in the demo/samples/openflashchart/build/reports directory.
You will notice that all generated documents excepting OpenFlashChartReport.html and OpenFlashChartReport.x.html are displaying two text elements labeled
'[Open Flash Chart Component]' . Also, when opening the HTML and XHTML pages, they are apparently empty. That's because of security restrictions that prevent Flash objects
to communicate with the HTML pages wherein they are embedded.
Such a problem could be avoided using a HTTP server. This exactly happens when the > ant viewHtml command is run in the terminal. A NanoHTTPD server is started and
the served web page may now access the trusted SWF file. Both pie and bar charts can be observed in the browser, for both HTML and XHTML output formats.
|
||||
|
|
© 2001- Jaspersoft Corporation www.jaspersoft.com |