Tài liệu JasperReports 3.5 for Java Developers- P5 - Pdf 87

Chapter 7
[
189
]
vAlign
This attribute indicates the vertical alignment of the image. The valid values for
this attribute are as follows:
•
Bottom
: The image will be placed at the bottom of the area dened by
its
<reportElement>
.
•
Middle
: The image will be vertically centered between the top and bottom
boundaries dened by
<reportElement>
.
•
Top
: The image will be placed at the top of the area dened
by
<reportElement>
.
The default value of
vAlign
is
Top
.
IsLazy

.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Adding Charts and Graphics to Reports
[
190
]
onErrorType
The
onErrorType
attribute determines the report's behavior when there is a problem
loading the image. The valid values for this attribute are as follows:
•
Blank
: Only blank space will be displayed instead of the image.
•
Error
: An exception will be thrown, and the report will not be lled
or viewed.
•
Icon
: An icon indicating a missing image will be displayed.
The default value of
onErrorType
is
Error
.
The
<image>
element contains other attributes to support hyperlinks and bookmarks,

customizerClass
This attribute denes the name of a class that can be used to customize the chart. The
value for this element must be a string containing the name of a customizer class.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 7
[
191
]
evaluationGroup
When
evaluationTime
is
Group
, the
evaluationGroup
attribute determines
the name of the group to use for evaluating the chart's expressions. The value
for this attribute must match the group name we would like to use as the chart's
evaluation group.
evaluationTime
The
evaluationTime
attribute determines when the chart's expression will be
evaluated. The valid values for this attribute are as follows:
•
Band
: The chart is rendered when the containing band has nished rendering
all other elements.
•

true
: A legend will be displayed on the report.
•
false
: A legend will not be displayed on the report.
The default value of
isShowLegend
is
true
.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Adding Charts and Graphics to Reports
[
192
]
Chart customization
JasperReports uses
JFreeChart
as the underlying charting library;
JFreeChart

contains features that are not directly supported by JasperReports. We can
take advantage of these features by supplying a
customizer
class through the
customizerClass
attribute.
All the customizer classes must implement the
net.sf.jasperreports.engine.

element that
denes when the chart's expressions are evaluated and reset.
Attributes of the <dataset> element
The following sections describe all of the attributes for the JRXML
<dataset>
element.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 7
[
193
]
incrementType
The
incrementType
attribute determines when to recalculate the value of the chart
expression. The valid values for this attribute are as follows:
•
Column
: The chart expression is recalculated at the end of each column.
•
Group
: The chart expression is recalculated when the group specied by
incrementGroup
changes.
•
None
: The chart expression is recalculated with every record.
•
Page

incrementGroup
changes.
•
None
: The chart expression is never reset.
•
Page
: The chart expression is recalculated at the beginning of every page.
•
Report
: The chart expression is recalculated once at the beginning of
the report.
The default value of
resetType
is
Report
.
resetGroup
The
resetGroup
determines the name of the group at which the chart expression
value is reset, when
resetType
is
Group
. The value for this attribute must match
the name of any group declared in the JRXML report template.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Adding Charts and Graphics to Reports

and
1
,
both inclusive. The higher the number, the less transparent the background will be.
The default value of
backgroundAlpha
is
1
.
foregroundAlpha
The
foregroundAlpha
attribute denes the transparency of the chart's foreground
colors. The valid values for this attribute include any decimal number between
0
and
1
, both inclusive. The higher the number, the less transparent the foreground will be.
The default value of
foregroundAlpha
is
1
.
orientation
The
orientation
attribute denes the chart's orientation (vertical or horizontal).
The valid values for this attribute are as follows:
•
Horizontal

