JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
JasperReports - Open Flash Chart Component Sample (version 5.6.0) |

Shows how Open Flash Chart objects could be included in HTML and PDF reports using a specially designed component.
Download All Sample Source Files
Browse Sample Source Files on SVN

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.extension.registry.factory.fcharts=net.sf.jasperreports.extensions.SpringExtensionsRegistryFactory` `net.sf.jasperreports.extension.fcharts.spring.beans.resource=net/sf/jasperreports/components/ofc/chart_beans.xml` Hence the Open Flash Chart components will be registered as Spring-based extension, in accordance with beans in the `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: `BarChartCompiler.java` - the compiler class for the bar chart component `BarChartComponent.java` - the bar chart component class `BarChartFillFactory.java` - the fill factory class for the bar chart component `BarChartXmlFactory.java` - the XML factory class for the bar chart component `BarChartXmlWriter.java` - the XML writer class for the bar chart component `BarDataset.java` - the embedded bar dataset class `BarSeries.java` - the bar series class `BaseChartHandler.java` - the basic chart handler class that produces either a simple text replacement or a cell containing a text replacement, to be used in output formats that not support Flash `ChartDataGenerator.java` - class that generates data for pie and bar charts in JSON format `ChartDocxHandler.java` - handler class for the DOCX output format. Produces text replacement for the Flash object. `ChartHtmlHandler.java` - handler class for the HTML output format. Embeds the Flash object into the HTML page. `ChartJExcelApiHandler.java` - handler class for the Excel 2003 output format, using the JExcelApiExporter (based on the JExcelApi library). Produces text replacement for the Flash object. `ChartNoPdfHandler.java` - handler class for the PDF output format that produces text replacement for the Flash object `ChartOdsHandler.java` - handler class for the ODS output format. Produces text replacement for the Flash object. `ChartOdtHandler.java` - handler class for the ODT output format. Produces text replacement for the Flash object. `ChartPdfHandler.java` - handler class for the PDF output format that embeds the Flash object into the PDF document `ChartPptxHandler.java` - handler class for the PPTX output format. Produces text replacement for the Flash object. `ChartRtfHandler.java` - handler class for the RTF output format. Produces text replacement for the Flash object. `ChartsDigester.java` - the digester class for the chart component `ChartXhtmlHandler.java` - handler class for the XHTML output format. Embeds the Flash object into the generated HTML page. `ChartXlsHandler.java` - handler class for the Excel 2003 output format, using the JRXlsExporter (based on the Apache POI library). Produces text replacement for the Flash object. `ChartXlsxHandler.java` - handler class for the Excel 2010 (XLSX) output format. Produces text replacement for the Flash object. `ChartXml4SwfHandler.java` - handler class to be used when exporting the document to the Flash viewer. `CompiledBarDataset.java` - class for compiled bar dataset `CompiledPieDataset.java` - class for compiled pie dataset `DefaultBarSeries.java` - class that configure the series structure in a bar dataset `DesignBarDataset.java` - design class for the bar dataset `DesignPieDataset.java` - design class for the pie dataset `FillBarChart.java` - the fill bar chart class `FillBarDataset.java` - the fill bar dataset class `FillBarSeries.java` - the fill bar series class `FillPieChart.java` - the fill pie chart class `FillPieDataset.java` - the fill pie dataset class `PieChartCompiler.java` - the compiler class for the pie chart component `PieChartComponent.java` - the pie chart component class `PieChartFillFactory.java` - the fill factory class for the pie chart component `PieChartXmlFactory.java` - the XML factory class for the pie chart component `PieChartXmlWriter.java` - the XML writer for the pie chart component `PieDataset.java` - class for the pie dataset The JCharts Component - Sample 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