1 36 37 package org.jfree.chart.demo; 38 39 import java.lang.reflect.Array ; 40 import java.util.List ; 41 import java.util.Vector ; 42 43 import org.jfree.data.AbstractSeriesDataset; 44 import org.jfree.data.XYDataset; 45 import org.jfree.data.YisSymbolic; 46 47 53 public class SampleYSymbolicDataset extends AbstractSeriesDataset 54 implements XYDataset, YisSymbolic { 55 56 57 private static final int DEFAULT_SERIES_COUNT = 1; 58 59 60 private static final int DEFAULT_ITEM_COUNT = 50; 61 62 63 private int serie; 64 65 66 private int item; 67 68 69 private String [] serieNames; 70 71 72 private Double [][] xValues; 73 74 75 private Integer [][] yValues; 76 77 78 private String [] ySymbolicValues; 79 80 81 private String datasetName; 82 83 90 public SampleYSymbolicDataset(String datasetName, int xRange, String [] tabString) { 91 this(datasetName, xRange, tabString, DEFAULT_SERIES_COUNT, DEFAULT_ITEM_COUNT, null); 92 } 93 94 104 public SampleYSymbolicDataset(String datasetName, 105 int xRange, 106 String [] tabString, 107 int seriesCount, 108 int itemCount, 109 String [] serieNames) { 110 111 this.datasetName = datasetName; 112 this.ySymbolicValues = tabString; 113 this.serie = seriesCount; 114 this.item = itemCount; 115 this.serieNames = serieNames; 116 this.xValues = new Double [seriesCount][itemCount]; 117 this.yValues = new Integer [seriesCount][itemCount]; 118 119 for (int s = 0; s < seriesCount; s++) { 120 for (int i = 0; i < itemCount; i++) { 121 double x = Math.random() * xRange; 122 double y = Math.random() * tabString.length; 123 xValues[s][i] = new Double (x); 124 yValues[s][i] = new Integer ((int) y); 125 } 126 } 127 } 128 129 140 public SampleYSymbolicDataset(String datasetName, 141 Double [][] xValues, 142 Integer [][] yValues, 143 String [] ySymbolicValues, 144 int seriesCount, 145 int itemCount, 146 String [] serieNames) { 147 148 this.datasetName = datasetName; 149 this.xValues = xValues; 150 this.yValues = yValues; 151 this.ySymbolicValues = ySymbolicValues; 152 this.serie = seriesCount; 153 this.item = itemCount; 154 this.serieNames = serieNames; 155 156 } 157 158 159 168 public Number getXValue(int series, int item) { 169 return xValues[series][item]; 170 } 171 172 181 public Number getYValue(int series, int item) { 182 return yValues[series][item]; 183 } 184 185 196 public void setYValue(int series, int item, Number newValue) { 197 yValues[series][item] = (Integer ) newValue; 198 } 199 200 205 public int getSeriesCount() { 206 return this.serie; 207 } 208 209 216 public String getSeriesName(int series) { 217 if (serieNames != null) { 218 return serieNames[series]; 219 } 220 else { 221 return datasetName + series; 222 } 223 } 224 225 232 public int getItemCount(int series) { 233 return this.item; 234 } 235 236 241 public String [] getYSymbolicValues() { 242 return ySymbolicValues; 243 } 244 245 250 public void setYSymbolicValues(String [] sValues) { 251 ySymbolicValues = sValues; 252 } 253 254 263 public String getYSymbolicValue(int series, int item) { 264 Integer intValue = (Integer ) getYValue(series, item); 265 return getYSymbolicValue(intValue); 266 } 267 268 275 public String getYSymbolicValue(Integer val) { 276 return ySymbolicValues[val.intValue()]; 277 } 278 279 295 public static String [] combineYSymbolicDataset(YisSymbolic dataset1, YisSymbolic dataset2) { 296 297 SampleYSymbolicDataset sDataset1 = (SampleYSymbolicDataset) dataset1; 298 SampleYSymbolicDataset sDataset2 = (SampleYSymbolicDataset) dataset2; 299 String [] sDatasetSymbolicValues1 = sDataset1.getYSymbolicValues(); 300 String [] sDatasetSymbolicValues2 = sDataset2.getYSymbolicValues(); 301 302 int s1length = sDatasetSymbolicValues1.length; 304 int s2length = sDatasetSymbolicValues2.length; 305 List ySymbolicValuesCombined = new Vector (); 306 for (int i = 0; i < s1length; i++) { 307 ySymbolicValuesCombined.add(sDatasetSymbolicValues1[i]); 308 } 309 for (int i = 0; i < s2length; i++) { 310 if (!ySymbolicValuesCombined.contains(sDatasetSymbolicValues2[i])) { 311 ySymbolicValuesCombined.add(sDatasetSymbolicValues2[i]); 312 } 313 } 314 315 int newIndex; 317 for (int i = 0; i < sDataset2.getSeriesCount(); i++) { 318 for (int j = 0; j < sDataset2.getItemCount(i); j++) { 319 newIndex = ySymbolicValuesCombined.indexOf(sDataset2.getYSymbolicValue(i, j)); 320 sDataset2.setYValue(i, j, new Integer (newIndex)); 321 } 322 } 323 324 String [] ySymbolicValuesCombinedA = new String [ySymbolicValuesCombined.size()]; 326 ySymbolicValuesCombined.toArray(ySymbolicValuesCombinedA); 327 sDataset1.setYSymbolicValues(ySymbolicValuesCombinedA); 328 sDataset2.setYSymbolicValues(ySymbolicValuesCombinedA); 329 330 return ySymbolicValuesCombinedA; 331 } 332 333 338 public Object clone() { 339 String nDatasetName = new String (this.datasetName); 340 Double [][] nXValues = (Double [][]) cloneArray(this.xValues); 341 Integer [][] nYValues = (Integer [][]) cloneArray(this.yValues); 342 String [] nYSymbolicValues = (String []) cloneArray(this.ySymbolicValues); 343 int serie = this.serie; 344 int item = this.item; 345 String [] serieNames = (String []) cloneArray(this.serieNames); 346 return new SampleYSymbolicDataset(nDatasetName, nXValues, nYValues, 347 nYSymbolicValues, serie, item, serieNames); 348 } 349 350 357 private static Object cloneArray(Object arr) { 358 359 if (arr == null) { 360 return arr; 361 } 362 363 Class cls = arr.getClass(); 364 if (!cls.isArray()) { 365 return arr; 366 } 367 368 int length = Array.getLength(arr); 369 Object [] newarr = (Object []) Array.newInstance(cls.getComponentType(), length); 370 371 Object obj; 372 373 for (int i = 0; i < length; i++) { 374 obj = Array.get(arr, i); 375 if (obj.getClass().isArray()) { 376 newarr[i] = cloneArray(obj); 377 } 378 else { 379 newarr[i] = obj; 380 } 381 } 382 383 return newarr; 384 } 385 386 } 387 | Popular Tags |