Java save XChart as a PDF

2.3k views Asked by At

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...?
}
2

There are 2 answers

0
dawnfly On BEST ANSWER

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);

} }

0
blong On

Similar to dawnfyly's answer, but a bit more specific to the OP's question and writes the output to a PDF only.

A PDF can be written using the VectorGraphicsEncoder, which is shown in the examples under the heading "Save Chart as Bitmap".

Here, I've slimmed down the example to just the relevant information:

package com.example;

import java.io.IOException;

import org.knowm.xchart.VectorGraphicsEncoder;
import org.knowm.xchart.VectorGraphicsEncoder.VectorGraphicsFormat;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYSeries;
import org.knowm.xchart.style.markers.SeriesMarkers;

public class TryXchartToPdf extends XchartBase {
    /**
     * Creates a simple Chart and saves it as a PDF file.
     * @throws IOException
     */

    public static void main(String[] args) throws IOException {

        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);

        VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.PDF);

    }
}