Using XChart I can make nice graphs. When I make the graph, I can right-click it, and an option to "save as" comes up. Then, I can save the image in a specified format, to a specified directory by clicking around.
How could I write Java code that automates this for me? That is, I have an application that creates about fifty graphs, and I don't want to have to deal with manually saving each one where it belongs every time I run the app.
Here's how I make my chart:
private void makeLineChart(int[][] data, String title) {
MyLineChart c = new MyLineChart(data, title);
XYChart chart = c.getChart();
new SwingWrapper<XYChart>(chart).displayChart();
// save as pdf...?
}
The sample code of xchart has the solution. BitmapEncoder can do that everytime you start your app.BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.PNG);
public class Example1 {
public static void main(String[] args) throws Exception {
double[] yData = new double[] { 2.0, 1.0, 0.0 };
// Create Chart
XYChart chart = new XYChart(500, 400);
chart.setTitle("Sample Chart");
chart.setXAxisTitle("X");
chart.setXAxisTitle("Y");
XYSeries series = chart.addSeries("y(x)", null, yData);
series.setMarker(SeriesMarkers.CIRCLE);
BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.PNG);
BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.JPG);
BitmapEncoder.saveJPGWithQuality(chart, "./Sample_Chart_With_Quality.jpg", 0.95f);
BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.BMP);
BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.GIF);
BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.PNG, 300);
BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.JPG, 300);
BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.GIF, 300);
VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.EPS);
VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.PDF);
VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.SVG);
} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With