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

Shows how the built-in spider chart component can be included in reports.
Download All Sample Source Files
Browse Sample Source Files on SVN

Main Features in This Sample
| Using the Built-in Spider Chart Component

top
| Using the Built-in Spider Chart Component | Documented by Sanda Zaharia
| Description / Goal |  | How to configure the built-in spider chart component rendered by the JFreeChart library.
| Since |  | 3.7.4
| Other Samples |  |
/demo/samples/jfreechart
| The Built-in Spider Chart Component - Overview Spider charts (also known as radar charts) are very useful when multivariate data sets have to be represented. Such data sets are usually displayed as overlapping spider webs anchored in a multiple radial axis system. The JR built-in spider chart was developed as component element in order to illustrate how to implement custom components based on the JFreeChart charting engine. The component in this case should output a JFreeChart object, collecting its data from an usual CategoryDataset, but rendering it as a spider web based on the SpiderWebPlot API. According to the component's schema, the content of a spider chart component is structured in 3 complementary elements, each one prefixed with the `c:` namespace prefix: `c:chartSettings` - stores general settings for the generated chart object such as title, subtitles and chart legend. Plot settings are not included. `c:spiderDataset` - stores appropriate settings for the category dataset `c:spiderPlot` - stores settings available for the spider web plot As any other component element, the spider chart provides the `evaluationTime` and `evaluationGroup` attributes:
    <element name="spiderChart" substitutionGroup="jr:component">
      <complexType>
        <complexContent>
    	  <extension base="jr:componentType">
    		<sequence>
    		  <element ref="c:chartSettings" />
    		  <element ref="c:spiderDataset" />
    		  <element ref="c:spiderPlot" />
    		</sequence>
    		<attribute name="evaluationTime" type="jr:basicEvaluationTime" use="optional" default="Now"/>
    		<attribute name="evaluationGroup" type="string" use="optional"/>
    	  </extension>
        </complexContent>
      </complexType>
    </element>
Chart Settings In The Spider Chart Component In the `<chartSettings>` element one can configure the following structure similar to the `<chart>` element in usual JR charts: `jr:chartTitle` `jr:chartSubtitle` `jr:chartLegend` `jr:anchorNameExpression` `jr:hyperlinkReferenceExpression` `jr:hyperlinkAnchorExpression` `jr:hyperlinkPageExpression` `jr:hyperlinkTooltipExpression` `jr:hyperlinkParameter` Note: In a regular JRXML file the `jr:` prefix may be omitted. The attributes below are also available for the `<chartSettings>` element, with similar meanings as in usual JR charts: `isShowLegend` `backcolor` `hyperlinkType` `hyperlinkTarget` `bookmarkLevel` `customizerClass` `renderType` Note: The main difference here is that the chart customizer must be an implementation of the ChartCustomizer interface that exposes the `public void customize(JFreeChart chart, ChartComponent chartComponent)` method adapted for chart components. The easiest way to implement this interface is by extending the context-aware AbstractChartCustomizer class in order to get access to the component element with its properties, for a completely flexible chart customization:
      public void customize(JFreeChart chart, ChartComponent chartComponent)
      {
        JRElement element = chartComponent.getContext().getComponentElement();
        //other customizations here
      }
Here is the XSD schema fragment:
    <element name="chartSettings">
     <complexType>
      <sequence>
       <element ref="jr:chartTitle" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:chartSubtitle" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:chartLegend" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:anchorNameExpression" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:hyperlinkReferenceExpression" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:hyperlinkAnchorExpression" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:hyperlinkPageExpression" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:hyperlinkTooltipExpression" minOccurs="0" maxOccurs="1"/>
       <element ref="jr:hyperlinkParameter" minOccurs="0" maxOccurs="unbounded"/>
      </sequence>
      <attribute name="isShowLegend" use="optional">
       <simpleType>
        <restriction base="string">
         <enumeration value="true"/>
         <enumeration value="false"/>
        </restriction>
       </simpleType>
      </attribute>
      <attribute name="backcolor" type="string" use="optional">
       <annotation>
    	<documentation>Background color of the chart</documentation>
       </annotation>
      </attribute>
      <attribute name="hyperlinkType" type="string" use="optional"/>
      <attribute name="hyperlinkTarget" type="string" use="optional"/>
      <attribute name="bookmarkLevel" type="NMTOKEN" use="optional" default="0"/>
      <attribute name="customizerClass" type="string" use="optional"/>
      <attribute name="renderType" type="string" use="optional"/>
     </complexType>
    </element>