Adding Charts and Graphics to Reports
[
196
]
xmlns:xsi=" /> xsi:schemaLocation="rceforge
.net/jasperreports http://jasperreports
.sourceforge.net/xsd/jasperreport.xsd"
name="PieChartDemoReport">
<queryString>
<![CDATA[select am.model from aircraft a, aircraft_models am
where city='WASHINGTON' and state='DC'
and a.aircraft_model_code = am.aircraft_model_code
order by model]]>
</queryString>
<field name="model" class="java.lang.String" />
<variable name="totalAircraft" class="java.lang.Integer"
calculation="Count" resetType="Group"
resetGroup="modelGroup">
<variableExpression>
<![CDATA[$F{model}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new java.lang.Integer(0)]]>
</initialValueExpression>
</variable>
<group name="modelGroup">
<groupExpression>
<![CDATA[$F{model}]]>
</groupExpression>
</group>

<chart evaluationTime="Report" isShowLegend="false">
<reportElement x="125" y="375" width="300" height="200" />
</chart>
<pieDataset>
<dataset incrementType="None" />
<keyExpression>
<![CDATA[$F{model}]]>
</keyExpression>
<valueExpression>
<![CDATA[$V{totalAircraft}]]>
</valueExpression>
</pieDataset>
<pie3DPlot>
<plot/>
</pie3DPlot>
</pie3DChart>
<!-- End 3D Pie Chart -->
</band>
</summary>
</jasperReport>
We can see from this example that the JRXML element to create a 2D pie chart is
<pieChart>
, and the JRXML element to create a 3D pie chart is
<pie3DChart>
.
Just like all the other JRXML chart elements, these elements also contain a
<chart>

subelement. They contain a
<pieDataSet>

chart's
<plot>
element. Also, element
<pie3DChart>
must contain an analogous
<pie3DPlot>
element. The
<piePlot>
element has no attributes, whereas the
<pie3DPlot>
element has a single optional attribute called
depthFactor
. This
attribute indicates the depth (how tall or short the pie chart is) of the 3D pie chart;
its default value is 0.2.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Adding Charts and Graphics to Reports
[
198
]
Bar charts
Bar charts, just like pie charts, can be used to illustrate quantitative differences
between chart elements. They can be used to display the same data a pie chart
displays, but in a different way. One advantage that bar charts have over pie charts is
that the same data for more than one category can be displayed.
Suppose we are asked to produce a report comparing the number of aircraft
registered in Washington, DC, with the number of aircraft registered in New York
city. The report must also illustrate the most popular aircraft models in each city.
If we wanted to display this data graphically using a pie chart, we would have to

calculation="Count" resetType="Group"
resetGroup="modelGroup">
<variableExpression>
<![CDATA[$F{model}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new java.lang.Integer(0)]]>
</initialValueExpression>
</variable>
<group name="modelGroup">
<groupExpression>
<![CDATA[$F{model}]]>
</groupExpression>
</group>
<summary>
<band height="750">
<!-- Start 2D Bar Chart -->
<barChart>
<chart evaluationTime="Report">
<reportElement x="0" y="0" width="555" height="350" />
</chart>
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Adding Charts and Graphics to Reports
[
200
]
<categoryDataset>
<dataset incrementType="None" />
<categorySeries>

</categoryExpression>
<valueExpression>
<![CDATA[$V{totalAircraft}]]>
</valueExpression>
</categorySeries>
</categoryDataset>
<bar3DPlot>
<plot />
</bar3DPlot>
</bar3DChart>
<!-- End 3D Bar Chart -->
</band>
</summary>
</jasperReport>
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 7
[
201
]
As we can see in this example, the process used to create bar charts is very similar to
the one for creating pie charts. This example creates two bar charts, a 2D and a 3D.
Let's discuss the 2D bar chart rst.
The JRXML element used to create a 2D bar chart is
<barChart>
. Just like all the
other charts in JasperReports, it must contain a
<chart>
subelement, which contains
a

<plot/>
element must be a subelement of
<bar3DPlot>
. The
<bar3DPlot>

