View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   *
27   *
28   */
29  
30  package ar.com.fdvs.dj.test;
31  
32  import ar.com.fdvs.dj.domain.*;
33  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
34  import ar.com.fdvs.dj.domain.builders.DJChartBuilder;
35  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
36  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
37  import ar.com.fdvs.dj.domain.constants.*;
38  import ar.com.fdvs.dj.domain.constants.Font;
39  import ar.com.fdvs.dj.domain.constants.Transparency;
40  import ar.com.fdvs.dj.domain.entities.DJGroup;
41  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
42  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
43  import net.sf.jasperreports.view.JasperViewer;
44  
45  import java.awt.*;
46  import java.util.Map;
47  
48  public class ChartReportWithExpressionColumnTest extends BaseDjReportTest {
49  
50  	public DynamicReport buildReport() throws Exception {
51  
52  		Style detailStyle = new Style();
53  		Style headerStyle = new Style();
54  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
55  		headerStyle.setBorder(Border.PEN_2_POINT());
56  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
57  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
58  
59  		Style titleStyle = new Style();
60  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
61  		Style importeStyle = new Style();
62  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
63  		Style oddRowStyle = new Style();
64  		oddRowStyle.setBorder(Border.NO_BORDER());
65  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
66  		oddRowStyle.setTransparency(Transparency.OPAQUE);
67  
68  		DynamicReportBuilder drb = new DynamicReportBuilder();
69  		int margin = 20;
70  		drb
71  				.setTitleStyle(titleStyle)
72  				.setTitle("November " + getYear() +" sales report")					//defines the title of the report
73  				.setSubtitle("The items in this report correspond "
74  					+"to the main products: DVDs, Books, Foods and Magazines")
75  				.setDetailHeight(new Integer(15)).setLeftMargin(margin)
76  				.setMargins(margin, margin, margin, margin)
77  //				.setPrintBackgroundOnOddRows(true)
78  				.setPrintColumnNames(false)
79  				.setOddRowBackgroundStyle(oddRowStyle);
80  
81  		AbstractColumn columnState = ColumnBuilder.getNew()
82  				.setColumnProperty("state", String.class.getName()).setTitle(
83  						"State").setWidth(new Integer(85))
84  				.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
85  
86  		AbstractColumn columnBranch = ColumnBuilder.getNew()
87  				.setColumnProperty("branch", String.class.getName()).setTitle(
88  						"Branch").setWidth(new Integer(85)).setStyle(
89  						detailStyle).setHeaderStyle(headerStyle).build();
90  
91  		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
92  				.setColumnProperty("productLine", String.class.getName())
93  				.setTitle("Product Line").setWidth(new Integer(85)).setStyle(
94  						detailStyle).setHeaderStyle(headerStyle).build();
95  
96  		AbstractColumn columnaItem = ColumnBuilder.getNew()
97  				.setColumnProperty("item", String.class.getName()).setTitle(
98  						"Item").setWidth(new Integer(85)).setStyle(detailStyle)
99  				.setHeaderStyle(headerStyle).build();
100 
101 		AbstractColumn columnCode = ColumnBuilder.getNew()
102 				.setColumnProperty("id", Long.class.getName()).setTitle("ID")
103 				.setWidth(new Integer(40)).setStyle(importeStyle)
104 				.setHeaderStyle(headerStyle).build();
105 
106 		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
107 				.setColumnProperty("quantity", Long.class.getName()).setTitle(
108 						"Quantity").setWidth(new Integer(80)).setStyle(
109 						importeStyle).setHeaderStyle(headerStyle).build();
110 
111 		AbstractColumn columnAmount = ColumnBuilder.getNew()
112                 .setCustomExpression(new CustomExpression() {
113                     public Object evaluate(Map fields, Map variables, Map parameters) {
114                         return ((Float)fields.get("amount")).floatValue() * 500f;
115                     }
116 
117                     public String getClassName() {
118                         return Float.class.getName();
119                     }
120                 })
121 				//.setColumnProperty("amount", Float.class.getName())
122                 .setTitle("Amount").setWidth(new Integer(90))
123 				.setPattern("$ 0.00").setStyle(importeStyle)
124                 .setHeaderStyle(headerStyle)
125                 .build();
126 
127 
128 
129 		GroupBuilder gb1 = new GroupBuilder();
130 
131 //		 define the criteria column to group by (columnState)
132 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState).addFooterVariable(columnAmount,
133 						DJCalculation.SUM) // tell the group place a variable footer of the column "columnAmount" with the SUM of allvalues of the columnAmount in this group.
134 				.addFooterVariable(columnaQuantity,
135 						DJCalculation.SUM) // idem for the columnaQuantity column
136 				.setGroupLayout(GroupLayout.DEFAULT_WITH_HEADER) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
137 				.build();
138 
139 
140 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
141 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) // and we add the same operations for the columnAmount and
142 				.addFooterVariable(columnAmount,
143 						DJCalculation.SUM) // columnaQuantity columns
144 				.addFooterVariable(columnaQuantity,
145 						DJCalculation.SUM).build();
146 
147 		drb.addColumn(columnState);
148 		drb.addColumn(columnBranch);
149 		drb.addColumn(columnaProductLine);
150 		drb.addColumn(columnaItem);
151 		drb.addColumn(columnCode);
152 		drb.addColumn(columnaQuantity);
153 		drb.addColumn(columnAmount);
154 
155         drb.addField("amount",Float.class.getName());
156 
157 		drb.addGroup(g1); // add group g1
158 //		drb.addGroup(g2); // add group g2
159 
160 		drb.setUseFullPageWidth(true);
161 
162 		DJChartBuilder cb = new DJChartBuilder();
163 		DJChart chart =  cb.setType(DJChart.BAR_CHART)
164 						.setOperation(DJChart.CALCULATION_SUM)
165 						.setColumnsGroup(g1)
166 						.addColumn(columnAmount)
167 //						.addColumn(columnaQuantity)
168 						.setPosition(DJChartOptions.POSITION_HEADER)
169 						.setShowLabels(true)
170 						.build();
171 
172 		drb.addChart(chart); //add chart
173 
174 		DynamicReport dr = drb.build();
175 
176 		return dr;
177 	}
178 
179 	public static void main(String[] args) throws Exception {
180 		ChartReportWithExpressionColumnTest test = new ChartReportWithExpressionColumnTest();
181 		test.testReport();
182 		test.exportToJRXML();
183 		JasperViewer.viewReport(test.jp);
184 //		JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(test.dr, test.getLayoutManager(),test.params));
185 	}
186 
187 }