1 25 package org.jrobin.graph; 26 27 import java.util.Iterator ; 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.Serializable ; 32 import java.io.ByteArrayOutputStream ; 33 import javax.imageio.ImageIO ; 34 import javax.imageio.IIOImage ; 35 import javax.imageio.ImageWriter ; 36 import javax.imageio.ImageWriteParam ; 37 import javax.imageio.stream.ImageOutputStream ; 38 import java.awt.image.RenderedImage ; 39 import java.awt.image.BufferedImage ; 40 import java.awt.*; 41 42 import org.jrobin.core.RrdOpener; 43 import org.jrobin.core.RrdException; 44 45 51 public class RrdGraph extends RrdOpener implements Serializable 52 { 53 private Grapher grapher; 57 private BufferedImage img; 58 59 private boolean useImageSize = false; 60 61 62 68 public RrdGraph() 69 { 70 super( false, true ); 71 } 72 73 77 public RrdGraph( boolean usePool ) 78 { 79 super( usePool, true ); 80 } 81 82 86 public RrdGraph( RrdGraphDef graphDef ) 87 { 88 this( graphDef, false ); 89 } 90 91 96 public RrdGraph( RrdGraphDef graphDef, boolean usePool ) 97 { 98 super( usePool, true ); 99 grapher = new Grapher( graphDef, this ); 100 } 101 102 103 112 public void specifyImageSize( boolean specImgSize ) 113 { 114 this.useImageSize = specImgSize; 115 } 116 117 121 public void setGraphDef( RrdGraphDef graphDef ) 122 { 123 img = null; 124 grapher = new Grapher( graphDef, this ); 125 } 126 127 135 public void saveAsPNG( String path ) throws RrdException, IOException 136 { 137 saveAsPNG( path, 0, 0 ); 138 } 139 140 149 public void saveAsPNG( String path, int width, int height ) throws RrdException, IOException 150 { 151 File imgFile = new File ( path ); 152 153 if ( shouldGenerate(imgFile) ) 154 ImageIO.write( getBufferedImage(width, height, BufferedImage.TYPE_INT_RGB), "png", imgFile ); 155 } 156 157 165 public void saveAsGIF( String path ) throws RrdException, IOException 166 { 167 saveAsGIF( path, 0, 0 ); 168 } 169 170 179 public void saveAsGIF(String path, int width, int height) throws RrdException, IOException 180 { 181 File imgFile = new File ( path ); 182 183 if ( shouldGenerate(imgFile) ) 184 { 185 GifEncoder gifEncoder = new GifEncoder( getBufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED) ); 186 FileOutputStream stream = new FileOutputStream ( path, false ); 187 188 gifEncoder.encode(stream); 189 190 stream.close(); 191 } 192 } 193 194 202 public void saveAsJPEG( String path, float quality ) throws RrdException, IOException 203 { 204 saveAsJPEG( path, 0, 0, quality ); 205 } 206 207 216 public void saveAsJPEG( String path, int width, int height, float quality ) throws RrdException, IOException 217 { 218 File imgFile = new File ( path ); 219 220 if ( !shouldGenerate(imgFile) ) 221 return; 222 223 RenderedImage rndImage = getBufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 226 227 ImageWriter writer = null; 229 Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); 230 if (iter.hasNext()) { 231 writer = (ImageWriter )iter.next(); 232 } 233 234 ImageOutputStream ios = ImageIO.createImageOutputStream(new File (path)); 236 writer.setOutput(ios); 237 238 ImageWriteParam iwparam = new JpegImageWriteParam(); 240 iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; 241 iwparam.setCompressionQuality(quality); 242 243 writer.write(null, new IIOImage (rndImage, null, null), iwparam); 245 246 ios.flush(); 248 writer.dispose(); 249 ios.close(); 250 } 251 252 257 public byte[] getPNGBytes() throws IOException , RrdException 258 { 259 return getPNGBytes( 0, 0 ); 260 } 261 262 269 public byte[] getPNGBytes( int width, int height ) throws IOException , RrdException 270 { 271 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 272 273 ImageIO.write(getBufferedImage(width, height, BufferedImage.TYPE_INT_RGB), "png", outputStream ); 274 275 return outputStream.toByteArray(); 276 } 277 278 284 public byte[] getJPEGBytes( float quality ) throws IOException , RrdException 285 { 286 return getJPEGBytes( 0, 0, quality ); 287 } 288 289 297 public byte[] getJPEGBytes( int width, int height, float quality ) throws IOException , RrdException 298 { 299 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 300 301 RenderedImage rndImage = getBufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 303 304 ImageWriter writer = null; 306 Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); 307 if (iter.hasNext()) { 308 writer = (ImageWriter )iter.next(); 309 } 310 311 ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); 313 writer.setOutput(ios); 314 315 ImageWriteParam iwparam = new JpegImageWriteParam(); 317 iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; 318 iwparam.setCompressionQuality(quality); 319 320 writer.write(null, new IIOImage (rndImage, null, null), iwparam); 322 323 ios.flush(); 325 writer.dispose(); 326 ios.close(); 327 328 return outputStream.toByteArray(); 329 } 330 331 336 public byte[] getGIFBytes() throws RrdException, IOException 337 { 338 return getGIFBytes( 0, 0 ); 339 } 340 341 348 public byte[] getGIFBytes(int width, int height) throws RrdException, IOException 349 { 350 BufferedImage image = getBufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED); 351 ByteArrayOutputStream bStream = new ByteArrayOutputStream (); 352 353 GifEncoder gifEncoder = new GifEncoder( image ); 354 gifEncoder.encode( bStream ); 355 356 return bStream.toByteArray(); 357 } 358 359 368 public BufferedImage getBufferedImage( int width, int height ) throws IOException , RrdException 369 { 370 return getBufferedImage( width, height, BufferedImage.TYPE_INT_RGB ); 371 } 372 373 377 public ChartPanel getChartPanel() throws RrdException, IOException 378 { 379 ChartPanel p = new ChartPanel(); 380 p.setChart( getBufferedImage(0, 0, BufferedImage.TYPE_INT_RGB) ); 381 382 return p; 383 } 384 385 394 public void renderImage( Graphics2D graphics, int width, int height ) throws RrdException, IOException 395 { 396 if ( useImageSize ) 397 grapher.renderImage( width, height, graphics, true ); 398 else 399 grapher.renderImage( width, height, graphics, false ); 400 } 401 402 409 public ExportData getExportData() throws RrdException { 410 return grapher.createExportData(); 411 } 412 413 422 public ExportData fetchExportData() throws RrdException, IOException { 423 return grapher.fetch( Grapher.DEFAULT_WIDTH ); 424 } 425 426 438 public ExportData fetchExportData( int maxRows ) throws RrdException, IOException { 439 return grapher.fetch( maxRows ); 440 } 441 442 454 private boolean shouldGenerate( File imgFile ) throws RrdException, IOException 455 { 456 if ( !imgFile.exists() ) 457 return true; 458 459 return grapher.shouldGenerate( imgFile.lastModified() ); 460 } 461 462 private BufferedImage getBufferedImage(int width, int height, int colorType) throws RrdException, IOException 463 { 464 if ( useImageSize ) 466 img = grapher.createImageGlobal( width, height, colorType ); 467 else 468 img = grapher.createImage( width, height, colorType ); 469 470 return img; 471 } 472 } 473 | Popular Tags |