1 72 73 package org.jfree.chart; 74 75 import java.awt.Graphics2D ; 76 import java.awt.geom.AffineTransform ; 77 import java.awt.geom.Rectangle2D ; 78 import java.awt.image.BufferedImage ; 79 import java.io.BufferedOutputStream ; 80 import java.io.File ; 81 import java.io.FileOutputStream ; 82 import java.io.IOException ; 83 import java.io.OutputStream ; 84 import java.io.PrintWriter ; 85 86 import org.jfree.chart.imagemap.ImageMapUtilities; 87 import org.jfree.chart.imagemap.OverLIBToolTipTagFragmentGenerator; 88 import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator; 89 import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator; 90 import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator; 91 import org.jfree.chart.imagemap.URLTagFragmentGenerator; 92 93 import org.jfree.chart.encoders.EncoderUtil; 94 import org.jfree.chart.encoders.ImageFormat; 95 96 101 public abstract class ChartUtilities { 102 103 113 public static void writeChartAsPNG(OutputStream out, 114 JFreeChart chart, 115 int width, 116 int height) throws IOException { 117 118 writeChartAsPNG(out, chart, width, height, null); 120 121 } 122 123 135 public static void writeChartAsPNG(OutputStream out, 136 JFreeChart chart, 137 int width, 138 int height, 139 boolean encodeAlpha, 140 int compression) throws IOException { 141 142 ChartUtilities.writeChartAsPNG( 144 out, chart, width, height, null, encodeAlpha, compression 145 ); 146 147 } 148 149 163 public static void writeChartAsPNG(OutputStream out, 164 JFreeChart chart, 165 int width, 166 int height, 167 ChartRenderingInfo info) 168 throws IOException { 169 170 if (chart == null) { 171 throw new IllegalArgumentException ("Null 'chart' argument."); 172 } 173 BufferedImage bufferedImage 174 = chart.createBufferedImage(width, height, info); 175 EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG, out); 176 } 177 178 195 public static void writeChartAsPNG(OutputStream out, 196 JFreeChart chart, 197 int width, int height, 198 ChartRenderingInfo info, 199 boolean encodeAlpha, 200 int compression) throws IOException { 201 202 if (out == null) { 203 throw new IllegalArgumentException ("Null 'out' argument."); 204 } 205 if (chart == null) { 206 throw new IllegalArgumentException ("Null 'chart' argument."); 207 } 208 BufferedImage chartImage = chart.createBufferedImage( 209 width, height, BufferedImage.TYPE_INT_ARGB, info 210 ); 211 ChartUtilities.writeBufferedImageAsPNG( 212 out, chartImage, encodeAlpha, compression 213 ); 214 215 } 216 217 229 public static void writeScaledChartAsPNG(OutputStream out, 230 JFreeChart chart, 231 int width, 232 int height, 233 int widthScaleFactor, 234 int heightScaleFactor) 235 throws IOException { 236 237 if (out == null) { 238 throw new IllegalArgumentException ("Null 'out' argument."); 239 } 240 if (chart == null) { 241 throw new IllegalArgumentException ("Null 'chart' argument."); 242 } 243 244 double desiredWidth = width * widthScaleFactor; 245 double desiredHeight = height * heightScaleFactor; 246 double defaultWidth = width; 247 double defaultHeight = height; 248 boolean scale = false; 249 250 if ((widthScaleFactor != 1) || (heightScaleFactor != 1)) { 252 scale = true; 253 } 254 255 double scaleX = desiredWidth / defaultWidth; 256 double scaleY = desiredHeight / defaultHeight; 257 258 BufferedImage image = new BufferedImage ( 259 (int) desiredWidth, (int) desiredHeight, BufferedImage.TYPE_INT_ARGB 260 ); 261 Graphics2D g2 = image.createGraphics(); 262 263 if (scale) { 264 AffineTransform saved = g2.getTransform(); 265 g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY)); 266 chart.draw( 267 g2, new Rectangle2D.Double (0, 0, defaultWidth, defaultHeight), 268 null, null 269 ); 270 g2.setTransform(saved); 271 g2.dispose(); 272 } 273 else { 274 chart.draw( 275 g2, new Rectangle2D.Double (0, 0, defaultWidth, defaultHeight), 276 null, null 277 ); 278 } 279 out.write(encodeAsPNG(image)); 280 281 } 282 283 293 public static void saveChartAsPNG(File file, 294 JFreeChart chart, 295 int width, 296 int height) throws IOException { 297 298 saveChartAsPNG(file, chart, width, height, null); 300 301 } 302 303 317 public static void saveChartAsPNG(File file, 318 JFreeChart chart, 319 int width, 320 int height, 321 ChartRenderingInfo info) 322 throws IOException { 323 324 if (file == null) { 325 throw new IllegalArgumentException ("Null 'file' argument."); 326 } 327 OutputStream out = new BufferedOutputStream (new FileOutputStream (file)); 328 ChartUtilities.writeChartAsPNG(out, chart, width, height, info); 329 out.close(); 330 } 331 332 348 public static void saveChartAsPNG(File file, 349 JFreeChart chart, 350 int width, 351 int height, 352 ChartRenderingInfo info, 353 boolean encodeAlpha, 354 int compression) throws IOException { 355 356 if (file == null) { 357 throw new IllegalArgumentException ("Null 'file' argument."); 358 } 359 if (chart == null) { 360 throw new IllegalArgumentException ("Null 'chart' argument."); 361 } 362 363 OutputStream out = new BufferedOutputStream (new FileOutputStream (file)); 364 writeChartAsPNG( 365 out, chart, width, height, info, encodeAlpha, compression 366 ); 367 out.close(); 368 369 } 370 371 382 public static void writeChartAsJPEG(OutputStream out, 383 JFreeChart chart, 384 int width, 385 int height) throws IOException { 386 387 writeChartAsJPEG(out, chart, width, height, null); 389 390 } 391 392 404 public static void writeChartAsJPEG(OutputStream out, 405 float quality, 406 JFreeChart chart, 407 int width, 408 int height) throws IOException { 409 410 ChartUtilities.writeChartAsJPEG( 412 out, quality, chart, width, height, null 413 ); 414 415 } 416 417 431 public static void writeChartAsJPEG(OutputStream out, 432 JFreeChart chart, 433 int width, 434 int height, 435 ChartRenderingInfo info) 436 throws IOException { 437 438 if (chart == null) { 439 throw new IllegalArgumentException ("Null 'chart' argument."); 440 } 441 BufferedImage image = chart.createBufferedImage(width, height, info); 442 EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out); 443 444 } 445 446 461 public static void writeChartAsJPEG(OutputStream out, 462 float quality, 463 JFreeChart chart, 464 int width, 465 int height, 466 ChartRenderingInfo info) 467 throws IOException { 468 469 if (chart == null) { 470 throw new IllegalArgumentException ("Null 'chart' argument."); 471 } 472 BufferedImage image = chart.createBufferedImage(width, height, info); 473 EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality); 474 475 } 476 477 487 public static void saveChartAsJPEG(File file, 488 JFreeChart chart, 489 int width, 490 int height) throws IOException { 491 492 saveChartAsJPEG(file, chart, width, height, null); 494 495 } 496 497 508 public static void saveChartAsJPEG(File file, 509 float quality, 510 JFreeChart chart, 511 int width, 512 int height) throws IOException { 513 514 saveChartAsJPEG(file, quality, chart, width, height, null); 516 517 } 518 519 533 public static void saveChartAsJPEG(File file, 534 JFreeChart chart, 535 int width, 536 int height, 537 ChartRenderingInfo info) 538 throws IOException { 539 540 if (file == null) { 541 throw new IllegalArgumentException ("Null 'file' argument."); 542 } 543 if (chart == null) { 544 throw new IllegalArgumentException ("Null 'chart' argument."); 545 } 546 OutputStream out = new BufferedOutputStream (new FileOutputStream (file)); 547 writeChartAsJPEG(out, chart, width, height, info); 548 out.close(); 549 550 } 551 552 567 public static void saveChartAsJPEG(File file, 568 float quality, 569 JFreeChart chart, 570 int width, 571 int height, 572 ChartRenderingInfo info) 573 throws IOException { 574 575 if (file == null) { 576 throw new IllegalArgumentException ("Null 'file' argument."); 577 } 578 if (chart == null) { 579 throw new IllegalArgumentException ("Null 'chart' argument."); 580 } 581 OutputStream out = new BufferedOutputStream (new FileOutputStream (file)); 582 writeChartAsJPEG(out, quality, chart, width, height, info); 583 out.close(); 584 585 } 586 587 595 public static void writeBufferedImageAsJPEG(OutputStream out, 596 BufferedImage image) 597 throws IOException { 598 599 writeBufferedImageAsJPEG(out, 0.75f, image); 601 602 } 603 604 613 public static void writeBufferedImageAsJPEG(OutputStream out, float quality, 614 BufferedImage image) 615 throws IOException { 616 617 EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality); 618 619 } 620 621 629 public static void writeBufferedImageAsPNG(OutputStream out, 630 BufferedImage image) 631 throws IOException { 632 633 EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out); 634 635 } 636 637 647 public static void writeBufferedImageAsPNG(OutputStream out, 648 BufferedImage image, 649 boolean encodeAlpha, 650 int compression) 651 throws IOException { 652 653 EncoderUtil.writeBufferedImage( 654 image, ImageFormat.PNG, out, compression, encodeAlpha 655 ); 656 } 657 658 667 public static byte[] encodeAsPNG(BufferedImage image) throws IOException { 668 return EncoderUtil.encode(image, ImageFormat.PNG); 669 } 670 671 682 public static byte[] encodeAsPNG(BufferedImage image, boolean encodeAlpha, 683 int compression) 684 throws IOException { 685 return EncoderUtil.encode( 686 image, ImageFormat.PNG, compression, encodeAlpha 687 ); 688 } 689 690 701 public static void writeImageMap(PrintWriter writer, 702 String name, 703 ChartRenderingInfo info, 704 boolean useOverLibForToolTips) 705 throws IOException { 706 707 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null; 708 if (useOverLibForToolTips) { 709 toolTipTagFragmentGenerator 710 = new OverLIBToolTipTagFragmentGenerator(); 711 } 712 else { 713 toolTipTagFragmentGenerator 714 = new StandardToolTipTagFragmentGenerator(); 715 } 716 ImageMapUtilities.writeImageMap( 717 writer, name, info, toolTipTagFragmentGenerator, 718 new StandardURLTagFragmentGenerator() 719 ); 720 721 } 722 723 734 public static void writeImageMap(PrintWriter writer, String name, 735 ChartRenderingInfo info, 736 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 737 URLTagFragmentGenerator urlTagFragmentGenerator) 738 throws IOException { 739 740 writer.println( 741 ImageMapUtilities.getImageMap( 742 name, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator 743 ) 744 ); 745 } 746 747 755 public static String getImageMap(String name, ChartRenderingInfo info) { 756 return ImageMapUtilities.getImageMap( 757 name, 758 info, 759 new StandardToolTipTagFragmentGenerator(), 760 new StandardURLTagFragmentGenerator() 761 ); 762 } 763 764 774 public static String getImageMap(String name, 775 ChartRenderingInfo info, 776 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 777 URLTagFragmentGenerator urlTagFragmentGenerator) { 778 779 return ImageMapUtilities.getImageMap( 780 name, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator 781 ); 782 783 } 784 785 } 786 | Popular Tags |