JMSL Chart Programmer’s Guide
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.
View code file
 
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;
}
}