1 package com.calipso.reportgenerator.userinterface; 2 import javax.swing.*; 3 import com.calipso.reportgenerator.common.HeaderTableModel; 4 import com.calipso.reportgenerator.common.LanguageTraslator; 5 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 6 import org.jfree.data.category.DefaultCategoryDataset; 7 8 12 public class ChartData { 13 14 private JTable dataTable; 15 private HeaderTableModel rowHeaderTableModel; 16 private HeaderTableModel columnHeaderTableModel; 17 18 23 public ChartData(JTable dataTable, HeaderTableModel rowHeaderTableModel, HeaderTableModel columnHeaderTableModel){ 24 this.dataTable = dataTable; 25 this.rowHeaderTableModel = rowHeaderTableModel; 26 this.columnHeaderTableModel = columnHeaderTableModel; 27 } 28 29 34 public double[] getChartDataArray(){ 35 double [] chartDataArray = null; 36 int [] selectedRows = dataTable.getSelectedRows(); 37 int [] selectedColumns = dataTable.getSelectedColumns(); 38 39 double total=0; 40 if(selectedRows.length > 1){ 41 chartDataArray = new double [selectedRows.length]; 42 int selectedColumn = dataTable.getSelectedColumn(); 43 for(int i=0 ; i<selectedRows.length ; i++){ 44 chartDataArray[i] = Double.valueOf(dataTable.getValueAt(selectedRows[i], selectedColumn).toString()).doubleValue(); 45 total = total + chartDataArray[i]; 46 } 47 48 }else { 49 chartDataArray = new double [selectedColumns.length]; 50 int selectedRow = dataTable.getSelectedRow(); 51 for(int i=0 ; i<selectedColumns.length ; i++){ 52 chartDataArray[i] = Double.valueOf(dataTable.getValueAt(selectedRow, selectedColumns[i]).toString()).doubleValue(); 53 total = total + chartDataArray[i]; 54 } 55 56 } 57 58 for(int i=0 ; i<chartDataArray.length ; i++){ 59 chartDataArray[i] = (chartDataArray[i]*100) / total; 60 } 61 62 return chartDataArray; 63 } 64 65 70 public String [] getDescriptionForChart(Boolean byRow){ 71 int [] selectedRows = dataTable.getSelectedRows(); 72 int [] selectedColumns = dataTable.getSelectedColumns(); 73 String [] descChartPie; 74 if(((selectedRows.length > 1) && (byRow==null)) || ((byRow !=null) && (byRow.booleanValue()))){ 75 descChartPie = new String [selectedRows.length]; 76 for(int i=0 ; i<descChartPie.length ; i++ ){ 77 Object [] rowsDesc = rowHeaderTableModel.getValueFrom(selectedRows[i]); 78 descChartPie[i] = ""; 79 for(int j=0 ; j< rowsDesc.length ; j++){ 80 if(rowsDesc[j] != null){ 81 descChartPie[i] = descChartPie[i] + rowsDesc[j].toString() + " "; 82 } else{ 83 descChartPie[i] = descChartPie[i] + " TOTAL"; 84 } 85 } 86 } 87 }else{ 88 descChartPie = new String [selectedColumns.length]; 89 for(int i=0 ; i<descChartPie.length ; i++ ){ 90 Object [] colsDesc = columnHeaderTableModel.getValueFrom(selectedColumns[i]); 91 for(int j=0 ; j< colsDesc.length ; j++){ 92 descChartPie[i] = ""; 93 if(colsDesc != null){ 94 descChartPie[i] = descChartPie[i] + colsDesc[j].toString() + "/"; 95 }else{ 96 descChartPie[i] = descChartPie[i] + " TOTAL"; 97 } 98 } 99 } 100 } 101 return descChartPie; 102 } 103 104 110 public DefaultCategoryDataset getDatasetForChart(boolean isRow){ 111 DefaultCategoryDataset dataset = null; 112 int [] selectedRows = dataTable.getSelectedRows(); 113 int [] selectedColumns = dataTable.getSelectedColumns(); 114 if((selectedRows.length == 0 && selectedColumns.length == 0) || 115 (selectedRows.length == 1 && selectedColumns.length == 1)) { 116 int [] rows = getIndexes(dataTable.getRowCount()); 117 int [] cols = getIndexes(dataTable.getColumnCount()); 118 dataset = getDataForChart(cols, rows, isRow); 119 } else { 120 dataset = getDataForChart(selectedColumns,selectedRows, isRow); 121 } 122 return dataset; 123 } 124 125 private int[] getIndexes(int count) { 126 int [] objects = new int[count]; 127 for(int i = 0 ; i < count ; i++) { 128 objects [i] = i; 129 } 130 return objects; 131 } 132 133 140 private DefaultCategoryDataset getDataForChart(int [] firstList, int[] secondList, boolean isRow){ 141 float numbersArray=0; 142 String factorArray=null; 143 String seriesArray=null; 144 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 145 for(int i = 0 ; i < firstList.length ; i++){ 146 if(isRow){ 147 seriesArray = getDescription(columnHeaderTableModel.getValueFrom(firstList[i])); 148 }else{ 149 factorArray = getDescription(columnHeaderTableModel.getValueFrom(firstList[i])); 150 } 151 for(int j = 0 ; j < secondList.length ; j++){ 152 if(((dataTable.getValueAt(secondList[j], firstList[i]) == null))||((((SharedFloat)dataTable.getValueAt(secondList[j], firstList[i])).getValue().equals(new Float (Float.NaN))))){ 153 numbersArray = 0; 154 }else { 155 numbersArray = Float.valueOf(dataTable.getValueAt(secondList[j], firstList[i]).toString()).floatValue(); 156 } 157 if(isRow){ 158 factorArray = getDescription(rowHeaderTableModel.getValueFrom(secondList[j])); 159 }else { 160 seriesArray = getDescription(rowHeaderTableModel.getValueFrom(secondList[j])); 161 } 162 dataset.addValue(numbersArray, seriesArray , factorArray); 163 } 164 } 165 return dataset; 166 } 167 168 173 private String getDescription(Object [] descArray) { 174 String description = ""; 175 for(int i = 0 ; i < descArray.length ; i++){ 176 if(descArray[i] != null){ 177 description = description + descArray[i].toString() + " "; 178 }else{ 179 description = description + LanguageTraslator.traslate("358"); 180 } 181 } 182 return description; 183 } 184 185 } 186 | Popular Tags |