Appendix B: Writing a Chart as a Bitmap Image File

Using the ImageIO class

A JMSL chart can be saved as an image file using the Java ImageIO class.

The chart tree is constructed in the usual manner. Here a method called
createChart is used to create a simple chart, with the null-argument Chart constructor.

The bitmap is generated by creating a BufferedImage object and painting the chart into the buffered image using the graphics object from the buffered image. The buffered image is written to a file using the ImageIO.write method.

This method of creating bitmaps does not require that a windowing system be running. The argument -Djava.awt.headlesss=true can be used with the java command to run Java in a "headless" mode.

(Download Code)

 

import com.imsl.chart.*;

import java.awt.image.BufferedImage;

import java.io.File;

 

public class SampleImageIO {

    public static void main(String argv[]) throws java.io.IOException {       

        Chart chart = createChart();

        chart.setScreenSize(new java.awt.Dimension(500,500));

       

        BufferedImage bi = new BufferedImage(500, 500,

            BufferedImage.TYPE_4BYTE_ABGR_PRE);

        chart.paintChart(bi.createGraphics());

       

        File file = new File("SampleImageIO.png");

        javax.imageio.ImageIO.write(bi, "PNG", file);      

    }

   

 

    static Chart createChart() {       

        Chart chart = new Chart();

        AxisXY axis = new AxisXY(chart);     

       

        int  npoints = 20;

        double dx = .5 * Math.PI/(npoints-1);

        double x[] = new double[npoints];       

        double y[] = new double[npoints];       

 

        //  Generate some data      

        for (int i = 0; i < npoints; i++){           

            x[i] = i * dx;           

            y[i] = Math.sin(x[i]);           

        }       

        new Data(axis, x, y);  

        return chart;

    }   

}

 

Using the Scalable Vector Graphics (SVG) API

Scalable Vector Graphics (SVG) is an XML vocabulary for vector graphics.

JMSL's SVG support is based on the Apache Batik toolkit. Batik is not included with JMSL Numerical Library. It can be downloaded, for free, from the Apache Batik site. The batik.jar file needs to be added to the CLASSPATH.

Once batik.jar is installed, a JMSL chart can be saved as an SVG file by using the Chart.writeSVG method.

SVG can also be created from a JFrameChart window. Its File | SaveAs menu allows the chart to be saved as either a PNG image or as an SVG file. The SVG option is active only if batik.jar is in the CLASSPATH.

The following example generates SVG output:

The output encoding format is set to UTF-8. This is done because SVG renderers do not support all encodings. The UTF-8 is the most general encoding.

 

(Download Code)

 

import com.imsl.chart.*;

import java.io.*;

import java.awt.*;

 

public class SampleSVG {

    public static void main(String argv[]) throws java.io.IOException {       

        Frame frame = new Frame();

        frame.show();

        frame.setSize(500, 500);

        frame.setVisible(false);       

        Chart chart = createChart(frame);

       

        OutputStream os = new FileOutputStream("SampleSVG.svg");

        Writer writer = new OutputStreamWriter(os, "UTF-8");

        chart.writeSVG(writer, true);

      

        System.exit(0);

    }

 

   

    static Chart createChart(Component component) {       

        Chart chart = new Chart(component);       

        AxisXY axis = new AxisXY(chart);     

       

        int  npoints = 20;

        double dx = .5 * Math.PI/(npoints-1);       

        double x[] = new double[npoints];       

        double y[] = new double[npoints];      

 

        //  Generate some data      

        for (int i = 0; i < npoints; i++){           

            x[i] = i * dx;           

            y[i] = Math.sin(x[i]);           

        }       

        new Data(axis, x, y);       

        return chart;       

    }   

}

 

 

 

 



Visual Numerics - Developers of IMSL and PV-WAVE
http://www.vni.com/
PHONE: 713.784.3131
FAX:713.781.9260