1 /* =========================================================== 2 * JFreeChart : a free chart library for the Java(tm) platform 3 * =========================================================== 4 * 5 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 6 * 7 * Project Info: http://www.jfree.org/jfreechart/index.html 8 * 9 * This library is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 2.1 of the License, or 12 * (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 * License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 25 * Other names may be trademarks of their respective owners.] 26 * 27 * ------------------- 28 * ChartUtilities.java 29 * ------------------- 30 * (C) Copyright 2001-2013, by Object Refinery Limited and Contributors. 31 * 32 * Original Author: David Gilbert (for Object Refinery Limited); 33 * Contributor(s): Wolfgang Irler; 34 * Richard Atkinson; 35 * Xavier Poinsard; 36 * 37 * Changes 38 * ------- 39 * 11-Dec-2001 : Version 1. The JPEG method comes from Wolfgang Irler's 40 * JFreeChartServletDemo class (DG); 41 * 23-Jan-2002 : Changed saveChartAsXXX() methods to pass IOExceptions back to 42 * caller (DG); 43 * 26-Jun-2002 : Added image map methods (DG); 44 * 05-Aug-2002 : Added writeBufferedImage methods 45 * Modified writeImageMap method to support flexible image 46 * maps (RA); 47 * 26-Aug-2002 : Added saveChartAsJPEG and writeChartAsJPEG methods with info 48 * objects (RA); 49 * 05-Sep-2002 : Added writeImageMap() method to support OverLIB 50 * - http://www.bosrup.com/web/overlib (RA); 51 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG); 52 * 17-Oct-2002 : Exposed JPEG quality setting and PNG compression level as 53 * parameters (DG); 54 * 25-Oct-2002 : Fixed writeChartAsJPEG() empty method bug (DG); 55 * 13-Mar-2003 : Updated writeImageMap method as suggested by Xavier Poinsard 56 * (see Feature Request 688079) (DG); 57 * 12-Aug-2003 : Added support for custom image maps using 58 * ToolTipTagFragmentGenerator and URLTagFragmentGenerator (RA); 59 * 02-Sep-2003 : Separated PNG encoding from writing chart to an 60 * OutputStream (RA); 61 * 04-Dec-2003 : Chart draw() method modified to include anchor point (DG); 62 * 20-Feb-2004 : Edited Javadocs and added argument checking (DG); 63 * 05-Apr-2004 : Fixed problem with buffered image type (DG); 64 * 01-Aug-2004 : Modified to use EncoderUtil for all image encoding (RA); 65 * 02-Aug-2004 : Delegated image map related functionality to ImageMapUtil (RA); 66 * 13-Jan-2005 : Renamed ImageMapUtil --> ImageMapUtilities, removed method 67 * writeImageMap(PrintWriter, String, ChartRenderingInfo) which 68 * exists in ImageMapUtilities (DG); 69 * ------------- JFREECHART 1.0.x --------------------------------------------- 70 * 06-Feb-2006 : API doc update (DG); 71 * 19-Mar-2007 : Use try-finally to close output stream in saveChartAsXXX() 72 * methods (DG); 73 * 10-Jan-2008 : Fix bug 1868251 - don't create image with transparency when 74 * saving to JPEG format (DG); 75 * 02-Jul-2013 : Use ParamChecks class (DG); 76 * 77 */ 78 79 package org.jfree.chart; 80 81 import java.awt.Graphics2D; 82 import java.awt.geom.AffineTransform; 83 import java.awt.geom.Rectangle2D; 84 import java.awt.image.BufferedImage; 85 import java.io.BufferedOutputStream; 86 import java.io.File; 87 import java.io.FileOutputStream; 88 import java.io.IOException; 89 import java.io.OutputStream; 90 import java.io.PrintWriter; 91 92 import org.jfree.chart.encoders.EncoderUtil; 93 import org.jfree.chart.encoders.ImageFormat; 94 import org.jfree.chart.imagemap.ImageMapUtilities; 95 import org.jfree.chart.imagemap.OverLIBToolTipTagFragmentGenerator; 96 import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator; 97 import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator; 98 import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator; 99 import org.jfree.chart.imagemap.URLTagFragmentGenerator; 100 import org.jfree.chart.util.ParamChecks; 101 102 /** 103 * A collection of utility methods for JFreeChart. Includes methods for 104 * converting charts to image formats (PNG and JPEG) plus creating simple HTML 105 * image maps. 106 * 107 * @see ImageMapUtilities 108 */ 109 public abstract class ChartUtilities { 110 111 /** 112 * Applies the current theme to the specified chart. This method is 113 * provided for convenience, the theme itself is stored in the 114 * {@link ChartFactory} class. 115 * 116 * @param chart the chart (<code>null</code> not permitted). 117 * 118 * @since 1.0.11 119 */ applyCurrentTheme(JFreeChart chart)120 public static void applyCurrentTheme(JFreeChart chart) { 121 ChartFactory.getChartTheme().apply(chart); 122 } 123 124 /** 125 * Writes a chart to an output stream in PNG format. 126 * 127 * @param out the output stream (<code>null</code> not permitted). 128 * @param chart the chart (<code>null</code> not permitted). 129 * @param width the image width. 130 * @param height the image height. 131 * 132 * @throws IOException if there are any I/O errors. 133 */ writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height)134 public static void writeChartAsPNG(OutputStream out, JFreeChart chart, 135 int width, int height) throws IOException { 136 137 // defer argument checking... 138 writeChartAsPNG(out, chart, width, height, null); 139 140 } 141 142 /** 143 * Writes a chart to an output stream in PNG format. 144 * 145 * @param out the output stream (<code>null</code> not permitted). 146 * @param chart the chart (<code>null</code> not permitted). 147 * @param width the image width. 148 * @param height the image height. 149 * @param encodeAlpha encode alpha? 150 * @param compression the compression level (0-9). 151 * 152 * @throws IOException if there are any I/O errors. 153 */ writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, boolean encodeAlpha, int compression)154 public static void writeChartAsPNG(OutputStream out, JFreeChart chart, 155 int width, int height, boolean encodeAlpha, int compression) 156 throws IOException { 157 158 // defer argument checking... 159 ChartUtilities.writeChartAsPNG(out, chart, width, height, null, 160 encodeAlpha, compression); 161 162 } 163 164 /** 165 * Writes a chart to an output stream in PNG format. This method allows 166 * you to pass in a {@link ChartRenderingInfo} object, to collect 167 * information about the chart dimensions/entities. You will need this 168 * info if you want to create an HTML image map. 169 * 170 * @param out the output stream (<code>null</code> not permitted). 171 * @param chart the chart (<code>null</code> not permitted). 172 * @param width the image width. 173 * @param height the image height. 174 * @param info the chart rendering info (<code>null</code> permitted). 175 * 176 * @throws IOException if there are any I/O errors. 177 */ writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, ChartRenderingInfo info)178 public static void writeChartAsPNG(OutputStream out, JFreeChart chart, 179 int width, int height, ChartRenderingInfo info) 180 throws IOException { 181 182 ParamChecks.nullNotPermitted(chart, "chart"); 183 BufferedImage bufferedImage 184 = chart.createBufferedImage(width, height, info); 185 EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG, out); 186 } 187 188 /** 189 * Writes a chart to an output stream in PNG format. This method allows 190 * you to pass in a {@link ChartRenderingInfo} object, to collect 191 * information about the chart dimensions/entities. You will need this 192 * info if you want to create an HTML image map. 193 * 194 * @param out the output stream (<code>null</code> not permitted). 195 * @param chart the chart (<code>null</code> not permitted). 196 * @param width the image width. 197 * @param height the image height. 198 * @param info carries back chart rendering info (<code>null</code> 199 * permitted). 200 * @param encodeAlpha encode alpha? 201 * @param compression the PNG compression level (0-9). 202 * 203 * @throws IOException if there are any I/O errors. 204 */ writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, ChartRenderingInfo info, boolean encodeAlpha, int compression)205 public static void writeChartAsPNG(OutputStream out, JFreeChart chart, 206 int width, int height, ChartRenderingInfo info, 207 boolean encodeAlpha, int compression) throws IOException { 208 209 ParamChecks.nullNotPermitted(out, "out"); 210 ParamChecks.nullNotPermitted(chart, "chart"); 211 BufferedImage chartImage = chart.createBufferedImage(width, height, 212 BufferedImage.TYPE_INT_ARGB, info); 213 ChartUtilities.writeBufferedImageAsPNG(out, chartImage, encodeAlpha, 214 compression); 215 216 } 217 218 /** 219 * Writes a scaled version of a chart to an output stream in PNG format. 220 * 221 * @param out the output stream (<code>null</code> not permitted). 222 * @param chart the chart (<code>null</code> not permitted). 223 * @param width the unscaled chart width. 224 * @param height the unscaled chart height. 225 * @param widthScaleFactor the horizontal scale factor. 226 * @param heightScaleFactor the vertical scale factor. 227 * 228 * @throws IOException if there are any I/O problems. 229 */ writeScaledChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, int widthScaleFactor, int heightScaleFactor)230 public static void writeScaledChartAsPNG(OutputStream out, 231 JFreeChart chart, int width, int height, int widthScaleFactor, 232 int heightScaleFactor) throws IOException { 233 234 ParamChecks.nullNotPermitted(out, "out"); 235 ParamChecks.nullNotPermitted(chart, "chart"); 236 237 double desiredWidth = width * widthScaleFactor; 238 double desiredHeight = height * heightScaleFactor; 239 double defaultWidth = width; 240 double defaultHeight = height; 241 boolean scale = false; 242 243 // get desired width and height from somewhere then... 244 if ((widthScaleFactor != 1) || (heightScaleFactor != 1)) { 245 scale = true; 246 } 247 248 double scaleX = desiredWidth / defaultWidth; 249 double scaleY = desiredHeight / defaultHeight; 250 251 BufferedImage image = new BufferedImage((int) desiredWidth, 252 (int) desiredHeight, BufferedImage.TYPE_INT_ARGB); 253 Graphics2D g2 = image.createGraphics(); 254 255 if (scale) { 256 AffineTransform saved = g2.getTransform(); 257 g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY)); 258 chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, 259 defaultHeight), null, null); 260 g2.setTransform(saved); 261 g2.dispose(); 262 } 263 else { 264 chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, 265 defaultHeight), null, null); 266 } 267 out.write(encodeAsPNG(image)); 268 269 } 270 271 /** 272 * Saves a chart to the specified file in PNG format. 273 * 274 * @param file the file name (<code>null</code> not permitted). 275 * @param chart the chart (<code>null</code> not permitted). 276 * @param width the image width. 277 * @param height the image height. 278 * 279 * @throws IOException if there are any I/O errors. 280 */ saveChartAsPNG(File file, JFreeChart chart, int width, int height)281 public static void saveChartAsPNG(File file, JFreeChart chart, 282 int width, int height) throws IOException { 283 284 // defer argument checking... 285 saveChartAsPNG(file, chart, width, height, null); 286 287 } 288 289 /** 290 * Saves a chart to a file in PNG format. This method allows you to pass 291 * in a {@link ChartRenderingInfo} object, to collect information about the 292 * chart dimensions/entities. You will need this info if you want to 293 * create an HTML image map. 294 * 295 * @param file the file (<code>null</code> not permitted). 296 * @param chart the chart (<code>null</code> not permitted). 297 * @param width the image width. 298 * @param height the image height. 299 * @param info the chart rendering info (<code>null</code> permitted). 300 * 301 * @throws IOException if there are any I/O errors. 302 */ saveChartAsPNG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info)303 public static void saveChartAsPNG(File file, JFreeChart chart, 304 int width, int height, ChartRenderingInfo info) 305 throws IOException { 306 307 ParamChecks.nullNotPermitted(file, "file"); 308 OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 309 try { 310 ChartUtilities.writeChartAsPNG(out, chart, width, height, info); 311 } 312 finally { 313 out.close(); 314 } 315 } 316 317 /** 318 * Saves a chart to a file in PNG format. This method allows you to pass 319 * in a {@link ChartRenderingInfo} object, to collect information about the 320 * chart dimensions/entities. You will need this info if you want to 321 * create an HTML image map. 322 * 323 * @param file the file (<code>null</code> not permitted). 324 * @param chart the chart (<code>null</code> not permitted). 325 * @param width the image width. 326 * @param height the image height. 327 * @param info the chart rendering info (<code>null</code> permitted). 328 * @param encodeAlpha encode alpha? 329 * @param compression the PNG compression level (0-9). 330 * 331 * @throws IOException if there are any I/O errors. 332 */ saveChartAsPNG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info, boolean encodeAlpha, int compression)333 public static void saveChartAsPNG(File file, JFreeChart chart, 334 int width, int height, ChartRenderingInfo info, boolean encodeAlpha, 335 int compression) throws IOException { 336 337 ParamChecks.nullNotPermitted(file, "file"); 338 ParamChecks.nullNotPermitted(chart, "chart"); 339 OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 340 try { 341 writeChartAsPNG(out, chart, width, height, info, encodeAlpha, 342 compression); 343 } 344 finally { 345 out.close(); 346 } 347 348 } 349 350 /** 351 * Writes a chart to an output stream in JPEG format. Please note that 352 * JPEG is a poor format for chart images, use PNG if possible. 353 * 354 * @param out the output stream (<code>null</code> not permitted). 355 * @param chart the chart (<code>null</code> not permitted). 356 * @param width the image width. 357 * @param height the image height. 358 * 359 * @throws IOException if there are any I/O errors. 360 */ writeChartAsJPEG(OutputStream out, JFreeChart chart, int width, int height)361 public static void writeChartAsJPEG(OutputStream out, 362 JFreeChart chart, int width, int height) throws IOException { 363 364 // defer argument checking... 365 writeChartAsJPEG(out, chart, width, height, null); 366 367 } 368 369 /** 370 * Writes a chart to an output stream in JPEG format. Please note that 371 * JPEG is a poor format for chart images, use PNG if possible. 372 * 373 * @param out the output stream (<code>null</code> not permitted). 374 * @param quality the quality setting. 375 * @param chart the chart (<code>null</code> not permitted). 376 * @param width the image width. 377 * @param height the image height. 378 * 379 * @throws IOException if there are any I/O errors. 380 */ writeChartAsJPEG(OutputStream out, float quality, JFreeChart chart, int width, int height)381 public static void writeChartAsJPEG(OutputStream out, float quality, 382 JFreeChart chart, int width, int height) throws IOException { 383 384 // defer argument checking... 385 ChartUtilities.writeChartAsJPEG(out, quality, chart, width, height, 386 null); 387 388 } 389 390 /** 391 * Writes a chart to an output stream in JPEG format. This method allows 392 * you to pass in a {@link ChartRenderingInfo} object, to collect 393 * information about the chart dimensions/entities. You will need this 394 * info if you want to create an HTML image map. 395 * 396 * @param out the output stream (<code>null</code> not permitted). 397 * @param chart the chart (<code>null</code> not permitted). 398 * @param width the image width. 399 * @param height the image height. 400 * @param info the chart rendering info (<code>null</code> permitted). 401 * 402 * @throws IOException if there are any I/O errors. 403 */ writeChartAsJPEG(OutputStream out, JFreeChart chart, int width, int height, ChartRenderingInfo info)404 public static void writeChartAsJPEG(OutputStream out, JFreeChart chart, 405 int width, int height, ChartRenderingInfo info) 406 throws IOException { 407 408 ParamChecks.nullNotPermitted(out, "out"); 409 ParamChecks.nullNotPermitted(chart, "chart"); 410 BufferedImage image = chart.createBufferedImage(width, height, 411 BufferedImage.TYPE_INT_RGB, info); 412 EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out); 413 414 } 415 416 /** 417 * Writes a chart to an output stream in JPEG format. This method allows 418 * you to pass in a {@link ChartRenderingInfo} object, to collect 419 * information about the chart dimensions/entities. You will need this 420 * info if you want to create an HTML image map. 421 * 422 * @param out the output stream (<code>null</code> not permitted). 423 * @param quality the output quality (0.0f to 1.0f). 424 * @param chart the chart (<code>null</code> not permitted). 425 * @param width the image width. 426 * @param height the image height. 427 * @param info the chart rendering info (<code>null</code> permitted). 428 * 429 * @throws IOException if there are any I/O errors. 430 */ writeChartAsJPEG(OutputStream out, float quality, JFreeChart chart, int width, int height, ChartRenderingInfo info)431 public static void writeChartAsJPEG(OutputStream out, float quality, 432 JFreeChart chart, int width, int height, ChartRenderingInfo info) 433 throws IOException { 434 435 ParamChecks.nullNotPermitted(out, "out"); 436 ParamChecks.nullNotPermitted(chart, "chart"); 437 BufferedImage image = chart.createBufferedImage(width, height, 438 BufferedImage.TYPE_INT_RGB, info); 439 EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality); 440 441 } 442 443 /** 444 * Saves a chart to a file in JPEG format. 445 * 446 * @param file the file (<code>null</code> not permitted). 447 * @param chart the chart (<code>null</code> not permitted). 448 * @param width the image width. 449 * @param height the image height. 450 * 451 * @throws IOException if there are any I/O errors. 452 */ saveChartAsJPEG(File file, JFreeChart chart, int width, int height)453 public static void saveChartAsJPEG(File file, JFreeChart chart, 454 int width, int height) throws IOException { 455 456 // defer argument checking... 457 saveChartAsJPEG(file, chart, width, height, null); 458 459 } 460 461 /** 462 * Saves a chart to a file in JPEG format. 463 * 464 * @param file the file (<code>null</code> not permitted). 465 * @param quality the JPEG quality setting. 466 * @param chart the chart (<code>null</code> not permitted). 467 * @param width the image width. 468 * @param height the image height. 469 * 470 * @throws IOException if there are any I/O errors. 471 */ saveChartAsJPEG(File file, float quality, JFreeChart chart, int width, int height)472 public static void saveChartAsJPEG(File file, float quality, 473 JFreeChart chart, int width, int height) throws IOException { 474 475 // defer argument checking... 476 saveChartAsJPEG(file, quality, chart, width, height, null); 477 478 } 479 480 /** 481 * Saves a chart to a file in JPEG format. This method allows you to pass 482 * in a {@link ChartRenderingInfo} object, to collect information about the 483 * chart dimensions/entities. You will need this info if you want to 484 * create an HTML image map. 485 * 486 * @param file the file name (<code>null</code> not permitted). 487 * @param chart the chart (<code>null</code> not permitted). 488 * @param width the image width. 489 * @param height the image height. 490 * @param info the chart rendering info (<code>null</code> permitted). 491 * 492 * @throws IOException if there are any I/O errors. 493 */ saveChartAsJPEG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info)494 public static void saveChartAsJPEG(File file, JFreeChart chart, 495 int width, int height, ChartRenderingInfo info) throws IOException { 496 497 ParamChecks.nullNotPermitted(file, "file"); 498 ParamChecks.nullNotPermitted(chart, "chart"); 499 OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 500 try { 501 writeChartAsJPEG(out, chart, width, height, info); 502 } 503 finally { 504 out.close(); 505 } 506 507 } 508 509 /** 510 * Saves a chart to a file in JPEG format. This method allows you to pass 511 * in a {@link ChartRenderingInfo} object, to collect information about the 512 * chart dimensions/entities. You will need this info if you want to 513 * create an HTML image map. 514 * 515 * @param file the file name (<code>null</code> not permitted). 516 * @param quality the quality setting. 517 * @param chart the chart (<code>null</code> not permitted). 518 * @param width the image width. 519 * @param height the image height. 520 * @param info the chart rendering info (<code>null</code> permitted). 521 * 522 * @throws IOException if there are any I/O errors. 523 */ saveChartAsJPEG(File file, float quality, JFreeChart chart, int width, int height, ChartRenderingInfo info)524 public static void saveChartAsJPEG(File file, float quality, 525 JFreeChart chart, int width, int height, 526 ChartRenderingInfo info) throws IOException { 527 528 ParamChecks.nullNotPermitted(file, "file"); 529 ParamChecks.nullNotPermitted(chart, "chart"); 530 OutputStream out = new BufferedOutputStream(new FileOutputStream( 531 file)); 532 try { 533 writeChartAsJPEG(out, quality, chart, width, height, info); 534 } 535 finally { 536 out.close(); 537 } 538 539 } 540 541 /** 542 * Writes a {@link BufferedImage} to an output stream in JPEG format. 543 * 544 * @param out the output stream (<code>null</code> not permitted). 545 * @param image the image (<code>null</code> not permitted). 546 * 547 * @throws IOException if there are any I/O errors. 548 */ writeBufferedImageAsJPEG(OutputStream out, BufferedImage image)549 public static void writeBufferedImageAsJPEG(OutputStream out, 550 BufferedImage image) throws IOException { 551 552 // defer argument checking... 553 writeBufferedImageAsJPEG(out, 0.75f, image); 554 555 } 556 557 /** 558 * Writes a {@link BufferedImage} to an output stream in JPEG format. 559 * 560 * @param out the output stream (<code>null</code> not permitted). 561 * @param quality the image quality (0.0f to 1.0f). 562 * @param image the image (<code>null</code> not permitted). 563 * 564 * @throws IOException if there are any I/O errors. 565 */ writeBufferedImageAsJPEG(OutputStream out, float quality, BufferedImage image)566 public static void writeBufferedImageAsJPEG(OutputStream out, float quality, 567 BufferedImage image) throws IOException { 568 569 EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality); 570 571 } 572 573 /** 574 * Writes a {@link BufferedImage} to an output stream in PNG format. 575 * 576 * @param out the output stream (<code>null</code> not permitted). 577 * @param image the image (<code>null</code> not permitted). 578 * 579 * @throws IOException if there are any I/O errors. 580 */ writeBufferedImageAsPNG(OutputStream out, BufferedImage image)581 public static void writeBufferedImageAsPNG(OutputStream out, 582 BufferedImage image) throws IOException { 583 584 EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out); 585 586 } 587 588 /** 589 * Writes a {@link BufferedImage} to an output stream in PNG format. 590 * 591 * @param out the output stream (<code>null</code> not permitted). 592 * @param image the image (<code>null</code> not permitted). 593 * @param encodeAlpha encode alpha? 594 * @param compression the compression level (0-9). 595 * 596 * @throws IOException if there are any I/O errors. 597 */ writeBufferedImageAsPNG(OutputStream out, BufferedImage image, boolean encodeAlpha, int compression)598 public static void writeBufferedImageAsPNG(OutputStream out, 599 BufferedImage image, boolean encodeAlpha, int compression) 600 throws IOException { 601 602 EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out, 603 compression, encodeAlpha); 604 } 605 606 /** 607 * Encodes a {@link BufferedImage} to PNG format. 608 * 609 * @param image the image (<code>null</code> not permitted). 610 * 611 * @return A byte array in PNG format. 612 * 613 * @throws IOException if there is an I/O problem. 614 */ encodeAsPNG(BufferedImage image)615 public static byte[] encodeAsPNG(BufferedImage image) throws IOException { 616 return EncoderUtil.encode(image, ImageFormat.PNG); 617 } 618 619 /** 620 * Encodes a {@link BufferedImage} to PNG format. 621 * 622 * @param image the image (<code>null</code> not permitted). 623 * @param encodeAlpha encode alpha? 624 * @param compression the PNG compression level (0-9). 625 * 626 * @return The byte array in PNG format. 627 * 628 * @throws IOException if there is an I/O problem. 629 */ encodeAsPNG(BufferedImage image, boolean encodeAlpha, int compression)630 public static byte[] encodeAsPNG(BufferedImage image, boolean encodeAlpha, 631 int compression) throws IOException { 632 return EncoderUtil.encode(image, ImageFormat.PNG, compression, 633 encodeAlpha); 634 } 635 636 /** 637 * Writes an image map to an output stream. 638 * 639 * @param writer the writer (<code>null</code> not permitted). 640 * @param name the map name (<code>null</code> not permitted). 641 * @param info the chart rendering info (<code>null</code> not permitted). 642 * @param useOverLibForToolTips whether to use OverLIB for tooltips 643 * (http://www.bosrup.com/web/overlib/). 644 * 645 * @throws IOException if there are any I/O errors. 646 */ writeImageMap(PrintWriter writer, String name, ChartRenderingInfo info, boolean useOverLibForToolTips)647 public static void writeImageMap(PrintWriter writer, String name, 648 ChartRenderingInfo info, boolean useOverLibForToolTips) 649 throws IOException { 650 651 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator; 652 if (useOverLibForToolTips) { 653 toolTipTagFragmentGenerator 654 = new OverLIBToolTipTagFragmentGenerator(); 655 } 656 else { 657 toolTipTagFragmentGenerator 658 = new StandardToolTipTagFragmentGenerator(); 659 } 660 ImageMapUtilities.writeImageMap(writer, name, info, 661 toolTipTagFragmentGenerator, 662 new StandardURLTagFragmentGenerator()); 663 664 } 665 666 /** 667 * Writes an image map to the specified writer. 668 * 669 * @param writer the writer (<code>null</code> not permitted). 670 * @param name the map name (<code>null</code> not permitted). 671 * @param info the chart rendering info (<code>null</code> not permitted). 672 * @param toolTipTagFragmentGenerator a generator for the HTML fragment 673 * that will contain the tooltip text (<code>null</code> not permitted 674 * if <code>info</code> contains tooltip information). 675 * @param urlTagFragmentGenerator a generator for the HTML fragment that 676 * will contain the URL reference (<code>null</code> not permitted if 677 * <code>info</code> contains URLs). 678 * 679 * @throws IOException if there are any I/O errors. 680 */ writeImageMap(PrintWriter writer, String name, ChartRenderingInfo info, ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, URLTagFragmentGenerator urlTagFragmentGenerator)681 public static void writeImageMap(PrintWriter writer, String name, 682 ChartRenderingInfo info, 683 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 684 URLTagFragmentGenerator urlTagFragmentGenerator) 685 throws IOException { 686 687 writer.println(ImageMapUtilities.getImageMap(name, info, 688 toolTipTagFragmentGenerator, urlTagFragmentGenerator)); 689 } 690 691 /** 692 * Creates an HTML image map. This method maps to 693 * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo, 694 * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}, using default 695 * generators. 696 * 697 * @param name the map name (<code>null</code> not permitted). 698 * @param info the chart rendering info (<code>null</code> not permitted). 699 * 700 * @return The map tag. 701 */ getImageMap(String name, ChartRenderingInfo info)702 public static String getImageMap(String name, ChartRenderingInfo info) { 703 return ImageMapUtilities.getImageMap(name, info, 704 new StandardToolTipTagFragmentGenerator(), 705 new StandardURLTagFragmentGenerator()); 706 } 707 708 /** 709 * Creates an HTML image map. This method maps directly to 710 * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo, 711 * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}. 712 * 713 * @param name the map name (<code>null</code> not permitted). 714 * @param info the chart rendering info (<code>null</code> not permitted). 715 * @param toolTipTagFragmentGenerator a generator for the HTML fragment 716 * that will contain the tooltip text (<code>null</code> not permitted 717 * if <code>info</code> contains tooltip information). 718 * @param urlTagFragmentGenerator a generator for the HTML fragment that 719 * will contain the URL reference (<code>null</code> not permitted if 720 * <code>info</code> contains URLs). 721 * 722 * @return The map tag. 723 */ getImageMap(String name, ChartRenderingInfo info, ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, URLTagFragmentGenerator urlTagFragmentGenerator)724 public static String getImageMap(String name, ChartRenderingInfo info, 725 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 726 URLTagFragmentGenerator urlTagFragmentGenerator) { 727 728 return ImageMapUtilities.getImageMap(name, info, 729 toolTipTagFragmentGenerator, urlTagFragmentGenerator); 730 731 } 732 733 } 734