1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 import java.awt.GradientPaint ; 42 import java.util.Date ; 43 44 import org.jfree.chart.ChartPanel; 45 import org.jfree.chart.JFreeChart; 46 import org.jfree.chart.axis.ColorBar; 47 import org.jfree.chart.axis.DateAxis; 48 import org.jfree.chart.axis.LogarithmicAxis; 49 import org.jfree.chart.axis.NumberAxis; 50 import org.jfree.chart.axis.ValueAxis; 51 import org.jfree.chart.plot.ContourPlot; 52 import org.jfree.data.ContourDataset; 53 import org.jfree.data.DefaultContourDataset; 54 import org.jfree.ui.ApplicationFrame; 55 import org.jfree.ui.RefineryUtilities; 56 57 65 public class ContourPlotDemo extends ApplicationFrame { 66 67 68 private ValueAxis xAxis = null; 69 70 71 private NumberAxis yAxis = null; 72 73 74 private ColorBar zColorBar = null; 75 76 77 79 80 private static boolean xIsDate = false; 81 82 83 private static boolean xIsLog = false; 84 85 86 private static boolean yIsLog = false; 87 88 89 private static boolean zIsLog = false; 90 91 92 private static boolean xIsInverted = false; 93 94 95 private static boolean yIsInverted = false; 96 97 98 private static boolean zIsInverted = false; 99 100 101 private static boolean makeHoles = false; 102 103 104 private static int numX = 10; 105 106 107 private static int numY = 20; 108 109 110 private static double ratio = 0.0; 111 112 113 public ChartPanel panel = null; 114 115 116 121 public ContourPlotDemo(String title) { 122 123 super(title); 124 125 JFreeChart chart = createContourPlot(); 126 panel = new ChartPanel(chart, true, true, true, true, true); 127 panel.setPreferredSize(new java.awt.Dimension (500, 270)); 128 panel.setMaximumDrawHeight(100000); panel.setMaximumDrawWidth(100000); panel.setHorizontalZoom(true); 131 panel.setVerticalZoom(true); 132 panel.setFillZoomRectangle(true); 133 134 } 135 136 147 152 private JFreeChart createContourPlot() { 153 154 String title = "Contour Plot"; 155 String xAxisLabel = "X Values"; 156 String yAxisLabel = "Y Values"; 157 String zAxisLabel = "Color Values"; 158 159 if (xIsDate) { 160 xAxis = new DateAxis(xAxisLabel); 161 xIsLog = false; } 163 else { 164 if (xIsLog) { 165 xAxis = new LogarithmicAxis(xAxisLabel); 166 } 167 else { 168 xAxis = new NumberAxis(xAxisLabel); 169 } 170 } 171 172 if (yIsLog) { 173 yAxis = new LogarithmicAxis(yAxisLabel); 174 } 175 else { 176 yAxis = new NumberAxis(yAxisLabel); 177 } 178 179 if (zIsLog) { 180 zColorBar = new ColorBar(zAxisLabel); 181 } 182 else { 183 zColorBar = new ColorBar(zAxisLabel); 184 } 185 186 if (xAxis instanceof NumberAxis) { 187 ((NumberAxis) xAxis).setAutoRangeIncludesZero(false); 188 ((NumberAxis) xAxis).setInverted(xIsInverted); 189 } 190 191 yAxis.setAutoRangeIncludesZero(false); 192 193 yAxis.setInverted(yIsInverted); 194 195 if (!xIsDate) { 196 ((NumberAxis) xAxis).setLowerMargin(0.0); 197 ((NumberAxis) xAxis).setUpperMargin(0.0); 198 } 199 200 yAxis.setLowerMargin(0.0); 201 yAxis.setUpperMargin(0.0); 202 203 zColorBar.getAxis().setInverted(zIsInverted); 204 zColorBar.getAxis().setTickMarksVisible(true); 205 206 ContourDataset data = createDataset(); 207 208 ContourPlot plot = new ContourPlot(data, xAxis, yAxis, zColorBar); 209 210 if (xIsDate) { 211 ratio = Math.abs(ratio); } 213 plot.setDataAreaRatio(ratio); 214 215 JFreeChart chart = new JFreeChart(title, null, plot, false); 216 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.green)); 217 218 return chart; 219 220 } 221 222 227 private ContourDataset createDataset() { 228 229 int numValues = numX * numY; 230 Date [] tmpDateX = new Date [numValues]; 231 double[] tmpDoubleX = new double[numValues]; 232 double[] tmpDoubleY = new double[numValues]; 233 234 Double [] oDoubleX = new Double [numValues]; 235 Double [] oDoubleY = new Double [numValues]; 236 Double [] oDoubleZ = new Double [numValues]; 237 238 int j = 0; 239 int z = 0; 240 int i = 0; 241 int last = 0; 242 double zmult = 1.0; 243 for (int k = 0; k < numValues; k++) { 244 i = k / numX; 245 if (last != i) { 246 last = i; 247 z = 0; 248 zmult = 1.005 * zmult; 249 } 250 tmpDateX[k] = new Date ((long) ((i + 100) * 1.e8)); 251 tmpDoubleX[k] = i + 2; 252 tmpDoubleY[k] = zmult * (z++); 253 oDoubleX[k] = new Double (tmpDoubleX[k]); 254 oDoubleY[k] = new Double (tmpDoubleY[k]); 255 double rad = Math.random(); 256 if (makeHoles && (rad > 0.4 && rad < 0.6)) { 257 oDoubleZ[k] = null; 258 } 259 else { 260 oDoubleZ[k] = new Double (3.0 * ((tmpDoubleX[k] + 1) * (tmpDoubleY[k] + 1) + 100)); 262 } 263 j++; 264 } 265 ContourDataset data = null; 266 267 if (xIsDate) { 268 data = new DefaultContourDataset("Contouring", tmpDateX, oDoubleY, oDoubleZ); 269 } 270 else { 271 data = new DefaultContourDataset("Contouring", oDoubleX, oDoubleY, oDoubleZ); 272 } 273 return data; 274 275 } 276 277 284 protected static boolean processArgs(String [] args) { 285 286 String [] options = {"-?", 287 "-invert", 288 "-log", 289 "-date", 290 "-vertical", 291 "-holes", 292 "-ratio:", 293 "-numX:", 294 "-numY:"}; 295 296 for (int i = 0; i < args.length; i++) { 297 boolean foundOption = false; 298 for (int j = 0; j < options.length; j++) { 299 if (args[i].startsWith(options[j])) { 300 foundOption = true; 301 int index = 0; 302 String tmpStr = null; 303 switch (j) { 304 case 0: usage(); 306 return false; 307 case 1: 308 xIsInverted = true; 309 yIsInverted = true; 310 zIsInverted = true; 311 break; 312 case 2: 313 xIsLog = true; 314 yIsLog = true; 315 zIsLog = true; 316 break; 317 case 3: 318 xIsDate = true; 319 break; 320 case 4: 321 break; 323 case 5: 324 makeHoles = true; 325 break; 326 case 6: 327 index = args[i].indexOf(':'); 328 tmpStr = args[i].substring(index + 1); 329 ratio = Double.parseDouble(tmpStr); 330 break; 331 case 7: 332 index = args[i].indexOf(':'); 333 tmpStr = args[i].substring(index + 1); 334 numX = Integer.parseInt(tmpStr); 335 break; 336 case 8: 337 index = args[i].indexOf(':'); 338 tmpStr = args[i].substring(index + 1); 339 numY = Integer.parseInt(tmpStr); 340 break; 341 default: 342 System.out.println("Only 9 options available, update options array"); 343 } 344 } 345 } 346 if (!foundOption) { 347 System.out.println("Unknown option: " + args[i]); 348 usage(); 349 return false; 350 } 351 } 352 353 return true; } 355 356 359 public static void usage() { 360 System.out.println("Usage:"); 361 System.out.println("ContourPlotDemo -? -invert -log -date -vertical -holes -ratio:value " 362 + "-numX:value -numY:value"); 363 System.out.println("Where:"); 364 System.out.println("-? displays usage and exits"); 365 System.out.println("-invert cause axes to be inverted"); 366 System.out.println("-log all axes will be logcale"); 367 System.out.println("-date the X axis will be a date"); 368 System.out.println("-vertical the colorbar will be drawn vertically"); 369 System.out.println("-holes demos plotting data with missing values"); 370 System.out.println("-ratio forces plot to maintain aspect ratio (Y/X) indicated by value"); 371 System.out.println(" positive values are in pixels, while negative is in plot units"); 372 System.out.println("-numX number of values to generate along the X axis"); 373 System.out.println("-numY number of values to generate along the X axis"); 374 375 } 376 381 public static void main(String [] args) { 382 383 if (!processArgs(args)) { 384 System.exit(1); 385 } 386 ContourPlotDemo demo = new ContourPlotDemo("ContourPlot Demo"); 387 demo.setContentPane(demo.panel); 388 demo.pack(); 389 RefineryUtilities.centerFrameOnScreen(demo); 390 demo.setVisible(true); 391 392 } 393 394 } 395 | Popular Tags |