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 ChartReportTest3 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.setBorderBottom(Border.PEN_1_POINT());
62  		headerStyle.setBackgroundColor(Color.gray);
63  		headerStyle.setTextColor(Color.white);
64  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
65  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
66  		headerStyle.setTransparency(Transparency.OPAQUE);
67  		
68  		Style g2Variables = new Style();
69  		g2Variables.setFont(Font.ARIAL_MEDIUM_BOLD);
70  //		headerVariables.setBorderBottom(Border.THIN());
71  		g2Variables.setHorizontalAlign(HorizontalAlign.RIGHT);
72  		g2Variables.setVerticalAlign(VerticalAlign.MIDDLE);
73  		
74  		Style g1Variables = new Style();
75  		g1Variables.setFont(Font.ARIAL_MEDIUM_BOLD);
76  		g1Variables.setTextColor(Color.red);
77  		g1Variables.setHorizontalAlign(HorizontalAlign.RIGHT);
78  		g1Variables.setVerticalAlign(VerticalAlign.MIDDLE);				
79  
80  		Style titleStyle = new Style();
81  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
82  		Style importeStyle = new Style();
83  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
84  		Style oddRowStyle = new Style();
85  		oddRowStyle.setBorder(Border.NO_BORDER());
86  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
87  		oddRowStyle.setTransparency(Transparency.OPAQUE);
88  
89  		DynamicReportBuilder drb = new DynamicReportBuilder();
90  		int margin = 20;
91  		drb
92  				.setTitleStyle(titleStyle)
93  				.setTitle("November " + getYear() +" sales report")					//defines the title of the report
94  				.setSubtitle("The items in this report correspond "
95  					+"to the main products: DVDs, Books, Foods and Magazines")
96  				.setDetailHeight(new Integer(15)).setLeftMargin(margin)
97  				.setMargins(margin, margin, margin, margin)
98  //				.setPrintBackgroundOnOddRows(true)
99  				.setPrintColumnNames(false)
100 				.setOddRowBackgroundStyle(oddRowStyle);
101 		
102 		AbstractColumn columnState = ColumnBuilder.getNew()
103 				.setColumnProperty("state", String.class.getName()).setTitle(
104 						"State").setWidth(new Integer(85))
105 				.setStyle(titleStyle).setHeaderStyle(titleStyle).build();
106 
107 		AbstractColumn columnBranch = ColumnBuilder.getNew()
108 				.setColumnProperty("branch", String.class.getName()).setTitle(
109 						"Branch").setWidth(new Integer(85)).setStyle(
110 						detailStyle).setHeaderStyle(headerStyle).build();
111 
112 		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
113 				.setColumnProperty("productLine", String.class.getName())
114 				.setTitle("Product Line").setWidth(new Integer(85)).setStyle(
115 						detailStyle).setHeaderStyle(headerStyle).build();
116 
117 		AbstractColumn columnaItem = ColumnBuilder.getNew()
118 				.setColumnProperty("item", String.class.getName()).setTitle(
119 						"Item").setWidth(new Integer(85)).setStyle(detailStyle)
120 				.setHeaderStyle(headerStyle).build();
121 
122 		AbstractColumn columnCode = ColumnBuilder.getNew()
123 				.setColumnProperty("id", Long.class.getName()).setTitle("ID")
124 				.setWidth(new Integer(40)).setStyle(importeStyle)
125 				.setHeaderStyle(headerStyle).build();
126 
127 		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
128 				.setColumnProperty("quantity", Long.class.getName()).setTitle(
129 						"Quantity").setWidth(new Integer(80)).setStyle(
130 						importeStyle).setHeaderStyle(headerStyle).build();
131 
132 		AbstractColumn columnAmount = ColumnBuilder.getNew()
133 				.setColumnProperty("amount", Float.class.getName()).setTitle(
134 						"Amount").setWidth(new Integer(90))
135 				.setPattern("$ 0.00").setStyle(importeStyle).setHeaderStyle(
136 						headerStyle).build();
137 		
138 		drb.addGlobalFooterVariable(columnAmount,DJCalculation.SUM,g1Variables);
139 		drb.addGlobalFooterVariable(columnaQuantity,DJCalculation.SUM,g1Variables);
140 
141 		GroupBuilder gb1 = new GroupBuilder();
142 
143 //		 define the criteria column to group by (columnState)
144 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
145 					.addFooterVariable(columnAmount,DJCalculation.SUM,g1Variables)
146 				.addFooterVariable(columnaQuantity,DJCalculation.SUM,g1Variables) 
147 				.setGroupLayout(GroupLayout.VALUE_IN_HEADER_WITH_HEADERS) 
148 				.build();
149 
150 
151 		GroupBuilder gb2 = new GroupBuilder(); 
152 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) 
153 				.addFooterVariable(columnAmount,DJCalculation.SUM,g2Variables) 
154 				.addFooterVariable(columnaQuantity,DJCalculation.SUM,g2Variables)
155 				.build();
156 
157 		drb.addColumn(columnState);
158 		drb.addColumn(columnBranch);
159 		drb.addColumn(columnaProductLine);
160 		drb.addColumn(columnaItem);
161 		drb.addColumn(columnCode);
162 		drb.addColumn(columnaQuantity);
163 		drb.addColumn(columnAmount);
164 
165 		drb.addGroup(g1); // add group g1
166 		drb.addGroup(g2); // add group g2
167 
168 		drb.setUseFullPageWidth(true);
169 
170 		DJChartBuilder cb = new DJChartBuilder();
171 		int chartHeight = 200;
172 		DJChart chart = null;
173 //		chart =  cb.addType(DJChart.BAR_CHART)
174 //						.addOperation(DJChart.CALCULATION_SUM)
175 //						.addColumnsGroup(g1)
176 //						.addColumn(columnaQuantity)
177 //						.addColumn(columnAmount)
178 //						.setHeight(chartHeight)
179 //						.setPosition(DJChartOptions.POSITION_HEADER)
180 //						.setShowLabels(true)
181 //						.build();
182 //
183 //		drb.addChart(chart); //add chart
184 		
185 		//2nd chart
186 		cb = new DJChartBuilder();
187 		chart =  cb.addType(DJChart.BAR_CHART)
188 			.addOperation(DJChart.CALCULATION_SUM)
189 			.addColumnsGroup(g2)
190 			.setHeight(chartHeight)
191 			.addColumn(columnaQuantity)
192 			.addColumn(columnAmount)
193 			.setPosition(DJChartOptions.POSITION_HEADER)
194 			.setShowLabels(true)
195 			.build();
196 		
197 		drb.addChart(chart); //add chart
198 
199 		//3rd Chart
200 //		cb = new DJChartBuilder();
201 //		chart =  cb.addType(DJChart.BAR_CHART)
202 //		.addOperation(DJChart.CALCULATION_SUM)
203 //		.addColumnsGroup(g2)
204 //		.setHeight(chartHeight)
205 //		.addColumn(columnaQuantity)
206 //		.setPosition(DJChartOptions.POSITION_FOOTER)
207 //		.build();
208 //		
209 //		drb.addChart(chart); //add chart
210 
211 		DynamicReport dr = drb.build();
212 
213 		return dr;
214 	}
215 
216 	public static void main(String[] args) throws Exception {
217 		ChartReportTest3 test = new ChartReportTest3();
218 		test.testReport();
219 		JasperViewer.viewReport(test.jp);
220 		//JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(test.dr, new ClassicLayoutManager(),test.params));
221 	}
222 
223 }