ChartUtil { public static void writeChartAsSVG(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight) throws IOException { DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); Document document = domImpl.createDocument(null, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); chart.draw(svgGenerator, new Rectangle(aWidth, aHeight)); Writer out = new OutputStreamWriter(aOS, "UTF-8"); svgGenerator.stream(out, true ); out.flush(); out.close(); }  static void writeChartAsSVG(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight); static void writeChartAsPDF(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight);  }
@Test public void testSvg() throws Exception { double[][] data = new double[2][10]; for (int n = 1; n < 10; n++) { data[0][n] = 1.0 / n; data[1][n] = 1.0 - (1.0 / n); } DefaultXYDataset dataset = new DefaultXYDataset(); dataset.addSeries("data", data); JFreeChart chart = ChartFactory.createXYLineChart(null, "Recall", "Precision", dataset, PlotOrientation.VERTICAL, false, false, false); chart.getXYPlot().setRenderer(new XYSplineRenderer()); chart.getXYPlot().getRangeAxis().setRange(0.0, 1.0); chart.getXYPlot().getDomainAxis().setRange(0.0, 1.0); File tmp = File.createTempFile("testfile", ".svg"); try (OutputStream os = new FileOutputStream(tmp)) { ChartUtil.writeChartAsSVG(os, chart, 400, 400); } }