Showing a simple graph with XChart as SVG or PDF
Source code name: "SimpleXChart.java"
Programming language: Java
Topic: Plots/2D
DMelt Version 3. Last modified: 04/03/2023. License: Free
https://datamelt.org/code/cache/SimpleXChart_7641.java
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.BitmapEncoder.BitmapFormat;
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;

/** Creates a simple Chart and saves it as a PNG and JPEG image file. */
public class SimpleXChart {

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

    // saving options
    //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);
  }
}