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 ChartsMultipleReportTest 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)
127 				.setGroupLayout(GroupLayout.DEFAULT_WITH_HEADER)
128 				.build();
129 		
130 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
131 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) // and we add the same operations for the columnAmount and
132 				.addFooterVariable(columnAmount,DJCalculation.SUM) // columnaQuantity columns
133 				.addFooterVariable(columnaQuantity,	DJCalculation.SUM)
134 				.setGroupLayout(GroupLayout.DEFAULT)
135 				.build();
136 
137 		drb.addColumn(columnState);
138 		drb.addColumn(columnBranch);
139 		drb.addColumn(columnaProductLine);
140 		drb.addColumn(columnaItem);
141 		drb.addColumn(columnCode);
142 		drb.addColumn(columnaQuantity);
143 		drb.addColumn(columnAmount);
144 
145 		drb.addGroup(g1); // add group g1
146 		drb.addGroup(g2); // add group g2
147 
148 		drb.setUseFullPageWidth(true);
149 
150 		DJChartBuilder cb = new DJChartBuilder();
151 		DJChart chart =  cb.setType(DJChart.BAR_CHART)
152 						.setOperation(DJChart.CALCULATION_SUM)
153 						.setColumnsGroup(g1)
154 						.addColumn(columnAmount)
155 						.setPosition(DJChartOptions.POSITION_HEADER)
156 						.setShowLabels(true)
157 						.setHeight(200)
158 						.build();
159 
160 		drb.addChart(chart); //add chart
161 		
162 		DJChartBuilder cb2 = new DJChartBuilder();
163 		DJChart chart2 =  cb2.addType(DJChart.PIE_CHART)
164 						.setOperation(DJChart.CALCULATION_SUM)
165 						.setColumnsGroup(g1)
166 						.addColumn(columnAmount)
167 						.setPosition(DJChartOptions.POSITION_HEADER)
168 						.setShowLabels(true)
169 						.setHeight(200)
170 						.build();
171 
172 		drb.addChart(chart2); //add chart	
173 		
174 		DJChartBuilder cb3 = new DJChartBuilder();
175 		DJChart chart3 =  cb3.setType(DJChart.PIE_CHART)
176 		.setOperation(DJChart.CALCULATION_SUM)
177 		.setColumnsGroup(g1)
178 		.addColumn(columnAmount)
179 		.setPosition(DJChartOptions.POSITION_HEADER)
180 		.setShowLabels(true)
181 		.setHeight(200)
182 		.build();
183 		
184 		drb.addChart(chart3); //add chart		
185 
186 		DynamicReport dr = drb.build();
187 
188 		return dr;
189 	}
190 
191 	public static void main(String[] args) throws Exception {
192 		ChartsMultipleReportTest test = new ChartsMultipleReportTest();
193 		test.testReport();
194 		JasperViewer.viewReport(test.jp);
195 //		JasperDesignViewer.viewReportDesign(DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager()));
196 	}
197 
198 }