Image plug-ins allow to save the rendered chart into image files in different image formats. They are also used in servlet environments where they generate image data and sent it to servlet output stream. Also it is possible to set insets for images generated by all image plug-ins. In order for these plug-ins to work properly, they must use a separate thread.
These are the main image plug-ins:
| Class | Description |
|---|---|
| GIFEncoderPlugin | Constructs uncompressed GIF file |
| JPEGEncoderPlugin | Constructs JPEG file |
| PNGEncoderPlugin | Constructs PNG file, which is considered to be best format for Web graphics |
| SVGEncoderPlugin | Constructs SVG file, but this plug-in requires Apache Batik product |
| TIFFEncoderPlugin | Constructs TIFF file |
Table 5.7.2.1 Image Plug-ins
You can specify the size of the GIF picture, or create it according to the chart view size. Generated GIF image is uncompressed. It is sometimes comfortable to use GIF format, since you may choose to use color palette and it is very useful for the WEB applications.
Also you can specify the insets that will surround the original chart view or view of chart container in the resulting image:
GIFEncoderPlugin gifPlugin = new GIFEncoderPlugin (chart); gifPlugin.setInsets(new Insets(10, 10, 10 , 10)); |
If you wish to generate image file you must provide output stream for generateImageFile method:
gifPlugin.generateImageFile( new BufferedOutputStream(new FileOutputStream (new File("gifimage.gif")))); |
If you wish to change some extra properties for GIFEncoder, such as transparency or color palette, for the GIF encoder, you must to retrieve the encoder class from GIFEncoderPlugin and cast it to GIFEncoder class and then set those properties:
1 | GIFEncoder encoder =(GIFEncoder)gifPlugin.getEncoder(); encoder.setColorPalette(GIFEncoder.COLOR_PALETTE_WEB); encoder.setTransparent(true); |
You can specify the size of the JPEG picture, or create it according to the chart view size. Generally the JPEG picture is compressed and by default uses 50% quality compression. It is possible to specify the insets that will surround the original chart view or view of chart container in the resulting image as well:
JPEGEncoderPlugin jpegPlugin = new JPEGEncoderPlugin(chart); jpegPlugin.setInsets(new Insets(10, 10, 10 , 10)); |
If you wish to generate image file you must provide output stream for generateImageFile method:
jpegPlugin.generateImageFile( new BufferedOutputStream( new FileOutputStream (new File("jpegimage.jpg”)))); |
Somtimes you may require to change some of the JPEGEncoder properties. You can do that by retrieving the encoder class from JPEGEncoderPlugin and casting it to JPEGEncoder class and then setting those properties. You can specify the quality of the picture, horizontal and vertical DPI, which is used for picture output on printers:
1 | JPEGEncoder encoder = (JPEGEncoder)jpgPlugin.getEncoder(); encoder.setQuality(1.0f, true); encoder.setPhysicalDimension(600,600); |
This plug-in allows you to create PNG (Portable Network Graphics) image. PNG pictures are very popular in WEB applications. It is possible to specify the insets that will surround the original chart view or view of chart container in the resulting image as well:
PNGEncoderPlugin pngPlugin = new PNGEncoderPlugin (chart); pngPlugin.setInsets(new Insets(10, 10, 10 , 10)); |
If you wish to generate image file you must provide output stream for generateImageFile method:
pngPlugin.generateImageFile( new BufferedOutputStream( new FileOutputStream (new File("pngimage.png”)))); |
The PNG images as well as JPEG images let you to specify DPI which is used for output on printers. In order to do that you must retrieve the encoder from the plug-in and then cast it as it is shown in the example:
PNGEncoder encoder = (PNGEncoder) pngPlugin.getEncoder(); encoder.setPhysicalDimension(600,600); |
Note: PNG encoder uses java built-in functions.
This plug-in allows create SVG (Scalable Vector Graphics) image. It will require Apache Batik product in order to use it since this plug-in requires Batik libraries. You may specify the insets that will surround the original chart view or view of chart container in the resulting image:
SVGEncoderPlugin svgPlugin = new SVGEncoderPlugin (chart); svgPlugin.setInsets(new Insets(10, 10, 10 , 10)); |
If you wish to generate image file you must provide output stream for generateImageFile method:
svgPlugin.generateImageFile( new BufferedOutputStream( new FileOutputStream (new File("svgimage.svg")))); |
This plug-in allows create TIFF (Tagged Image File Format) image. TIFF is a flexible, adaptable file format for handling images and data within a single file, by including the header tags (size, definition, image-data arrangement, applied image compression) defining the image's geometry.
You may specify :
TIFFEncoderPlugin tiffPlugin = new TIFFEncoderPlugin (chart); tiffPlugin.setTiffType(TIFFType.CMYK); tiffPlugin.setInsets(new Insets(10, 10, 10 , 10)); |
If you wish to generate image file you must provide output stream for generateImageFile method:
tiffPlugin.generateImageFile( new BufferedOutputStream(new FileOutputStream(new File("tiffimage.tiff")))); |
ChartImageBuilder is a feature for servlets. You just have to pass the view to the constructor of ChartImageBuilder, so it can be either LabeledChart, Chart2D or Chart3D or any other object implementing View interface.
Using ChartImageBuilder:
1 | //Since ChartImageBuilder may throw an exception, try/catch blocks are needed try { // constructs ChartImageBuilder object, named builder, // with object implementing View interface, named chart ChartImageBuilder builder = new ChartImageBuilder(chart); //sets size builder.setSize(new Dimension(660, 660)); //invalidates View object, as it might consume a lot of RAM chart.invalidate(); // renders image builder.render(); // creates image Image img = builder.getImage(); } catch (Exception e) { //in case something went wrong e.printStackTrace(); } |