The Spider Dataset The spider dataset is a JRElementDataset based on category series. It contains the same structure as the `<categoryDataset>` element in usual JR charts: `jr:dataset` `jr:categorySeries` Again, the `jr:` prefix may be omitted in a regular JRXML file. The Spider Web Plot The spider plot element displays available data in the form of a "spider web", depending on specific settings. Multiple series can be plotted on the same (category) axis. Neither tick marks and tick labels nor negative values on axis are supported for the moment. The following attributes can be used to configure a spider plot: `rotation` - Specifies the mode the radar axis are drawn. Possible values are `Clockwise` and `Anticlockwise`. `tableOrder` - Specifies whether the data series are stored in rows or in columns. Possible values are `Row` and `Column`. `isWebFilled` - Specifies if the web polygons are filled or not. Possible values are `true` and `false`. `startAngle` - Specifies the starting angle (in degrees) for the spider chart axis. The default value is 90 deg. `headPercent` - Specifies the head radius percent. `interiorGap` - Specifies the interior gap percent. `axisLineColor` - Specifies the color to use when drawing the line showing the axis. `axisLineWidth` - Specifies the axis line width. `labelGap` - Specifies the axis label gap percent. `labelColor` - Specifies the label foreground color. `backcolor` - Specifies the background color of the plot. `backgroundAlpha` - Specifies the Alpha (transparency) parameter for the plot background. `foregroundAlpha` - Specifies the Alpha (transparency) parameter for the plot foreground. One can also define the `labelFont` element for the spider plot, and a `maxValueExpression` representing the maximum value any category axis can take. The Spider Chart Sample Here is the piece of code used to configure the spider chart in the JRXML sample:
    <componentElement>
      <reportElement x="50" y="90" width="400" height="400" mode="Opaque"/>
      <c:spiderChart xmlns:c="http://jasperreports.sourceforge.net/jasperreports/components" evaluationTime="Now">
        <c:chartSettings isShowLegend="true" backcolor="green" customizerClass="SpiderChartCustomizer">
          <chartTitle position="Top">
            <font isItalic="true" size="12"/>
            <titleExpression><![CDATA["Spider Chart"]] ></titleExpression>
          </chartTitle>
          <chartSubtitle color="blue">
            <font isBold="true" size="6"/>
            <subtitleExpression><![CDATA["Chart Displaying Spider Web Contours"]] ></subtitleExpression>
          </chartSubtitle>
          <chartLegend backgroundColor="#eeffee">
            <font size="6"/>
          </chartLegend>
          <hyperlinkTooltipExpression><![CDATA["Spider Chart"]] ></hyperlinkTooltipExpression>
        </c:chartSettings>
        <c:spiderDataset>
          <dataset incrementType="Group" incrementGroup="category"/>
          <categorySeries>
            <seriesExpression><![CDATA[$F{series}]] ></seriesExpression>
            <categoryExpression><![CDATA[$F{category}]] ></categoryExpression>
            <valueExpression><![CDATA[$F{value}]] ></valueExpression>
            <itemHyperlink hyperlinkType="Reference" hyperlinkTarget="Blank">
              <hyperlinkReferenceExpression>
                <![CDATA["http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/SpiderWebPlot.html"]] >
              </hyperlinkReferenceExpression>
            </itemHyperlink>
          </categorySeries>
        </c:spiderDataset>
        <c:spiderPlot
          rotation="Anticlockwise"
          startAngle="39"
          interiorGap="0.2"
          labelGap="0.05"
          headPercent="0.01"
          axisLineColor="#ff0000"
          backcolor="yellow"
          axisLineWidth="2.0"
          tableOrder="Row">
          <labelFont>
            <font isBold="true" isItalic="true" size="6"/>
          </labelFont>
          <c:maxValueExpression><![CDATA[Double.valueOf(10)]] ></c:maxValueExpression>
        </c:spiderPlot>
      </c:spiderChart>
    </componentElement>
The resulting spider chart will have a green background, an italicized title and a blue subtitle, a legend with its own background color and font size, and a plot with rotation anticlockwise, start angle = 39deg, red axis, yellow backcolor and italicized bold labels on axis. See also how the interior gaps and axis line widths were set. Note also that the plot outline in the spider chart is customized via the SpiderChartCustomizer class. 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/spiderchartcomponent` 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/spiderchartcomponent/build/reports` directory. Then the report will open in the JasperReports internal viewer.

© 2001- Jaspersoft Corporation www.jaspersoft.com