1 23 24 package de.progra.charting.test; 25 26 import de.progra.charting.*; 27 import de.progra.charting.model.*; 28 import de.progra.charting.render.*; 29 import java.awt.*; 30 import java.awt.geom.*; 31 import java.io.*; 32 import java.text.*; 33 import java.awt.geom.AffineTransform ; 34 35 40 public class TestChart { 41 42 43 public TestChart() { 44 } 45 46 47 50 public static void main(String [] args) { 51 makeSimpleLineTest(); 52 makeRadarTest(); 53 makeDiffSizedBarChartTest(); 54 makeBarChartFormatTest(); 55 makeAxisTitleTest(); 56 57 makeNevilleTest(); 58 59 makeSimplePieTest(); 60 makeLineChartTest(); 61 makePlotTest(); 62 makePlot2Test(); 63 makeBarTest(); 64 makeStackedBarTest(); 65 66 makeManualBarTest(); 67 makeSimpleNegativeLineTest(); 68 69 System.exit(0); 70 } 71 72 public static void makeAxisTitleTest() { 73 System.out.println("** Creating Bar Chart Plot with Axis Titles."); 74 int[][] model = {{23, 43, 12}, 75 {54, -15, 34}, 76 {99, 32, 45}}; 77 78 String [] columns = {"1997", "1998", "1999"}; 79 80 String [] rows = {"foobar.com", "@foo Ltd.", "bar.online"}; 81 82 String title = "Average Growth 1997 - 1999"; 83 84 int width = 640; 85 int height = 480; 86 87 ObjectChartDataModel data = new ObjectChartDataModel(model, columns, rows); 88 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y, "Year", "Growth"); 89 c.addChartRenderer(new BarChartRenderer(c.getCoordSystem(), 90 data), 1); 91 c.setBounds(new Rectangle(0, 0, width, height)); 92 try { 93 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/axistest.png"), c); 94 } catch(EncodingException e) { 95 System.out.println("** Error creating the axistest.png file, showing the bar chart."); 96 e.getCause().printStackTrace(); 97 return; 98 } catch(Exception e) { 99 System.out.println("** Error creating the axistest.png file, showing the bar chart."); 100 e.printStackTrace(); 101 return; 102 } 103 System.out.println("successfull."); 104 } 105 106 public static void makeRadarTest() { 107 System.out.println("** Creating Radar Chart Plot."); 108 int[][] model = {{23, 43, 12, 19, 12}, 109 {54, 20, 34, 40, 67}, 110 {15, 30, 8, 17, 23}, 111 {85, 15, 10, 26, 54}}; 112 113 String [] columns = {"1997", "1998", "1999", "2000", "2001"}; 114 115 String [] rows = {"foobar.com", "@foo Ltd.", "bar.online", "foo.co.uk"}; 116 117 String title = "Average Growth 1997 - 2001"; 118 119 int width = 640; 120 int height = 480; 121 122 ObjectChartDataModel data = new ObjectChartDataModel(model, columns, rows); 123 DefaultChart c = new DefaultChart(data, title); 124 c.addChartRenderer(new RadarChartRenderer(data), 1); 125 c.setBounds(new Rectangle(0, 0, width, height)); 126 127 try { 128 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/radartest.png"), c); 129 } catch(EncodingException e) { 130 System.out.println("** Error creating the radartest.png file, showing the radar chart."); 131 e.getCause().printStackTrace(); 132 return; 133 } catch(Exception e) { 134 System.out.println("** Error creating the radartest.png file, showing the radar chart."); 135 e.printStackTrace(); 136 return; 137 } 138 System.out.println("successfull."); 139 } 140 141 public static void makeBarChartFormatTest() { 142 System.out.println("** Creating Bar Chart Plot."); 143 int[][] model = {{23, 43, 12}, 144 {54, -15, 34}, 145 {99, 32, 45}}; 146 147 String [] columns = {"1997", "1998", "1999"}; 148 149 String [] rows = {"foobar.com", "@foo Ltd.", "bar.online"}; 150 151 String title = "Average Growth 1997 - 1999"; 152 153 int width = 640; 154 int height = 480; 155 156 ObjectChartDataModel data = new ObjectChartDataModel(model, columns, rows); 157 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y, "Year", "Growth"); 158 161 c.setCoordSystem(new CoordSystem(data, new DecimalFormat(), false, true, false)); 162 c.addChartRenderer(new BarChartRenderer(c.getCoordSystem(), 163 data, new DecimalFormat(), new Font("sans", Font.ITALIC, 9), 1.0f), 1); 164 c.setBounds(new Rectangle(0, 0, width, height)); 165 try { 166 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/barchart2test.png"), c); 167 } catch(EncodingException e) { 168 System.out.println("** Error creating the barchart2test.png file, showing the bar chart."); 169 e.getCause().printStackTrace(); 170 return; 171 } catch(Exception e) { 172 System.out.println("** Error creating the barchart2test.png file, showing the bar chart."); 173 e.printStackTrace(); 174 return; 175 } 176 System.out.println("successfull."); 177 } 178 179 public static void makeSimpleLineTest() { 180 System.out.println("** Creating Simple Line Test."); 181 int[][] model = {{-200000, 0, 100, 200000}}; 182 183 double[] columns = {-100, 0.0, 1.0, 2000.0}; 184 185 String [] rows = {"f(x) = x^3"}; 186 187 String title = "Neville Interpolation"; 188 189 int width = 640; 190 int height = 480; 191 192 DefaultChartDataModel data = new DefaultChartDataModel(model, columns, rows); 193 200 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 201 c.addChartRenderer(new PlotChartRenderer(c.getCoordSystem(), data), 1); 202 203 c.setBounds(new Rectangle(0, 0, width, height)); 204 205 try { 206 ChartEncoder.createEncodedImage(new FileOutputStream(System.getProperty("user.home")+"/linetest.png"), c, "png"); 207 } catch(EncodingException e) { 208 System.out.println("** Error creating the linetest.png file, showing the simple line chart."); 209 e.getCause().printStackTrace(); 210 return; 211 } catch(Exception e) { 212 System.out.println("** Error creating the linetest.png file, showing the simple line chart."); 213 e.printStackTrace(); 214 return; 215 } 216 System.out.println("successfull."); 217 } 218 219 public static void makeNevilleTest() { 220 System.out.println("** Creating Neville Interpolation Test."); 221 int[][] model = {{-8, 0, 1, 8}}; 222 223 double[] columns = {-2.0, 0.0, 1.0, 2.0}; 224 225 String [] rows = {"f(x) = x^3"}; 226 227 String title = "Neville Interpolation"; 228 229 int width = 640; 230 int height = 480; 231 232 DefaultChartDataModel data = new DefaultChartDataModel(model, columns, rows); 233 data.setAutoScale(true); 234 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 235 c.addChartRenderer(new LineChartRenderer(c.getCoordSystem(), data), 1); 236 237 c.addChartRenderer(new PlotChartRenderer(c.getCoordSystem(), data), 2); 238 239 c.addChartRenderer(new InterpolationChartRenderer(c.getCoordSystem(), data), 3); 240 c.setBounds(new Rectangle(0, 0, width, height)); 241 242 try { 243 ChartEncoder.createEncodedImage(new FileOutputStream(System.getProperty("user.home")+"/nevilletest.png"), c, "png"); 244 } catch(EncodingException e) { 245 System.out.println("** Error creating the nevilletest.png file, showing the Neville Interpolation."); 246 e.getCause().printStackTrace(); 247 return; 248 } catch(Exception e) { 249 System.out.println("** Error creating the nevilletest.png file, showing the Neville Interpolation."); 250 e.printStackTrace(); 251 return; 252 } 253 System.out.println("successfull."); 254 } 255 256 public static void makeSimplePieTest() { 257 System.out.println("** Creating Pie Chart Test."); 258 int[][] model = {{23, 43, 12}, 259 {54, -15, 34}, 260 {99, 32, 45}}; 261 262 String [] columns = {"1997", "1998", "1999"}; 263 264 String [] rows = {"foobar.com", "@foo Ltd.", "bar.online"}; 265 266 String title = "Average Growth 1997 - 1999"; 267 268 int width = 640; 269 int height = 480; 270 271 ObjectChartDataModel data = new ObjectChartDataModel(model, columns, rows); 272 DefaultChart c = new DefaultChart(data, title); 273 c.addChartRenderer(new PieChartRenderer(data), 1); 274 c.setBounds(new Rectangle(0, 0, width, height)); 275 try { 276 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/simplepie.png"), c); 277 } catch(EncodingException e) { 278 System.out.println("** Error creating the simple.png file, showing the pie chart."); 279 e.getCause().printStackTrace(); 280 return; 281 } catch(Exception e) { 282 System.out.println("** Error creating the simplepie.png file, showing the pie chart."); 283 e.printStackTrace(); 284 } 285 System.out.println("successfull."); 286 } 287 288 public static void makeLineChartTest() { 289 System.out.println("** Creating Line Chart Test."); 290 291 int[] quadr = {0, 1, 4, 9, 16, 25, 36}; 292 int[] exp = {1, 2, 4, 8, 16, 32, 64}; 293 double[] columns = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; 294 295 DefaultDataSet[] ds = new DefaultDataSet[3]; 296 ds[0] = new DefaultDataSet(ChartUtilities.transformArray(new int[] {0, 6}), 297 ChartUtilities.transformArray(new double[] {0.0, 6.0}), 298 CoordSystem.FIRST_YAXIS, 299 "Linear Growth"); 300 ds[1] = new DefaultDataSet(ChartUtilities.transformArray(quadr), 301 ChartUtilities.transformArray(columns), 302 CoordSystem.FIRST_YAXIS, 303 "Quadratic Growth"); 304 305 ds[2] = new DefaultDataSet(ChartUtilities.transformArray(exp), 306 ChartUtilities.transformArray(columns), 307 CoordSystem.FIRST_YAXIS, 308 "Exponential Growth"); 309 310 String title = "Growth Factor Comparison"; 311 312 int width = 640; 313 int height = 480; 314 315 DefaultChartDataModel data = new DefaultChartDataModel(ds); 316 317 data.setAutoScale(true); 319 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 320 c.addChartRenderer(new LineChartRenderer(c.getCoordSystem(), 321 data), 1); 322 323 c.setBounds(new Rectangle(0, 0, width, height)); 324 325 326 try { 327 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/simpleline.png"), c); 328 } catch(EncodingException e) { 329 System.out.println("** Error creating the simpleline.png file, showing the line chart."); 330 e.getCause().printStackTrace(); 331 return; 332 } catch(Exception e) { 333 System.out.println("** Error creating the simpleline.png file, showing the line chart."); 334 e.printStackTrace(); 335 return; 336 } 337 System.out.println("successfull."); 338 } 339 340 public static void makeDiffSizedBarChartTest() { 341 System.out.println("** Creating Bar Chart Test with different sized DataSets."); 342 343 int[] quadr = {0, 1, 4, 9, 16, 25, 36}; 344 int[] exp = {1, 2, 4, 8, 16, 32, 64}; 345 String [] columns = {"0", "1", "2", "3", "4", "5", "6"}; 346 347 DefaultDataSet[] ds = new DefaultDataSet[3]; 348 ds[0] = new DefaultDataSet(ChartUtilities.transformArray(new int[] {0, 6}), 349 new String [] {"0", "6"}, 350 CoordSystem.FIRST_YAXIS, 351 "Linear Growth"); 352 ds[1] = new DefaultDataSet(ChartUtilities.transformArray(quadr), 353 columns, 354 CoordSystem.FIRST_YAXIS, 355 "Quadratic Growth"); 356 357 ds[2] = new DefaultDataSet(ChartUtilities.transformArray(exp), 358 columns, 359 CoordSystem.FIRST_YAXIS, 360 "Exponential Growth"); 361 362 String title = "Growth Factor Comparison"; 363 364 int width = 640; 365 int height = 480; 366 367 ObjectChartDataModel data = new ObjectChartDataModel(ds, columns); 368 369 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y, "x", "y"); 372 c.addChartRenderer(new LineChartRenderer(c.getCoordSystem(), 373 data), 1); 374 375 c.setBounds(new Rectangle(0, 0, width, height)); 376 377 378 try { 379 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/objectbarchart.png"), c); 380 } catch(EncodingException e) { 381 System.out.println("** Error creating the objectbarchart.png file, showing the line chart."); 382 e.getCause().printStackTrace(); 383 return; 384 } catch(Exception e) { 385 System.out.println("** Error creating the objectbarchart.png file, showing the line chart."); 386 e.printStackTrace(); 387 return; 388 } 389 System.out.println("successfull."); 390 } 391 392 public static void makePlotTest() { 393 System.out.println("** Creating Function Plot."); 394 String title = "Plotting x^3"; 395 396 int width = 640; 397 int height = 480; 398 399 DefaultChartDataModel data = FunctionPlotter.createChartDataModelInstance(-10, 10, 2000, "1/x"); 400 data.setAutoScale(true); 401 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 402 c.addChartRenderer(new LineChartRenderer(c.getCoordSystem(), 403 data), 1); 404 405 c.setBounds(new Rectangle(0, 0, width, height)); 406 try { 407 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/plottest.png"), c); 408 } catch(EncodingException e) { 409 System.out.println("** Error creating the plottest.png file, showing the function plot."); 410 e.getCause().printStackTrace(); 411 return; 412 } catch(Exception e) { 413 System.out.println("** Error creating the plottest.png file, showing the function plot."); 414 e.printStackTrace(); 415 return; 416 } 417 System.out.println("successfull."); 418 } 419 420 public static void makePlot2Test() { 421 System.out.println("** Creating Plot Chart Test."); 422 423 double[] quadr = {0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0}; 424 double[] exp = {1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0}; 425 double[] log = {0.0, Math.log(1.0), Math.log(2.0), Math.log(3.0), Math.log(4.0), Math.log(5.0), Math.log(6.0)}; 426 double[] sqrt = {0.0, Math.sqrt(1.0), Math.sqrt(2.0), Math.sqrt(3.0), Math.sqrt(4.0), Math.sqrt(5.0), Math.sqrt(6.0)}; 427 double[] columns = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; 428 429 DefaultDataSet[] ds = new DefaultDataSet[5]; 430 ds[0] = new DefaultDataSet(ChartUtilities.transformArray(new double[] {0.0, 6.0}), 431 ChartUtilities.transformArray(new double[] {0.0, 6.0}), 432 CoordSystem.FIRST_YAXIS, 433 "Linear Growth"); 434 ds[1] = new DefaultDataSet(ChartUtilities.transformArray(quadr), 435 ChartUtilities.transformArray(columns), 436 CoordSystem.FIRST_YAXIS, 437 "Quadratic Growth"); 438 439 ds[2] = new DefaultDataSet(ChartUtilities.transformArray(exp), 440 ChartUtilities.transformArray(columns), 441 CoordSystem.FIRST_YAXIS, 442 "Exponential Growth"); 443 444 445 ds[3] = new DefaultDataSet(ChartUtilities.transformArray(log), 446 ChartUtilities.transformArray(columns), 447 CoordSystem.FIRST_YAXIS, 448 "Logarithmic Growth"); 449 450 451 ds[4] = new DefaultDataSet(ChartUtilities.transformArray(sqrt), 452 ChartUtilities.transformArray(columns), 453 CoordSystem.FIRST_YAXIS, 454 "Square Root Growth"); 455 456 String title = "Growth Factor Comparison"; 457 458 int width = 640; 459 int height = 480; 460 461 DefaultChartDataModel data = new DefaultChartDataModel(ds); 462 463 data.setAutoScale(true); 465 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 466 c.addChartRenderer(new PlotChartRenderer(c.getCoordSystem(), data), 1); 467 468 c.setBounds(new Rectangle(0, 0, width, height)); 469 470 try { 471 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/secondplot.png"), c); 472 } catch(EncodingException e) { 473 System.out.println("** Error creating the secondplot.png file, showing the plot chart."); 474 e.getCause().printStackTrace(); 475 return; 476 } catch(Exception e) { 477 System.out.println("** Error creating the secondplot.png file, showing the second plot chart."); 478 e.printStackTrace(); 479 return; 480 } 481 System.out.println("successfull."); 482 } 483 484 public static void makeBarTest() { 485 System.out.println("** Creating Bar Chart Plot."); 486 int[][] model = {{23, 43, 12}, 487 {54, -15, 34}, 488 {99, 32, 45}}; 489 490 String [] columns = {"1997", "1998", "1999"}; 491 492 String [] rows = {"foobar.com", "@foo Ltd.", "bar.online"}; 493 494 String title = "Average Growth 1997 - 1999"; 495 496 int width = 640; 497 int height = 480; 498 499 ObjectChartDataModel data = new ObjectChartDataModel(model, columns, rows); 500 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 501 c.addChartRenderer(new BarChartRenderer(c.getCoordSystem(), 502 data), 1); 503 c.setBounds(new Rectangle(0, 0, width, height)); 504 try { 505 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/simplebar.png"), c); 506 } catch(EncodingException e) { 507 System.out.println("** Error creating the simplebar.png file, showing the bar chart."); 508 e.getCause().printStackTrace(); 509 return; 510 } catch(Exception e) { 511 System.out.println("** Error creating the simplebar.png file, showing the bar chart."); 512 e.printStackTrace(); 513 return; 514 } 515 System.out.println("successfull."); 516 } 517 518 public static void makeStackedBarTest() { 519 System.out.println("** Creating Stacked Bar Chart Plot."); 520 int[][] model = {{23, 43, 12, 5, 20}, 521 {54, -15, 34, 40, 45}, 522 {99, -10, 45, -10, -25}}; 523 524 String [] columns = {"1997", "1998", "1999", "2000", "2001"}; 525 526 String [] rows = {"foobar.com", "@foo Ltd.", "bar.online"}; 527 528 String title = "Profit Shares 1997 - 2001"; 529 530 int width = 640; 531 int height = 480; 532 533 ObjectChartDataModel data = new ObjectChartDataModel(model, columns, rows); 534 535 data.setChartDataModelConstraints(CoordSystem.FIRST_YAXIS, 536 new StackedChartDataModelConstraints(data, CoordSystem.FIRST_YAXIS, false)); 537 538 data.setChartDataModelConstraints(CoordSystem.SECOND_YAXIS, 539 new StackedChartDataModelConstraints(data, CoordSystem.SECOND_YAXIS, false)); 540 541 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 542 c.addChartRenderer(new StackedBarChartRenderer(c.getCoordSystem(), 543 data), 1); 544 c.setBounds(new Rectangle(0, 0, width, height)); 545 try { 546 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/stackedbar.png"), c); 547 } catch(EncodingException e) { 548 System.out.println("** Error creating the stackedbar.png file, showing the stacked bar chart."); 549 e.getCause().printStackTrace(); 550 return; 551 } catch(Exception e) { 552 System.out.println("** Error creating the stackedbar.png file, showing the stacked bar chart."); 553 e.printStackTrace(); 554 return; 555 } 556 System.out.println("successfull."); 557 } 558 559 public static void makeManualBarTest() { 560 System.out.println("** Creating Bar Chart Plot with manual scaling."); 561 int[][] model = {{23, 43, 12}, 562 {54, 10, 34}, 563 {99, 32, 45}}; 564 565 String [] columns = {"1997", "1998", "1999"}; 566 567 String [] rows = {"foobar.com", "@foo Ltd.", "bar.online"}; 568 569 String title = "Average Growth 1997 - 1999"; 570 571 int width = 640; 572 int height = 480; 573 574 ObjectChartDataModel data = new ObjectChartDataModel(model, columns, rows); 575 576 data.setManualScale(true); 577 data.setMaximumValue(new Integer (100)); 578 data.setMinimumValue(new Integer (0)); 579 580 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 581 c.addChartRenderer(new BarChartRenderer(c.getCoordSystem(), 582 data), 1); 583 c.setBounds(new Rectangle(0, 0, width, height)); 584 try { 585 ChartEncoder.createPNG(new FileOutputStream(System.getProperty("user.home")+"/manualbar.png"), c); 586 } catch(EncodingException e) { 587 System.out.println("** Error creating the manualbar.png file, showing the manually scaled bar chart."); 588 e.getCause().printStackTrace(); 589 return; 590 } catch(Exception e) { 591 System.out.println("** Error creating the manualbar.png file, showing the manually scaled bar chart."); 592 e.printStackTrace(); 593 return; 594 } 595 System.out.println("successfull."); 596 } 597 598 public static void makeSimpleNegativeLineTest() { 599 System.out.println("** Creating Simple Negative Line Test."); 600 int[][] model = {{-200, -160, -80, 0}}; 601 602 double[] columns = {-100, -80, -40, 0}; 603 604 String [] rows = {"Negative sector"}; 605 606 String title = "Just a Test"; 607 608 int width = 640; 609 int height = 480; 610 611 DefaultChartDataModel data = new DefaultChartDataModel(model, columns, rows); 612 DefaultChart c = new DefaultChart(data, title, DefaultChart.LINEAR_X_LINEAR_Y); 614 c.addChartRenderer(new LineChartRenderer(c.getCoordSystem(), data), 1); 615 616 c.setBounds(new Rectangle(0, 0, width, height)); 617 618 try { 619 ChartEncoder.createEncodedImage(new FileOutputStream(System.getProperty("user.home")+"/neglinetest.png"), c, "png"); 620 } catch(EncodingException e) { 621 System.out.println("** Error creating the neglinetest.png file, showing the simple negative line test."); 622 e.getCause().printStackTrace(); 623 return; 624 } catch(Exception e) { 625 System.out.println("** Error creating the neglinetest.png file, showing the simple negative line test."); 626 e.printStackTrace(); 627 return; 628 } 629 System.out.println("successfull."); 630 } 631 } 632 | Popular Tags |