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.domain.chart.builder;
31  
32  import java.awt.Color;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import net.sf.jasperreports.charts.design.JRDesignScatterPlot;
37  import net.sf.jasperreports.charts.design.JRDesignXyDataset;
38  import net.sf.jasperreports.engine.JRFont;
39  import net.sf.jasperreports.engine.design.JRDesignChart;
40  import net.sf.jasperreports.engine.design.JRDesignGroup;
41  import net.sf.jasperreports.engine.design.JRDesignVariable;
42  import net.sf.jasperreports.engine.type.LineStyleEnum;
43  import net.sf.jasperreports.view.JasperViewer;
44  import ar.com.fdvs.dj.domain.DJHyperLink;
45  import ar.com.fdvs.dj.domain.DynamicJasperDesign;
46  import ar.com.fdvs.dj.domain.DynamicReport;
47  import ar.com.fdvs.dj.domain.StringExpression;
48  import ar.com.fdvs.dj.domain.Style;
49  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
50  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
51  import ar.com.fdvs.dj.domain.chart.DJChart;
52  import ar.com.fdvs.dj.domain.chart.DJChartOptions;
53  import ar.com.fdvs.dj.domain.chart.builder.DJScatterChartBuilder;
54  import ar.com.fdvs.dj.domain.chart.plot.DJAxisFormat;
55  import ar.com.fdvs.dj.domain.constants.Border;
56  import ar.com.fdvs.dj.domain.constants.Font;
57  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
58  import ar.com.fdvs.dj.domain.constants.Transparency;
59  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
60  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
61  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
62  import ar.com.fdvs.dj.domain.hyperlink.LiteralExpression;
63  import ar.com.fdvs.dj.test.BaseDjReportTest;
64  
65  public class ScatterChartBuilderTest extends BaseDjReportTest {
66  	private DynamicReportBuilder drb; 
67  	private JRDesignChart chart;
68  	
69  	protected void setUp() throws Exception {
70  		drb = new DynamicReportBuilder();
71  
72  		Style headerStyle = new Style();
73  		headerStyle.setFont(Font.VERDANA_MEDIUM_BOLD);
74  		headerStyle.setBorderBottom(Border.PEN_2_POINT());
75  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
76  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
77  		headerStyle.setBackgroundColor(Color.DARK_GRAY);
78  		headerStyle.setTextColor(Color.WHITE);
79  		headerStyle.setTransparency(Transparency.OPAQUE);
80  		drb.setDefaultStyles(null, null, headerStyle, null);			
81  		
82  		AbstractColumn columnCode = ColumnBuilder.getNew()
83  		.setColumnProperty("id", Long.class.getName()).setTitle("ID")
84  		.setWidth(new Integer(40)).build();
85  		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
86  		.setColumnProperty("quantity", Long.class.getName()).setTitle(
87  				"Quantity").setWidth(new Integer(80)).build();
88  		AbstractColumn columnAmount = ColumnBuilder.getNew()
89  		.setColumnProperty("amount", Float.class.getName()).setTitle(
90  				"Amount").setWidth(new Integer(90)).build();
91  
92  		drb.addColumn(columnCode);
93  		drb.addColumn(columnaQuantity);
94  		drb.addColumn(columnAmount);
95  		
96  		drb.setUseFullPageWidth(true);
97  		
98  		DJAxisFormat xAxisFormat = new DJAxisFormat("x");
99  		xAxisFormat.setLabelFont(Font.ARIAL_SMALL);
100 		xAxisFormat.setLabelColor(Color.DARK_GRAY);
101 		xAxisFormat.setTickLabelFont(Font.ARIAL_SMALL);
102 		xAxisFormat.setTickLabelColor(Color.DARK_GRAY);
103 		xAxisFormat.setTickLabelMask("#,###.#");
104 		xAxisFormat.setLineColor(Color.DARK_GRAY);
105 		
106 		DJAxisFormat yAxisFormat = new DJAxisFormat("y");
107 		yAxisFormat.setLabelFont(Font.ARIAL_SMALL);
108 		yAxisFormat.setLabelColor(Color.DARK_GRAY);
109 		yAxisFormat.setTickLabelFont(Font.ARIAL_SMALL);
110 		yAxisFormat.setTickLabelColor(Color.DARK_GRAY);
111 		yAxisFormat.setTickLabelMask("#,##0.0");
112 		yAxisFormat.setLineColor(Color.DARK_GRAY);
113 		
114 		DJChart djChart = new DJScatterChartBuilder()
115 		//chart		
116 		.setX(20)
117 		.setY(10)
118 		.setWidth(500)
119 		.setHeight(250)
120 		.setCentered(false)
121 		.setBackColor(Color.LIGHT_GRAY)
122 		.setShowLegend(true)
123 		.setPosition(DJChartOptions.POSITION_FOOTER)
124 		.setTitle("title")
125 		.setTitleColor(Color.DARK_GRAY)
126 		.setTitleFont(Font.ARIAL_BIG_BOLD)
127 		.setSubtitle("subtitle")
128 		.setSubtitleColor(Color.DARK_GRAY)
129 		.setSubtitleFont(Font.COURIER_NEW_BIG_BOLD)
130 		.setLegendColor(Color.DARK_GRAY)
131 		.setLegendFont(Font.COURIER_NEW_MEDIUM_BOLD)
132 		.setLegendBackgroundColor(Color.WHITE)
133 		.setLegendPosition(DJChartOptions.EDGE_BOTTOM)
134 		.setTitlePosition(DJChartOptions.EDGE_TOP)
135 		.setLineStyle(DJChartOptions.LINE_STYLE_DOTTED)
136 		.setLineWidth(1)
137 		.setLineColor(Color.DARK_GRAY)
138 		.setPadding(5)
139 		//dataset
140 		.setXValue((PropertyColumn) columnCode)
141 		.addSerie(columnaQuantity, "quant.")
142 		.addSerie(columnAmount)
143 		//plot
144 		.setShowShapes(true)
145 		.setShowLines(false)
146 		.setXAxisFormat(xAxisFormat)
147 		.setYAxisFormat(yAxisFormat)
148 		.build();
149 		drb.addChart(djChart);
150 		
151 		DJHyperLink djlink = new DJHyperLink();
152 		djlink.setExpression(new StringExpression() {
153 			public Object evaluate(Map fields, Map variables, Map parameters) {				
154 				return "http://thisIsAURL?count=" + variables.get("REPORT_COUNT");
155 			}
156 		});
157 		djlink.setTooltip(new LiteralExpression("I'm a literal tootltip"));		
158 		djChart.setLink(djlink);
159 		
160 		HashMap vars = new HashMap();
161 		vars.put(columnaQuantity, new JRDesignVariable());
162 		vars.put(columnAmount, new JRDesignVariable());
163 		JRDesignGroup group = new JRDesignGroup();
164 		chart = djChart.transform(new DynamicJasperDesign(), "", group, group, vars, 0);
165 	}
166 	
167 	public void testChart() {
168 		assertEquals(20, chart.getX());
169 		assertEquals(10, chart.getY());
170 		assertEquals(500, chart.getWidth());
171 		assertEquals(250, chart.getHeight());
172 		assertEquals(Color.LIGHT_GRAY, chart.getBackcolor());
173 		assertEquals(Boolean.TRUE, chart.getShowLegend());
174 		assertNotNull(chart.getTitleExpression().getText());
175 		assertEquals(Color.DARK_GRAY, chart.getTitleColor());
176 		testFont(Font.ARIAL_BIG_BOLD, chart.getTitleFont());		
177 		assertNotNull(chart.getSubtitleExpression().getText());
178 		assertEquals(Color.DARK_GRAY, chart.getSubtitleColor());
179 		testFont(Font.COURIER_NEW_BIG_BOLD, chart.getSubtitleFont());
180 		assertEquals(Color.DARK_GRAY, chart.getLegendColor());
181 		testFont(Font.COURIER_NEW_MEDIUM_BOLD, chart.getLegendFont());
182 		assertEquals(Color.WHITE, chart.getLegendBackgroundColor());
183 		assertEquals(new Byte(DJChartOptions.EDGE_BOTTOM), chart.getLegendPositionValue().getValueByte() );
184 		assertEquals(new Byte(DJChartOptions.EDGE_TOP), chart.getTitlePositionValue().getValueByte());
185 		assertEquals(LineStyleEnum.getByValue(new Byte(DJChartOptions.LINE_STYLE_DOTTED)), chart.getLineBox().getPen().getLineStyleValue());
186 		assertEquals(new Float(1), chart.getLineBox().getPen().getLineWidth());
187 		assertEquals(Color.DARK_GRAY, chart.getLineBox().getPen().getLineColor());
188 		assertEquals(new Integer(5), chart.getLineBox().getPadding());
189 	}
190 	
191 	public void testDataset() {
192 		JRDesignXyDataset dataset = (JRDesignXyDataset) chart.getDataset();
193 		assertEquals(2, dataset.getSeriesList().size());
194 		assertNotNull(dataset.getSeries()[0].getLabelExpression().getText());
195 		assertNotNull(dataset.getSeries()[0].getSeriesExpression().getText());
196 	}
197 	
198 	public void testPlot() {
199 		JRDesignScatterPlot plot = (JRDesignScatterPlot) chart.getPlot();
200 		assertEquals(Boolean.TRUE, plot.getShowShapes());
201 		assertEquals(Boolean.FALSE, plot.getShowLines());
202 		
203 		assertNotNull(plot.getXAxisLabelExpression().getText());
204 		testFont(Font.ARIAL_SMALL, plot.getXAxisLabelFont());
205 		assertEquals(Color.DARK_GRAY, plot.getXAxisLabelColor());		
206 		testFont(Font.ARIAL_SMALL, plot.getXAxisTickLabelFont());
207 		assertEquals(Color.DARK_GRAY, plot.getXAxisTickLabelColor());
208 		assertEquals("#,###.#", plot.getXAxisTickLabelMask());
209 		assertEquals(Color.DARK_GRAY, plot.getXAxisLineColor());
210 		
211 		assertNotNull(plot.getYAxisLabelExpression().getText());
212 		testFont(Font.ARIAL_SMALL, plot.getYAxisLabelFont());
213 		assertEquals(Color.DARK_GRAY, plot.getYAxisLabelColor());		
214 		testFont(Font.ARIAL_SMALL, plot.getYAxisTickLabelFont());
215 		assertEquals(Color.DARK_GRAY, plot.getYAxisTickLabelColor());
216 		assertEquals("#,##0.0", plot.getYAxisTickLabelMask());
217 		assertEquals(Color.DARK_GRAY, plot.getYAxisLineColor());
218 	}
219 	
220 	public DynamicReport buildReport() throws Exception {		
221 		return drb.build();
222 	}
223 
224 	private void testFont(Font djFont, JRFont jrFont) {
225 		assertEquals(djFont.getFontName(), jrFont.getFontName());
226 		assertEquals(djFont.getFontSize(), jrFont.getFontSize());
227 		assertEquals(djFont.isBold(), jrFont.isBold());
228 		assertEquals(djFont.isItalic(), jrFont.isItalic());
229 	}
230 	
231 	public static void main(String[] args) throws Exception {
232 		ScatterChartBuilderTest test = new ScatterChartBuilderTest();
233 		test.setUp();
234 		test.testReport();
235 		JasperViewer.viewReport(test.jp);
236 	}
237 }