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 java.awt.Color;
33  
34  import net.sf.jasperreports.view.JasperViewer;
35  import ar.com.fdvs.dj.domain.DJCalculation;
36  import ar.com.fdvs.dj.domain.DJChart;
37  import ar.com.fdvs.dj.domain.DJChartOptions;
38  import ar.com.fdvs.dj.domain.DynamicReport;
39  import ar.com.fdvs.dj.domain.Style;
40  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
41  import ar.com.fdvs.dj.domain.builders.DJChartBuilder;
42  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
43  import ar.com.fdvs.dj.domain.builders.GroupBuilder;
44  import ar.com.fdvs.dj.domain.constants.Border;
45  import ar.com.fdvs.dj.domain.constants.Font;
46  import ar.com.fdvs.dj.domain.constants.GroupLayout;
47  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
48  import ar.com.fdvs.dj.domain.constants.Transparency;
49  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
50  import ar.com.fdvs.dj.domain.entities.DJGroup;
51  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
52  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
53  
54  public class ChartReportTest extends BaseDjReportTest {
55  
56  	public DynamicReport buildReport() throws Exception {
57  
58  		Style detailStyle = new Style();
59  		Style headerStyle = new Style();
60  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
61  		headerStyle.setBorder(Border.PEN_2_POINT());
62  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
63  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
64  
65  		Style titleStyle = new Style();
66  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
67  		Style importeStyle = new Style();
68  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
69  		Style oddRowStyle = new Style();
70  		oddRowStyle.setBorder(Border.NO_BORDER());
71  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
72  		oddRowStyle.setTransparency(Transparency.OPAQUE);
73  
74  		DynamicReportBuilder drb = new DynamicReportBuilder();
75  		int margin = 20;
76  		drb
77  				.setTitleStyle(titleStyle)
78  				.setTitle("November " + getYear() +" sales report")					//defines the title of the report
79  				.setSubtitle("The items in this report correspond "
80  					+"to the main products: DVDs, Books, Foods and Magazines")
81  				.setDetailHeight(new Integer(15)).setLeftMargin(margin)
82  				.setMargins(margin, margin, margin, margin)
83  //				.setPrintBackgroundOnOddRows(true)
84  				.setPrintColumnNames(false)
85  				.setOddRowBackgroundStyle(oddRowStyle);
86  
87  		AbstractColumn columnState = ColumnBuilder.getNew()
88  				.setColumnProperty("state", String.class.getName()).setTitle(
89  						"State").setWidth(new Integer(85))
90  				.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
91  
92  		AbstractColumn columnBranch = ColumnBuilder.getNew()
93  				.setColumnProperty("branch", String.class.getName()).setTitle(
94  						"Branch").setWidth(new Integer(85)).setStyle(
95  						detailStyle).setHeaderStyle(headerStyle).build();
96  
97  		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
98  				.setColumnProperty("productLine", String.class.getName())
99  				.setTitle("Product Line").setWidth(new Integer(85)).setStyle(
100 						detailStyle).setHeaderStyle(headerStyle).build();
101 
102 		AbstractColumn columnaItem = ColumnBuilder.getNew()
103 				.setColumnProperty("item", String.class.getName()).setTitle(
104 						"Item").setWidth(new Integer(85)).setStyle(detailStyle)
105 				.setHeaderStyle(headerStyle).build();
106 
107 		AbstractColumn columnCode = ColumnBuilder.getNew()
108 				.setColumnProperty("id", Long.class.getName()).setTitle("ID")
109 				.setWidth(new Integer(40)).setStyle(importeStyle)
110 				.setHeaderStyle(headerStyle).build();
111 
112 		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
113 				.setColumnProperty("quantity", Long.class.getName()).setTitle(
114 						"Quantity").setWidth(new Integer(80)).setStyle(
115 						importeStyle).setHeaderStyle(headerStyle).build();
116 
117 		AbstractColumn columnAmount = ColumnBuilder.getNew()
118 				.setColumnProperty("amount", Float.class.getName()).setTitle(
119 						"Amount").setWidth(new Integer(90))
120 				.setPattern("$ 0.00").setStyle(importeStyle).setHeaderStyle(
121 						headerStyle).build();
122 
123 		GroupBuilder gb1 = new GroupBuilder();
124 
125 //		 define the criteria column to group by (columnState)
126 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState).addFooterVariable(columnAmount,
127 						DJCalculation.SUM) // tell the group place a variable footer of the column "columnAmount" with the SUM of allvalues of the columnAmount in this group.
128 				.addFooterVariable(columnaQuantity,
129 						DJCalculation.SUM) // idem for the columnaQuantity column
130 				.setGroupLayout(GroupLayout.DEFAULT_WITH_HEADER) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
131 				.build();
132 
133 
134 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
135 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) // and we add the same operations for the columnAmount and
136 				.addFooterVariable(columnAmount,
137 						DJCalculation.SUM) // columnaQuantity columns
138 				.addFooterVariable(columnaQuantity,
139 						DJCalculation.SUM).build();
140 
141 		drb.addColumn(columnState);
142 		drb.addColumn(columnBranch);
143 		drb.addColumn(columnaProductLine);
144 		drb.addColumn(columnaItem);
145 		drb.addColumn(columnCode);
146 		drb.addColumn(columnaQuantity);
147 		drb.addColumn(columnAmount);
148 
149 		drb.addGroup(g1); // add group g1
150 //		drb.addGroup(g2); // add group g2
151 
152 		drb.setUseFullPageWidth(true);
153 
154 		DJChartBuilder cb = new DJChartBuilder();
155 		DJChart chart =  cb.setType(DJChart.BAR_CHART)
156 						.setOperation(DJChart.CALCULATION_SUM)
157 						.setColumnsGroup(g1)
158 						.addColumn(columnAmount)
159 						.addColumn(columnaQuantity)
160 						.setPosition(DJChartOptions.POSITION_HEADER)
161 						.setShowLabels(true)
162 						.build();
163 
164 		drb.addChart(chart); //add chart
165 
166 		DynamicReport dr = drb.build();
167 
168 		return dr;
169 	}
170 
171 	public static void main(String[] args) throws Exception {
172 		ChartReportTest test = new ChartReportTest();
173 		test.testReport();
174 		test.exportToJRXML();
175 		JasperViewer.viewReport(test.jp);
176 //		JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(test.dr, test.getLayoutManager(),test.params));
177 	}
178 
179 }