From ee82986e40f8d6dab4e3830ace935a97eb3acf16 Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Sun, 22 Mar 2015 22:55:57 +0100 Subject: [PATCH] add "save as" for vector graphics (SVG, PDF, EPS) if VectorGraphics2D is found on classpath --- .../java/com/xeiam/xchart/XChartPanel.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/xchart/src/main/java/com/xeiam/xchart/XChartPanel.java b/xchart/src/main/java/com/xeiam/xchart/XChartPanel.java index db42fa65..e4053720 100644 --- a/xchart/src/main/java/com/xeiam/xchart/XChartPanel.java +++ b/xchart/src/main/java/com/xeiam/xchart/XChartPanel.java @@ -39,6 +39,7 @@ import javax.swing.KeyStroke; import javax.swing.filechooser.FileFilter; import com.xeiam.xchart.BitmapEncoder.BitmapFormat; +import com.xeiam.xchart.VectorGraphicsEncoder.VectorGraphicsFormat; /** * A Swing JPanel that contains a Chart @@ -118,6 +119,18 @@ public class XChartPanel extends JPanel { fileChooser.addChoosableFileFilter(pngFileFilter); fileChooser.addChoosableFileFilter(new SuffixSaveFilter("bmp")); fileChooser.addChoosableFileFilter(new SuffixSaveFilter("gif")); + + // VectorGraphics2D is optional, so if it's on the classpath, allow saving charts as vector graphic + try { + Class.forName("de.erichseifert.vectorgraphics2d.VectorGraphics2D"); + // it exists on the classpath + fileChooser.addChoosableFileFilter(new SuffixSaveFilter("svg")); + fileChooser.addChoosableFileFilter(new SuffixSaveFilter("eps")); + fileChooser.addChoosableFileFilter(new SuffixSaveFilter("pdf")); + } catch (ClassNotFoundException e) { + // it does not exist on the classpath + } + fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.setFileFilter(pngFileFilter); @@ -138,6 +151,12 @@ public class XChartPanel extends JPanel { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath().toString(), BitmapFormat.BMP); } else if (fileChooser.getFileFilter().getDescription().equals("*.gif,*.GIF")) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath().toString(), BitmapFormat.GIF); + } else if (fileChooser.getFileFilter().getDescription().equals("*.svg,*.SVG")) { + VectorGraphicsEncoder.saveVectorGraphic(chart, theFileToSave.getCanonicalPath().toString(), VectorGraphicsFormat.SVG); + } else if (fileChooser.getFileFilter().getDescription().equals("*.eps,*.EPS")) { + VectorGraphicsEncoder.saveVectorGraphic(chart, theFileToSave.getCanonicalPath().toString(), VectorGraphicsFormat.EPS); + } else if (fileChooser.getFileFilter().getDescription().equals("*.pdf,*.PDF")) { + VectorGraphicsEncoder.saveVectorGraphic(chart, theFileToSave.getCanonicalPath().toString(), VectorGraphicsFormat.PDF); } } catch (IOException e) { e.printStackTrace(); -- GitLab