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.XisSymbolic; 46 import org.jfree.data.YisSymbolic; 47 48 53 public class SampleXYSymbolicDataset extends AbstractSeriesDataset 54 implements XYDataset, XisSymbolic, YisSymbolic { 55 56 57 private String [] seriesName; 58 59 60 private int[] item; 61 62 63 private int serie; 64 65 66 private Integer [][] xValues; 67 68 69 private Integer [][] yValues; 70 71 72 private String [] xSymbolicValues; 73 74 75 private String [] ySymbolicValues; 76 77 78 private String datasetName; 79 80 90 public SampleXYSymbolicDataset(String datasetName, 91 Integer [][] xValues, 92 Integer [][] yValues, 93 String [] xSymbolicValues, 94 String [] ySymbolicValues, 95 String [] seriesName) { 96 97 this.datasetName = datasetName; 98 this.xValues = xValues; 99 this.yValues = yValues; 100 this.xSymbolicValues = xSymbolicValues; 101 this.ySymbolicValues = ySymbolicValues; 102 this.serie = xValues.length; 103 this.item = new int[serie]; 104 for (int i = 0; i < serie; i++) { 105 this.item[i] = xValues[i].length; 106 } 107 this.seriesName = seriesName; 108 109 } 110 111 120 public Number getXValue(int series, int item) { 121 return xValues[series][item]; 122 } 123 124 133 public Number getYValue(int series, int item) { 134 return yValues[series][item]; 135 } 136 137 148 public void setXValue(int series, int item, Number newValue) { 149 xValues[series][item] = (Integer ) newValue; 150 } 151 152 163 public void setYValue(int series, int item, Number newValue) { 164 yValues[series][item] = (Integer ) newValue; 165 } 166 167 172 public int getSeriesCount() { 173 return serie; 174 } 175 176 183 public String getSeriesName(int series) { 184 if (seriesName != null) { 185 return seriesName[series]; 186 } 187 else { 188 return datasetName + series; 189 } 190 } 191 192 198 public int getItemCount(int series) { 199 return item[series]; 200 } 201 202 207 public String [] getXSymbolicValues() { 208 return xSymbolicValues; 209 } 210 211 216 public String [] getYSymbolicValues() { 217 return ySymbolicValues; 218 } 219 220 225 public void setXSymbolicValues(String [] sValues) { 226 xSymbolicValues = sValues; 227 } 228 229 234 public void setYSymbolicValues(String [] sValues) { 235 ySymbolicValues = sValues; 236 } 237 238 247 public String getXSymbolicValue(int series, int item) { 248 Integer intValue = (Integer ) getXValue(series, item); 249 return getXSymbolicValue(intValue); 250 } 251 252 260 public String getYSymbolicValue(int series, int item) { 261 Integer intValue = (Integer ) getYValue(series, item); 262 return getYSymbolicValue(intValue); 263 } 264 265 272 public String getXSymbolicValue(Integer val) { 273 return xSymbolicValues[val.intValue()]; 274 } 275 276 283 public String getYSymbolicValue(Integer val) { 284 return ySymbolicValues[val.intValue()]; 285 } 286 287 303 public static String [] combineYSymbolicDataset(YisSymbolic dataset1, YisSymbolic dataset2) { 304 305 SampleXYSymbolicDataset sDataset1 = (SampleXYSymbolicDataset) dataset1; 306 SampleXYSymbolicDataset sDataset2 = (SampleXYSymbolicDataset) dataset2; 307 String [] sDatasetSymbolicValues1 = sDataset1.getYSymbolicValues(); 308 String [] sDatasetSymbolicValues2 = sDataset2.getYSymbolicValues(); 309 310 int s1length = sDatasetSymbolicValues1.length; 312 int s2length = sDatasetSymbolicValues2.length; 313 List ySymbolicValuesCombined = new Vector (); 314 for (int i = 0; i < s1length; i++) { 315 ySymbolicValuesCombined.add(sDatasetSymbolicValues1[i]); 316 } 317 for (int i = 0; i < s2length; i++) { 318 if (!ySymbolicValuesCombined.contains(sDatasetSymbolicValues2[i])) { 319 ySymbolicValuesCombined.add(sDatasetSymbolicValues2[i]); 320 } 321 } 322 323 int newIndex; 325 for (int i = 0; i < sDataset2.getSeriesCount(); i++) { 326 for (int j = 0; j < sDataset2.getItemCount(i); j++) { 327 newIndex = ySymbolicValuesCombined.indexOf(sDataset2.getYSymbolicValue(i, j)); 328 sDataset2.setYValue(i, j, new Integer (newIndex)); 329 } 330 } 331 332 String [] ySymbolicValuesCombinedA = new String [ySymbolicValuesCombined.size()]; 334 ySymbolicValuesCombined.toArray(ySymbolicValuesCombinedA); 335 sDataset1.setYSymbolicValues(ySymbolicValuesCombinedA); 336 sDataset2.setYSymbolicValues(ySymbolicValuesCombinedA); 337 338 return ySymbolicValuesCombinedA; 339 } 340 341 357 public static String [] combineXSymbolicDataset(XisSymbolic dataset1, XisSymbolic dataset2) { 358 SampleXYSymbolicDataset sDataset1 = (SampleXYSymbolicDataset) dataset1; 359 SampleXYSymbolicDataset sDataset2 = (SampleXYSymbolicDataset) dataset2; 360 String [] sDatasetSymbolicValues1 = sDataset1.getXSymbolicValues(); 361 String [] sDatasetSymbolicValues2 = sDataset2.getXSymbolicValues(); 362 363 int s1length = sDatasetSymbolicValues1.length; 365 int s2length = sDatasetSymbolicValues2.length; 366 List xSymbolicValuesCombined = new Vector (); 367 for (int i = 0; i < s1length; i++) { 368 xSymbolicValuesCombined.add(sDatasetSymbolicValues1[i]); 369 } 370 for (int i = 0; i < s2length; i++) { 371 if (!xSymbolicValuesCombined.contains(sDatasetSymbolicValues2[i])) { 372 xSymbolicValuesCombined.add(sDatasetSymbolicValues2[i]); 373 } 374 } 375 376 int newIndex; 378 for (int i = 0; i < sDataset2.getSeriesCount(); i++) { 379 for (int j = 0; j < sDataset2.getItemCount(i); j++) { 380 newIndex = xSymbolicValuesCombined.indexOf(sDataset2.getXSymbolicValue(i, j)); 381 sDataset2.setXValue(i, j, new Integer (newIndex)); 382 } 383 } 384 385 String [] xSymbolicValuesCombinedA = new String [xSymbolicValuesCombined.size()]; 387 xSymbolicValuesCombined.toArray(xSymbolicValuesCombinedA); 388 sDataset1.setXSymbolicValues(xSymbolicValuesCombinedA); 389 sDataset2.setXSymbolicValues(xSymbolicValuesCombinedA); 390 391 return xSymbolicValuesCombinedA; 392 } 393 394 399 public Object clone() { 400 String nDatasetName = new String (this.datasetName); 401 Integer [][] nXValues = (Integer [][]) cloneArray(this.xValues); 402 Integer [][] nYValues = (Integer [][]) cloneArray(this.yValues); 403 String [] nXSymbolicValues = (String []) cloneArray(this.xSymbolicValues); 404 String [] nYSymbolicValues = (String []) cloneArray(this.ySymbolicValues); 405 String [] seriesName = (String []) cloneArray(this.seriesName); 406 return new SampleXYSymbolicDataset(nDatasetName, nXValues, nYValues, 407 nXSymbolicValues, nYSymbolicValues, seriesName); 408 } 409 410 417 private static Object cloneArray(Object arr) { 418 419 if (arr == null) { 420 return arr; 421 } 422 423 Class cls = arr.getClass(); 424 if (!cls.isArray()) { 425 return arr; 426 } 427 428 int length = Array.getLength(arr); 429 Object [] newarr = (Object []) Array.newInstance(cls.getComponentType(), length); 430 431 Object obj; 432 433 for (int i = 0; i < length; i++) { 434 obj = Array.get(arr, i); 435 if (obj.getClass().isArray()) { 436 newarr[i] = cloneArray(obj); 437 } 438 else { 439 newarr[i] = obj; 440 } 441 } 442 443 return newarr; 444 } 445 446 } 447 | Popular Tags |