View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.database.chart;
17  
18  import java.util.List;
19  
20  import org.jfree.chart.ChartFactory;
21  import org.jfree.chart.JFreeChart;
22  import org.jfree.chart.plot.PlotOrientation;
23  import org.jfree.data.category.CategoryDataset;
24  import org.jfree.data.general.DatasetUtilities;
25  import org.jfree.data.general.PieDataset;
26  import org.seasar.tuigwaa.database.DataRow;
27  import org.seasar.tuigwaa.database.DataTable;
28  import org.seasar.tuigwaa.database.SpreadSheet;
29  import org.seasar.tuigwaa.util.TgwResource;
30  
31  
32  public class TCharFactory {
33  
34  	public static final JFreeChart createPieChart(DataTable dataTable) {
35  
36  		int columnSize = dataTable.getColumnSize();
37  		JFreeChart pieChart = null;
38  
39  		if (columnSize == 2) {
40  
41  			CategoryDataset cData = createCategoryDataset(dataTable);
42  			PieDataset pieData = DatasetUtilities.createPieDatasetForRow(cData,
43  					0);
44  
45  			pieChart = ChartFactory.createPieChart(dataTable.getName(),
46  					pieData, true, true, true);
47  
48  			// PiePlot piePlot = (PiePlot) pieChart.getPlot();
49  			// piePlot.setCircular(false);
50  			// piePlot.setExplodePercent(1, 0.5);
51  
52  		}
53  		return pieChart;
54  	}
55  
56  	public static JFreeChart createBarChart(DataTable dataTable) {
57  		CategoryDataset cData = createCategoryDataset(dataTable);
58  		String dataLabel = getDataLabel(dataTable);
59  		String columnLabel = getColumnLabel(dataTable);
60  
61  		JFreeChart barChart = ChartFactory.createBarChart(dataTable.getName(),
62  				columnLabel, dataLabel, cData, PlotOrientation.VERTICAL, true,
63  				true, true);
64  		return barChart;
65  	}
66  
67  	public static JFreeChart createLineChart(DataTable dataTable) {
68  		CategoryDataset cData = createCategoryDataset(dataTable);
69  		String dataLabel = getDataLabel(dataTable);
70  		String columnLabel = getColumnLabel(dataTable);
71  		JFreeChart chart = ChartFactory.createLineChart(dataTable.getName(),
72  				columnLabel, dataLabel, cData, PlotOrientation.VERTICAL, true,
73  				true, false);
74  		return chart;
75  	}
76  
77  	public static final CategoryDataset createCategoryDataset(
78  			DataTable dataTable) {
79  
80  		CategoryDataset dataset = null;
81  
82  		if (dataTable instanceof SpreadSheet) {
83  			int columnSize = dataTable.getColumnSize();
84  			int rowSize = dataTable.getRowSize();
85  			dataset = createCategoryDataset((SpreadSheet) dataTable, rowSize,
86  					columnSize);
87  		} else {
88  			int rowSize = dataTable.getRowSize();
89  			dataset = createCategoryDataset(dataTable, rowSize);
90  
91  		}
92  
93  		return dataset;
94  	}
95  
96  	public static final CategoryDataset createCategoryDataset(
97  			SpreadSheet sheet, int rowSize, int columnSize) {
98  
99  		double[][] data = new double[rowSize][columnSize];
100 		Comparable[] rowKeys = new Comparable[rowSize];
101 		Comparable[] columnKeys = new Comparable[columnSize];
102 		List rowHeaders = sheet.getRowHeaders();
103 		List columnHeaders = sheet.getColumnHeaders();
104 
105 		for (int row = 0; row < rowSize; row++) {
106 			rowKeys[row] = (Comparable) rowHeaders.get(row);
107 			DataRow dataRow = sheet.nextRow();
108 			for (int column = 0; column < columnSize; column++) {
109 				if (row == 0) {
110 					columnKeys[column] = (Comparable) columnHeaders.get(column);
111 				}
112 				Object obj = dataRow.next();
113 				if (obj != null) {
114 					data[row][column] = Double.parseDouble(String.valueOf(obj));
115 				} else {
116 					data[row][column] = 0.0;
117 				}
118 			}
119 		}
120 		return DatasetUtilities
121 				.createCategoryDataset(rowKeys, columnKeys, data);
122 	}
123 
124 	public static final CategoryDataset createCategoryDataset(
125 			DataTable dataTable, int rowSize) {
126 		double[][] data = new double[1][rowSize];
127 		Comparable[] rowKeys = new Comparable[1];
128 		Comparable[] columnKeys = new Comparable[rowSize];
129 		
130 		rowKeys[0] = (String)dataTable.getHeaders().get(1);
131 
132 		for (int i = 0; dataTable.hasNextRow(); i++) {
133 			DataRow dataRow = dataTable.nextRow();
134 			Comparable columnKey = (Comparable) dataRow.getCell(0);
135 			if(columnKey == null){
136 				columnKey  = TgwResource.getMessage("msg.nodata");
137 			}
138 			columnKeys[i] = columnKey;
139 			Object obj = dataRow.getCell(1);
140 			if (obj != null) {
141 				data[0][i] = Double.parseDouble(String.valueOf(obj));
142 			} else {
143 				data[0][i] = 0.0;
144 			}
145 		}
146 		return DatasetUtilities
147 				.createCategoryDataset(rowKeys, columnKeys, data);
148 	}
149 
150 	public static String getRawLabel(DataTable dataTable) {
151 		if (dataTable.isSpreadSheet()) {
152 			return "";//((SpreadSheet) dataTable).getRowLabel();
153 		} else {
154 			return "";
155 		}
156 	}
157 
158 	public static String getColumnLabel(DataTable dataTable) {
159 		if (dataTable.isSpreadSheet()) {
160 			return "";//((SpreadSheet) dataTable).getColumnLabel();
161 		} else {
162 			return "";
163 		}
164 	}
165 
166 	public static String getDataLabel(DataTable dataTable) {
167 		if (dataTable.isSpreadSheet()) {
168 			return "";//((SpreadSheet) dataTable).getDataLabel();
169 		} else {
170 			return "";
171 			//return dataTable.get;
172 		}
173 	}
174 }