1 23 24 package de.progra.charting.model; 25 26 import de.progra.charting.CoordSystem; 27 import de.progra.charting.ChartUtilities; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.TreeSet ; 31 import java.util.Set ; 32 import java.util.HashMap ; 33 34 40 public class DefaultChartDataModel extends AbstractChartDataModel { 41 42 43 protected TreeSet columnSet = new TreeSet (); 44 45 46 protected ArrayList data = new ArrayList (); 47 48 49 protected HashMap valuesbyaxis = new HashMap (); 50 51 52 protected ChartDataModelConstraints constraints1, constraints2; 53 54 56 public DefaultChartDataModel() { 57 TreeSet set1 = new TreeSet (); 58 valuesbyaxis.put(new Integer (CoordSystem.FIRST_YAXIS), set1); 59 TreeSet set2 = new TreeSet (); 60 valuesbyaxis.put(new Integer (CoordSystem.SECOND_YAXIS), set2); 61 constraints1 = new DefaultChartDataModelConstraints(this, CoordSystem.FIRST_YAXIS); 62 constraints2 = new DefaultChartDataModelConstraints(this, CoordSystem.SECOND_YAXIS); 63 } 64 65 74 public DefaultChartDataModel(Number [][] data, 75 double[] columns, 76 String [] rows) { 77 this(); 78 79 columnSet.addAll(Arrays.asList(ChartUtilities.transformArray(columns))); 80 81 TreeSet set = (TreeSet )valuesbyaxis.get(new Integer (CoordSystem.FIRST_YAXIS)); 82 83 ChartUtilities.addDataToSet(set, data); 84 trimSet(set); 85 86 for(int i = 0; i < data.length; i++) { 87 this.data.add(new DefaultDataSet(data[i], 88 ChartUtilities.transformArray(columns), 89 CoordSystem.FIRST_YAXIS, 90 rows[i])); 91 } 92 } 93 94 103 public DefaultChartDataModel(int[][] data, 104 double[] columns, 105 String [] rows) { 106 this(); 107 108 Number [][] numdata = ChartUtilities.transformArray(data); 109 110 columnSet.addAll(Arrays.asList(ChartUtilities.transformArray(columns))); 111 112 TreeSet set = 113 (TreeSet )valuesbyaxis.get(new Integer (CoordSystem.FIRST_YAXIS)); 114 115 ChartUtilities.addDataToSet(set, numdata); 116 117 trimSet(set); 118 119 for(int i = 0; i < data.length; i++) { 120 this.data.add(new DefaultDataSet(numdata[i], 121 ChartUtilities.transformArray(columns), 122 CoordSystem.FIRST_YAXIS, 123 rows[i])); 124 } 125 } 126 127 136 public DefaultChartDataModel(double[][] data, 137 double[] columns, 138 String [] rows) { 139 this(); 140 141 Number [][] numdata = ChartUtilities.transformArray(data); 142 143 columnSet.addAll(Arrays.asList(ChartUtilities.transformArray(columns))); 144 145 TreeSet set = 146 (TreeSet )valuesbyaxis.get(new Integer (CoordSystem.FIRST_YAXIS)); 147 148 ChartUtilities.addDataToSet(set, numdata); 149 trimSet(set); 150 for(int i = 0; i < data.length; i++) { 151 this.data.add(new DefaultDataSet(numdata[i], 152 ChartUtilities.transformArray(columns), 153 CoordSystem.FIRST_YAXIS, 154 rows[i])); 155 } 156 } 157 158 163 public DefaultChartDataModel(DataSet[] ds) { 164 this(); 165 166 TreeSet set; 167 for(int i = 0; i < ds.length; i++) { 168 data.add(ds[i]); 169 set = (TreeSet )valuesbyaxis.get(new Integer (ds[i].getYAxis())); 170 for(int j = 0; j < ds[i].getDataSetLength(); j++) { 171 columnSet.add(ds[i].getColumnValueAt(j)); 172 set.add(ds[i].getValueAt(j)); 173 174 trimSet(set); 175 } 176 } 177 } 178 179 183 public int getDataSetLength(int set) { 184 return ((DataSet)data.get(set)).getDataSetLength(); 185 } 186 187 190 public int getDataSetNumber() { 191 return data.size(); 192 } 193 194 199 public String getDataSetName(int set) { 200 return ((DataSet)data.get(set)).getTitle(); 201 } 202 203 207 public int getAxisBinding(int set) { 208 return ((DataSet)data.get(set)).getYAxis(); 209 } 210 211 214 public boolean isColumnNumeric() { 215 return true; 216 } 217 218 221 public Class getColumnClass() { 222 return Double .class; 223 } 224 225 230 public Number getValueAt(int set, int index) { 231 return (Number )((DataSet)data.get(set)).getValueAt(index); 232 } 233 234 235 239 public Object getColumnValueAt(int col) { 240 return null; 241 } 242 243 248 public Object getColumnValueAt(int set, int col) { 249 if(col < getDataSetLength(set)) 251 return new Double (((Number )((DataSet)data.get(set)).getColumnValueAt(col)).doubleValue()); 252 else 253 return null; 254 } 255 256 257 265 public ChartDataModelConstraints getChartDataModelConstraints(final int axis) { 266 if(axis == CoordSystem.FIRST_YAXIS) 267 return constraints1; 268 else 269 return constraints2; 270 } 271 272 278 public void setChartDataModelConstraints(int axis, ChartDataModelConstraints constraints) { 279 if(axis == CoordSystem.FIRST_YAXIS) 280 constraints1 = constraints; 281 else 282 constraints2 = constraints; 283 } 284 285 290 protected void trimSet(TreeSet s) { 291 while(((Number )s.first()).doubleValue() == Double.NEGATIVE_INFINITY) { 292 s.remove(s.first()); 293 } 294 double last = ((Number )s.last()).doubleValue(); 295 296 while(last == Double.POSITIVE_INFINITY 297 || last != last) { 298 s.remove(s.last()); 299 last = ((Number )s.last()).doubleValue(); 300 } 301 } 302 303 306 protected TreeSet getOrderedValues(int axis) { 307 return (TreeSet )valuesbyaxis.get(new Integer (axis)); 308 } 309 310 311 protected double getFirstColumnValue() { 312 return ((Number )columnSet.first()).doubleValue(); 313 } 314 315 316 protected double getLastColumnValue() { 317 return ((Number )columnSet.last()).doubleValue(); 318 } 319 } 320 | Popular Tags |