element contains three attributes:
•
isShowLabels
: It determines whether labels will be shown in the chart.
•
xOffset
: Its valid value is any numeric value indicating the number of pixels
to use for the 3D effect on the x axis.
•
yOffset
: Its valid value is any numeric value indicating the number of pixels
to use for the 3D effect on the y axis.
XY line charts
XY line charts allow us to view the relationship between two numerical values. For
our next example, let's suppose that we need to generate a report for a ight school
to illustrate the operating cost for ying a particular model of their aircraft. Let's
assume the ight school has an inventory of 43 of these aircraft, and the operating
cost of each aircraft is $45 per day. The JRXML to generate a report with a chart
illustrating the operating cost would look like the following:
<?xml version="1.0" encoding="UTF-8" ?>
<jasperReport
xmlns="
xmlns:xsi="
xsi:schemaLocation="

<xyDataset>
<dataset incrementType="None" />
<xySeries>
<seriesExpression>
<![CDATA["CH 2000"]]>
</seriesExpression>
<xValueExpression>
<![CDATA[$V{grandTotalAircraft}]]>
</xValueExpression>
<yValueExpression>
<![CDATA[new Long($V{grandTotalAircraft}.longValue()
*45L)]]>
</yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot />
</linePlot>
</xyLineChart>
<!-- End X Y Line Chart -->
</band>
</summary>
</jasperReport>
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 7
[
203
]
The generated report would look like this:

This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Adding Charts and Graphics to Reports
[
204
]
Other types of charts
As we have seen in the previous examples, all the JRXML elements used to display
a chart follow a pattern. First, we have the element that determines what chart
to plot (
<pieChart>
,
<barChart>
, and so on). Inside that element is a
<chart>

element followed by an element enclosing the
<dataset>
element (
<pieDataset>
,
<categoryDataset>
, and so on), which, in turn, is followed by an element enclosing
the
<plot>
element (
<piePlot>
,
<barPlot>
, and so on). As all of the charts follow

Candlestick
chart
<candlestickChart> <highLowDataset> <candlestickPlot>
Gantt chart
<ganttChart> <ganttDataset> <barPlot>
Meter chart
<meterChart> <valueDataset> <meterPlot>
Multiple axis
chart
<multiAxisChart> <categoryDataset> <multiAxisPlot>
Stacked area
chart
<stackedAreaChart> <categoryDataset> <areaPlot>
Thermometer
chart
<thermometerChart> <valueDataset> <thermometerPlot>
XY line chart
<xyLineChart> <xyDataset> <linePlot>
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 7
[
205
]
You can nd details of the attributes for each of these at

JR%20Website/jasperreports_quickref.html
.
Summary
In this chapter, we learned how to add graphical elements to our reports. We also

• How to execute snippets of Java code by using scriptlets
• How to create crosstab (cross-tabulation) reports
• How to use subdatasets to run a query with the results of a different query
• How to add anchors, hyperlinks, and bookmarks to the reports in order
to ease navigation between report sections
Report localization
JasperReports takes advantage of the Java language's internationalization features
to be able to generate reports in different languages. The following JRXML template
will generate a report displaying a line of text that will be different depending on the
locale used:
<?xml version="1.0" encoding="UTF-8" ?>
<jasperReport xmlns="
/jasperreports"
xmlns:xsi=" /> xsi:schemaLocation="
/jasperreports
/jasperreport.xsd"
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Other JasperReports Features
[
208
]
name="LocalizationDemoReport"
resourceBundle="localizationdemo">
<summary>
<band height="60">
<textField>
<reportElement x="0" y="0" width="200" height="30" />
<textFieldExpression>
<![CDATA[$R{localization.text1}]]>

.
To generate a report from this template in Spanish,
localization_es.properties

must look like this:
localization.text1=Este texto es en Español.
Notice how in both the property les the key (text before the equals sign) is the
same. This must be the case for each locale property le that we wish to use because
JasperReports uses this key to obtain the localized text to display in the report.
As can be seen in the example, the syntax to obtain the value for resource bundle
properties is
$R{key}
.
This material is copyright and is licensed for the sole use by William Anderson on 26th August 2009
4310 E Conway Dr. NW, , Atlanta, , 30327Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status