DynamicReports(Getting started)
Posted onDynamicReports(Getting started)
Getting started
Table of contents
- Step 1 : Start
- Step 2 : Styles
- Step 3 : Additional columns
- Step 4 : Group
- Step 5 : Subtotals
- Step 6 : Charts
- Step 7 : Column grid & Containers
- Step 8 : Hide subtotal
- Step 9 : Title
- Step 10 : Currency data type
- Step 11 : Detail row highlighters
- Step 12 : Conditional styles
Overview
Creating a report with DynamicReports is very easy, take a look at the example below 01
import
static
net.sf.dynamicreports.report.builder.DynamicReports./*;
02
public
class
Report { 03
04
private
void
build() { 05
try
{
06
report()
//create new report design 07
.columns(...)
//adds columns
08
.groupBy(...)
//adds groups 09
.subtotalsAtSummary(...)
//adds subtotals
10
... 11
//set datasource
12
.setDataSource(...) 13
//export report
14
.toPdf(...)
//export report to pdf 15
.toXls(...)
//export report to excel
16
... 17
//other outputs
18
.toJasperPrint()
//creates jasperprint object 19
.show()
//shows report
20
.print()
//prints report 21
...
22
}
catch
(DRException e) { 23
e.printStackTrace();
24
} 25
}
26
... 27
}
See ReportBuilder class for all available features and JasperReportBuilder class for all available exports and outputs
Simple report
Please note that this is a tutorial example and that this tutorial doesn't describe all features!
You will learn in this section how to create a simple report step by step. First we need to create an empty report desing, configure it and set a datasource. Afterwards the report will be ready to export into any format. It's important to remember the DynamicReports class, through which you will have available most of features. The class provides methods for building a particular piece of a report. Let's start!
Step 1 : Start
First define columns through DynamicReports.col.column(title, field name, datatype) (the field name must match with the name of a field contained in the datasource) and pass them into the report as follows: 1
.columns(
//add columns
2
// title, field name data type 3
col.column(
"Item"
,
"item"
, type.stringType()),
4
col.column(
"Quantity"
,
"quantity"
, type.integerType()), 5
col.column(
"Unit price"
,
"unitprice"
, type.bigDecimalType()))
now define some text at the title and number of pages at the page footer as follows:
1
.title(cmp.text(
"Getting started"
))
//shows report title
2
.pageFooter(cmp.pageXofY())
//shows number of page at page footer
DynamicReports.cmp.text(some text) - creates a component that shows a text DynamicReports.cmp.pageXofY() - creates a component that shows page X of Y Methods title(...) and pageFooter(...) allows to customize a particular report band by adding components to it
Step 2 : Styles
Each style can have a parent style from which it will inherit its properties. Empty style can be created by DynamicReports.stl.style() 01
StyleBuilder boldStyle = stl.style().bold();
02
StyleBuilder boldCenteredStyle = stl.style(boldStyle) 03
.setHorizontalAlignment(HorizontalAlignment.CENTER);
04
StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle) 05
.setBorder(stl.pen1Point())
06
.setBackgroundColor(Color.LIGHT_GRAY); 07
report()
08
.setColumnTitleStyle(columnTitleStyle) 09
.highlightDetailEvenRows()
10
.title(cmp.text(
"Getting started"
).setStyle(boldCenteredStyle)) 11
.pageFooter(cmp.pageXofY().setStyle(boldCenteredStyle)) SimpleReport_Step02
Step 3 : Additional columns
You can very easy multiply, divide, add or subtract column of numbers by another column of numbers or by a number value 1
//price = unitPrice /* quantity
2
TextColumnBuilder
.setTitle(
"Price"
);
Adding percentage of any column of numbers is simple DynamicReports.col.percentageColumn(title, column)
1
PercentageColumnBuilder pricePercColumn = col.percentageColumn(
"Price %"
, priceColumn);
DynamicReports.col.reportRowNumberColumn(title) creates a column that shows row number
1
TextColumnBuilder
2
col.reportRowNumberColumn(
"No."
) 3
//sets the fixed width of a column, width = 2 /* character width
4
.setFixedColumns(
2
) 5
.setHorizontalAlignment(HorizontalAlignment.CENTER); SimpleReport_Step03
Step 4 : Group
We continue by adding a group as shown below 1
.groupBy(itemColumn) SimpleReport_Step04
Step 5 : Subtotals
Now we can try to sum a column of numbers. Subtotal of sum is created through DynamicReports.sbt.sum(column) 1
.subtotalsAtSummary(
2
sbt.sum(unitPriceColumn), sbt.sum(priceColumn)) 3
.subtotalsAtFirstGroupFooter(
4
sbt.sum(unitPriceColumn), sbt.sum(priceColumn))
Method subtotalsAtSummary(...) allows to add subtotals to the summary band Method subtotalsAtFirstGroupFooter(...) will find first defined group and add subtotals to the group footer band
Step 6 : Charts
DynamicReports.cht provide methods for building charts. Category and series are required. 01
Bar3DChartBuilder itemChart = cht.bar3DChart()
02
.setTitle(
"<a href="
http:
//www.dynamicreports.org/examples/sales" title="Sales">Sales by item") 03
.setCategory(itemColumn)
04
.addSerie( 05
cht.serie(unitPriceColumn), cht.serie(priceColumn));
06
Bar3DChartBuilder itemChart2 = cht.bar3DChart() 07
.setTitle(
"<a href="
http:
//www.dynamicreports.org/examples/sales" title="Sales">Sales by item")
08
.setCategory(itemColumn) 09
.setUseSeriesAsCategory(
true
)
10
.addSerie( 11
cht.serie(unitPriceColumn), cht.serie(priceColumn));
Chart is a component and can be placed into any report band.
1
.summary(itemChart, itemChart2) SimpleReport_Step06
Step 7 : Column grid & Containers
Components inserted into the bands are arranged vertically, each component is below the previously added component. To arrange components horizontally it is needed to wrap these components with a horizontal container. Container is a component as well and therefore it can be added to any of the report bands. 1
.summary(
2
cmp.horizontalList(itemChart, itemChart2))
Columns layout can be modified by a column grid. The layout is applied to the columns title, details and subtotals.
1
.columnGrid(
2
rowNumberColumn, quantityColumn, unitPriceColumn, 3
grid.verticalColumnGridList(priceColumn, pricePercColumn)) SimpleReport_Step07
Step 8 : Hide subtotal
Subtotal for the group notebook is unnecessary because contains only one row. We need to change the group declaration and set the boolean expression condition on which depends whether subtotal is printed. DynamicReports.exp.printWhenGroupHasMoreThanOneRow(itemGroup) creates a boolean condition which returns true when itemGroup has more than one row. 1
ColumnGroupBuilder itemGroup = grp.group(itemColumn);
2
itemGroup.setPrintSubtotalsWhenExpression( 3
exp.printWhenGroupHasMoreThanOneRow(itemGroup));
1
.groupBy(itemGroup) SimpleReport_Step08
Step 9 : Title
First define a title style. 1
StyleBuilder titleStyle = stl.style(boldCenteredStyle)
2
.setVerticalAlignment(VerticalAlignment.MIDDLE) 3
.setFontSize(
15
);
DynamicReports.cmp.image() creates an image component DynamicReports.cmp.filler() creates an empty component
1
.title(
//shows report title
2
cmp.horizontalList() 3
.add(
4
cmp.image(getClass().getResourceAsStream(
"../images/dynamicreports.png"
)).setFixedDimension(
80
,
80
), 5
cmp.text(
"DynamicReports"
).setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
6
cmp.text(
"Getting started"
).setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT)) 7
.newRow()
8
.add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(
10
)))
The defined filler creates an additional blank space between the title and the column header. Setting top border of a filler draws the line at the bottom of the title. Horizontal list, as previously mentioned, arranges components horizontally in one row but by calling the method row() a new horizontal list is created which is located at the bottom of the previous horizontal list.
Step 10 : Currency data type
Unit price and price column are currency types. Showing currency is possible by setting pattern to both columns (via method setPattern()), but the best practice is to create a custom data type and apply it to the columns. The custom data type then can be used in other reports. 01
CurrencyType currencyType =
new
CurrencyType();
02
TextColumnBuilder
"Unit price"
,
"unitprice"
, currencyType); 03
//price = unitPrice /* quantity
04
TextColumnBuilder
"Price"
) 05
.setDataType(currencyType);
06
private
class
CurrencyType
extends
BigDecimalType { 07
private
static
final
long
serialVersionUID = 1L;
08
09
@Override
10
public
String getPattern() { 11
return
"$ /#,/#/#/#.00"
;
12
} 13
Step 11 : Detail row highlighters
1
ConditionalStyleBuilder condition1 = stl.conditionalStyle(cnd.greater(priceColumn,
150
))
2
.setBackgroundColor(
new
Color(
210
,
255
,
210
)); 3
ConditionalStyleBuilder condition2 = stl.conditionalStyle(cnd.smaller(priceColumn,
30
))
4
.setBackgroundColor(
new
Color(
255
,
210
,
210
));
1
.detailRowHighlighters(
2
condition1, condition2)
Condition1 is applied only if price is greater than 150 and sets background color of a row to green. Condition2 is applied only if price is smaller than 30 and sets background color of a row to red. SimpleReport_Step11
Step 12 : Conditional styles
1
ConditionalStyleBuilder condition3 = stl.conditionalStyle(cnd.greater(priceColumn,
200
))
2
.setBackgroundColor(
new
Color(
0
,
190
,
0
)) 3
.bold();
4
ConditionalStyleBuilder condition4 = stl.conditionalStyle(cnd.smaller(priceColumn,
20
)) 5
.setBackgroundColor(
new
Color(
190
,
0
,
0
))
6
.bold(); 7
StyleBuilder priceStyle = stl.style()
8
.conditionalStyles( 9
condition3, condition4);
1
priceColumn = unitPriceColumn.multiply(quantityColumn).setTitle(
"Price"
)
2
.setDataType(currencyType) 3
.setStyle(priceStyle);
Condition3 is applied only if price is greater than 200 and sets background color of a price to green. Condition4 is applied only if price is smaller than 20 and sets background color of a price to red. SimpleReport_Step1
来源: [http://www.dynamicreports.org/getting-started](http://www.dynamicreports.org/getting-started)