1 39 40 package org.jfree.chart.demo; 41 42 import java.awt.Color ; 43 import java.awt.GradientPaint ; 44 import java.lang.reflect.Array ; 45 46 import javax.swing.JFrame ; 47 48 import org.jfree.chart.ChartFrame; 49 import org.jfree.chart.JFreeChart; 50 import org.jfree.chart.axis.SymbolicAxis; 51 import org.jfree.chart.labels.SymbolicXYToolTipGenerator; 52 import org.jfree.chart.plot.CombinedDomainXYPlot; 53 import org.jfree.chart.plot.CombinedRangeXYPlot; 54 import org.jfree.chart.plot.XYPlot; 55 import org.jfree.chart.renderer.StandardXYItemRenderer; 56 import org.jfree.chart.renderer.XYItemRenderer; 57 import org.jfree.data.CombinedDataset; 58 import org.jfree.data.SubSeriesDataset; 59 import org.jfree.data.XYDataset; 60 import org.jfree.data.XisSymbolic; 61 import org.jfree.data.YisSymbolic; 62 import org.jfree.ui.RefineryUtilities; 63 64 69 public class SymbolicXYPlotDemo { 70 71 82 public static JFreeChart createXYSymbolicPlot(String title, String xAxisLabel, 83 String yAxisLabel, XYDataset data, 84 boolean legend) { 85 86 SymbolicAxis xSymbolicAxis 87 = new SymbolicAxis(xAxisLabel, ((XisSymbolic) data).getXSymbolicValues()); 88 89 SymbolicAxis ySymbolicAxis 90 = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data).getYSymbolicValues()); 91 92 XYPlot plot = new XYPlot(data, xSymbolicAxis, ySymbolicAxis, null); 93 XYItemRenderer renderer 94 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, 95 new SymbolicXYToolTipGenerator()); 96 plot.setRenderer(renderer); 97 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 98 return chart; 99 100 } 101 102 107 public static SampleYSymbolicDataset createYSymbolicSample1() { 108 109 String [] sData = {"Lion", "Elephant", "Monkey", "Hippopotamus", "Giraffe"}; 110 SampleYSymbolicDataset data = new SampleYSymbolicDataset("AY Sample", 20, sData, 4, 20, 111 new String [] {"Fall", "Spring", "Summer", "Winter"}); 112 return data; 113 114 } 115 116 121 public static SampleYSymbolicDataset createYSymbolicSample2() { 122 123 String [] sData = {"Giraffe", "Gazelle", "Zebra", "Gnu"}; 124 SampleYSymbolicDataset data = new SampleYSymbolicDataset("BY Sample", 40, sData, 4, 10, 125 new String [] {"Fall", "Spring", "Summer", "Winter"}); 126 return data; 127 128 } 129 130 135 public static SampleXYSymbolicDataset createXYSymbolicSample1() { 136 137 String [] xsData = {"Atmosphere", "Continental Ecosystem", 138 "Limnic Ecosystem", "Marine Ecosystem"}; 139 String [] ysData = {"Ionizing radiations", "Thermic pollutants", "Hydrocarbon", 140 "Synthetic materials", "Pesticides", "Detergent", 141 "Synthetic organic materials", "Sulphur", "Nitrate", "Phosphate", 142 "Heavy metals", "Fluors", "Aerosols", "Dead organic materials", 143 "Pathogen micro-organisms"}; 144 145 int[][] xd = {{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 146 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}}; 147 int[][] yd = {{0, 2, 3, 7, 10, 11, 12, 14, 0, 2, 3, 4, 7, 8, 9, 10, 11, 12, 14, 0, 1, 2, 3, 148 4, 5, 7, 8, 9, 10, 13, 14, 0, 1, 2, 3, 4, 5, 8, 9, 10, 13, 14}}; 149 Integer [][] xData = (Integer [][]) toArray(xd); 150 Integer [][] yData = (Integer [][]) toArray(yd); 151 SampleXYSymbolicDataset xySymbolicData = new SampleXYSymbolicDataset("AXY Sample", 152 xData, yData, xsData, ysData, new String [] {"A"}); 153 return xySymbolicData; 154 155 } 156 157 162 public static SampleXYSymbolicDataset createXYSymbolicSample2() { 163 164 String [] xsData = {"Physic pollutant", "Chemical pollutant", "Biological pollutant"}; 165 String [] ysData = {"Ionizing radiations", "Thermic pollutants", "Hydrocarbon", 166 "Synthetic materials", "Pesticides", "Detergent", 167 "Synthetic organic materials", "Sulphur", "Nitrate", "Phosphate", 168 "Heavy metals", "Fluors", "Aerosols", "Dead organic materials", 169 "Pathogen micro-organisms"}; 170 171 int[][] xd = {{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2}}; 172 int[][] yd = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}}; 173 Integer [][] xData = (Integer [][]) toArray(xd); 174 Integer [][] yData = (Integer [][]) toArray(yd); 175 SampleXYSymbolicDataset xySymbolicData = new SampleXYSymbolicDataset("BXY Sample", 176 xData, yData, xsData, ysData, new String [] {"B"}); 177 return xySymbolicData; 178 179 } 180 181 190 private static void displayXYSymbolic(String frameTitle, 191 XYDataset data, String chartTitle, 192 String xAxisLabel, String yAxisLabel) { 193 194 JFreeChart chart = createXYSymbolicPlot(chartTitle, xAxisLabel, yAxisLabel, data, true); 195 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 1000, 0, Color.green)); 196 JFrame frame = new ChartFrame(frameTitle, chart); 197 frame.pack(); 198 RefineryUtilities.positionFrameRandomly(frame); 199 frame.show(); 200 201 } 202 203 210 private static void displayXYSymbolicOverlaid(String frameTitle, 211 XYDataset data1, XYDataset data2) { 212 213 String title = "Pollutant Overlaid"; 214 String xAxisLabel = "Contamination and Type"; 215 String yAxisLabel = "Pollutant"; 216 217 String [] combinedXSymbolicValues 219 = SampleXYSymbolicDataset.combineXSymbolicDataset((XisSymbolic) data1, 220 (XisSymbolic) data2); 221 222 String [] combinedYSymbolicValues 224 = SampleXYSymbolicDataset.combineYSymbolicDataset((YisSymbolic) data1, 225 (YisSymbolic) data2); 226 227 CombinedDataset data = new CombinedDataset(); 229 data.add(data1); 230 data.add(data2); 231 232 XYDataset series0 = new SubSeriesDataset(data, 0); 234 XYDataset series1 = new SubSeriesDataset(data, 1); 235 236 SymbolicAxis hsymbolicAxis = new SymbolicAxis(xAxisLabel, combinedXSymbolicValues); 238 SymbolicAxis vsymbolicAxis = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues); 239 240 XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 241 XYPlot plot = new XYPlot(series0, hsymbolicAxis, vsymbolicAxis, renderer1); 242 243 XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 244 plot.setSecondaryDataset(0, series1); 245 plot.setSecondaryRenderer(0, renderer2); 246 247 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); 249 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.blue)); 250 251 JFrame frame = new ChartFrame(frameTitle, chart); 253 frame.pack(); 254 RefineryUtilities.positionFrameRandomly(frame); 255 frame.show(); 256 257 } 258 259 266 private static void displayXYSymbolicCombinedHorizontally(String frameTitle, 267 XYDataset data1, XYDataset data2) { 268 269 String title = "Pollutant Horizontally Combined"; 270 String x1AxisLabel = "Contamination"; 271 String x2AxisLabel = "Type"; 272 String yAxisLabel = "Pollutant"; 273 274 String [] combinedYSymbolicValues = 276 SampleXYSymbolicDataset.combineYSymbolicDataset((YisSymbolic) data1, 277 (YisSymbolic) data2); 278 279 CombinedDataset data = new CombinedDataset(); 281 data.add(data1); 282 data.add(data2); 283 284 XYDataset series0 = new SubSeriesDataset(data, 0); 286 XYDataset series1 = new SubSeriesDataset(data, 1); 287 288 JFreeChart chart = null; 289 290 SymbolicAxis hsymbolicAxis0 292 = new SymbolicAxis(x1AxisLabel, ((XisSymbolic) data1).getXSymbolicValues()); 293 SymbolicAxis hsymbolicAxis1 294 = new SymbolicAxis(x2AxisLabel, ((XisSymbolic) data2).getXSymbolicValues()); 295 SymbolicAxis symbolicAxis 296 = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues); 297 298 CombinedRangeXYPlot mainPlot = new CombinedRangeXYPlot(symbolicAxis); 300 301 XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 303 XYPlot subplot0 = new XYPlot(series0, hsymbolicAxis0, null, renderer); 304 XYPlot subplot1 = new XYPlot(series1, hsymbolicAxis1, null, renderer); 305 mainPlot.add(subplot0, 1); 306 mainPlot.add(subplot1, 1); 307 308 chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true); 310 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.blue)); 311 312 JFrame frame = new ChartFrame(frameTitle, chart); 314 frame.pack(); 315 RefineryUtilities.positionFrameRandomly(frame); 316 frame.show(); 317 318 } 319 320 331 338 private static void displayXYSymbolicCombinedVertically(String frameTitle, 339 XYDataset data1, XYDataset data2) { 340 341 String title = "Pollutant Vertically Combined"; 342 String xAxisLabel = "Contamination and Type"; 343 String yAxisLabel = "Pollutant"; 344 345 String [] combinedXSymbolicValues 347 = SampleXYSymbolicDataset.combineXSymbolicDataset((XisSymbolic) data1, 348 (XisSymbolic) data2); 349 350 CombinedDataset data = new CombinedDataset(); 352 data.add(data1); 353 data.add(data2); 354 355 XYDataset series0 = new SubSeriesDataset(data, 0); 357 XYDataset series1 = new SubSeriesDataset(data, 1); 358 359 SymbolicAxis hsymbolicAxis 361 = new SymbolicAxis(xAxisLabel, combinedXSymbolicValues); 362 363 SymbolicAxis vsymbolicAxis0 364 = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data1).getYSymbolicValues()); 365 366 SymbolicAxis vsymbolicAxis1 367 = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data2).getYSymbolicValues()); 368 369 CombinedDomainXYPlot mainPlot = new CombinedDomainXYPlot(hsymbolicAxis); 371 372 XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 374 XYPlot subplot0 = new XYPlot(series0, null, vsymbolicAxis0, renderer); 375 XYPlot subplot1 = new XYPlot(series1, null, vsymbolicAxis1, renderer); 376 377 mainPlot.add(subplot0, 1); 378 mainPlot.add(subplot1, 1); 379 380 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true); 382 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.blue)); 383 384 JFrame frame = new ChartFrame(frameTitle, chart); 386 frame.pack(); 387 RefineryUtilities.positionFrameRandomly(frame); 388 frame.show(); 389 390 } 391 392 399 private static Object toArray(Object arr) { 400 401 if (arr == null) { 402 return arr; 403 } 404 405 Class cls = arr.getClass(); 406 if (!cls.isArray()) { 407 return arr; 408 } 409 410 Class compType = cls.getComponentType(); 411 int dim = 1; 412 while (!compType.isPrimitive()) { 413 if (!compType.isArray()) { 414 return arr; 415 } 416 else { 417 dim++; 418 compType = compType.getComponentType(); 419 } 420 } 421 422 int[] length = new int[dim]; 423 length[0] = Array.getLength(arr); 424 Object [] newarr = null; 425 426 try { 427 if (compType.equals(Integer.TYPE)) { 428 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Integer"), length); 429 } 430 else if (compType.equals(Double.TYPE)) { 431 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Double"), length); 432 } 433 else if (compType.equals(Long.TYPE)) { 434 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Long"), length); 435 } 436 else if (compType.equals(Float.TYPE)) { 437 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Float"), length); 438 } 439 else if (compType.equals(Short.TYPE)) { 440 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Short"), length); 441 } 442 else if (compType.equals(Byte.TYPE)) { 443 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Byte"), length); 444 } 445 else if (compType.equals(Character.TYPE)) { 446 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Character"), length); 447 } 448 else if (compType.equals(Boolean.TYPE)) { 449 newarr = (Object []) Array.newInstance(Class.forName("java.lang.Boolean"), length); 450 } 451 } 452 catch (ClassNotFoundException ex) { 453 System.out.println(ex); 454 } 455 456 for (int i = 0; i < length[0]; i++) { 457 if (dim != 1) { 458 newarr[i] = toArray(Array.get(arr, i)); 459 } 460 else { 461 newarr[i] = Array.get(arr, i); 462 } 463 } 464 return newarr; 465 } 466 467 472 public static void main(String [] args) { 473 474 SampleXYSymbolicDataset s1 = createXYSymbolicSample1(); 475 SampleXYSymbolicDataset s2 = createXYSymbolicSample2(); 476 477 displayXYSymbolic("Example 1", s1, "Pollutant", "contamination", "pollutant"); 478 479 displayXYSymbolic("Example 2", s2, "Pollutant", "type", "pollutant"); 480 481 displayXYSymbolicCombinedHorizontally("Example 3", (SampleXYSymbolicDataset) s1.clone(), 482 (SampleXYSymbolicDataset) s2.clone()); 483 484 displayXYSymbolicCombinedVertically("Example 4", (SampleXYSymbolicDataset) s1.clone(), 485 (SampleXYSymbolicDataset) s2.clone()); 486 487 displayXYSymbolicOverlaid("Example 5", (SampleXYSymbolicDataset) s1.clone(), 488 (SampleXYSymbolicDataset) s2.clone()); 489 490 } 491 492 } 493 | Popular Tags |