From: lempinen Date: Fri, 14 Jun 2013 07:46:27 +0000 (+0000) Subject: Added support for exporting JFreeChart charts as SVG (fixes #4344) X-Git-Tag: 1.8.1~289 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=3327747592addc2560eb9271b634b3125dd6c260;p=simantics%2Fsysdyn.git Added support for exporting JFreeChart charts as SVG (fixes #4344) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27542 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.jfreechart/.classpath b/org.simantics.jfreechart/.classpath index 8a8f1668..ed5bcd8e 100644 --- a/org.simantics.jfreechart/.classpath +++ b/org.simantics.jfreechart/.classpath @@ -1,5 +1,12 @@ + + + + + + + diff --git a/org.simantics.jfreechart/META-INF/MANIFEST.MF b/org.simantics.jfreechart/META-INF/MANIFEST.MF index 945a09d6..fb0ab09c 100644 --- a/org.simantics.jfreechart/META-INF/MANIFEST.MF +++ b/org.simantics.jfreechart/META-INF/MANIFEST.MF @@ -35,3 +35,11 @@ Export-Package: org.simantics.jfreechart, org.simantics.jfreechart.chart.properties.bar, org.simantics.jfreechart.chart.properties.pie, org.simantics.jfreechart.chart.properties.xyline +Bundle-ClassPath: batik/batik-awt-util.jar, + batik/batik-dom.jar, + batik/batik-ext.jar, + batik/batik-svg-dom.jar, + batik/batik-svggen.jar, + batik/batik-util.jar, + batik/batik-xml.jar, + . diff --git a/org.simantics.jfreechart/batik/batik-1.7.zip b/org.simantics.jfreechart/batik/batik-1.7.zip new file mode 100644 index 00000000..b1f32e7d Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-1.7.zip differ diff --git a/org.simantics.jfreechart/batik/batik-awt-util.jar b/org.simantics.jfreechart/batik/batik-awt-util.jar new file mode 100644 index 00000000..e64605af Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-awt-util.jar differ diff --git a/org.simantics.jfreechart/batik/batik-dom.jar b/org.simantics.jfreechart/batik/batik-dom.jar new file mode 100644 index 00000000..32d5b46d Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-dom.jar differ diff --git a/org.simantics.jfreechart/batik/batik-ext.jar b/org.simantics.jfreechart/batik/batik-ext.jar new file mode 100644 index 00000000..8c904e1f Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-ext.jar differ diff --git a/org.simantics.jfreechart/batik/batik-src-1.7.zip b/org.simantics.jfreechart/batik/batik-src-1.7.zip new file mode 100644 index 00000000..77478cd3 Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-src-1.7.zip differ diff --git a/org.simantics.jfreechart/batik/batik-svg-dom.jar b/org.simantics.jfreechart/batik/batik-svg-dom.jar new file mode 100644 index 00000000..b4c8a620 Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-svg-dom.jar differ diff --git a/org.simantics.jfreechart/batik/batik-svggen.jar b/org.simantics.jfreechart/batik/batik-svggen.jar new file mode 100644 index 00000000..4d6bb144 Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-svggen.jar differ diff --git a/org.simantics.jfreechart/batik/batik-util.jar b/org.simantics.jfreechart/batik/batik-util.jar new file mode 100644 index 00000000..86d75e70 Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-util.jar differ diff --git a/org.simantics.jfreechart/batik/batik-xml.jar b/org.simantics.jfreechart/batik/batik-xml.jar new file mode 100644 index 00000000..d05eb25f Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-xml.jar differ diff --git a/org.simantics.jfreechart/build.properties b/org.simantics.jfreechart/build.properties index 5948409c..3806e2b7 100644 --- a/org.simantics.jfreechart/build.properties +++ b/org.simantics.jfreechart/build.properties @@ -4,4 +4,11 @@ bin.includes = META-INF/,\ .,\ plugin.xml,\ adapters.xml,\ - icons/ + icons/,\ + batik/batik-awt-util.jar,\ + batik/batik-dom.jar,\ + batik/batik-ext.jar,\ + batik/batik-svg-dom.jar,\ + batik/batik-svggen.jar,\ + batik/batik-util.jar,\ + batik/batik-xml.jar diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java index 150c6a26..ae9a23c4 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java @@ -1,8 +1,18 @@ package org.simantics.jfreechart.chart; +import java.awt.Dimension; +import java.awt.geom.Rectangle2D; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.util.ArrayList; import java.util.UUID; +import org.apache.batik.dom.GenericDOMImplementation; +import org.apache.batik.svggen.SVGGraphics2D; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; @@ -15,6 +25,8 @@ import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.operation.Layer0X; import org.simantics.sysdyn.JFreeChartResource; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; /** * Utilities for handling charts @@ -147,4 +159,29 @@ public class ChartUtils { return realizationURI; } + + + public static void writeSVG(org.jfree.chart.JFreeChart chart, Rectangle2D bounds, File file) throws IOException { + + // Get a DOMImplementation. + DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); + + // Create an instance of org.w3c.dom.Document. + Document document = domImpl.createDocument(null, "svg", null); + + // Create an instance of the SVG Generator. + SVGGraphics2D svgGenerator = new SVGGraphics2D(document); + + // Paint chart panel + svgGenerator.setSVGCanvasSize(new Dimension((int)bounds.getWidth(), (int)bounds.getHeight())); + chart.draw(svgGenerator, bounds); + + // Finally, stream out SVG to the standard output using + // UTF-8 encoding. + OutputStream outputStream = new FileOutputStream(file); + Writer out = new OutputStreamWriter(outputStream, "UTF-8"); + svgGenerator.stream(out, false); + outputStream.flush(); + outputStream.close(); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java index 0bd78c8a..9da9370e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java @@ -8,66 +8,65 @@ package org.simantics.sysdyn.ui.trend; //import java.io.UnsupportedEncodingException; //import java.io.Writer; +import java.awt.geom.Rectangle2D; +import java.io.File; +import java.io.IOException; + import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; -//import org.eclipse.swt.SWT; -//import org.eclipse.swt.widgets.FileDialog; -//import org.eclipse.swt.widgets.Shell; -//import org.eclipse.ui.handlers.HandlerUtil; -//import org.jfree.chart.JFreeChart; -//import org.w3c.dom.DOMImplementation; -//import org.w3c.dom.Document; -//import org.apache.batik.*; -//import org.apache.batik.dom.GenericDOMImplementation; -//import org.apache.batik.svggen.SVGGraphics2D; -//import org.apache.batik.svggen.SVGGraphics2DIOException; - -//This class needs Batik libraries to be imported, if SVG picture is really needed. +import org.eclipse.core.expressions.EvaluationContext; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.jfreechart.chart.ChartUtils; public class TrendToSvg extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { + EvaluationContext c = (EvaluationContext)event.getApplicationContext(); + Object o = c.getParent().getVariable("activePart"); + if(o != null && o instanceof TrendView) { + TrendView trendView = (TrendView) o; + + Shell shell = HandlerUtil.getActiveShellChecked(event); + + while(true) { + FileDialog fd = new FileDialog(shell, SWT.SAVE); + fd.setText("Export trend to SVG"); + String[] ext = {"*.svg"}; + fd.setFilterExtensions(ext); + String selected = fd.open(); + + if(selected == null) + return null; + + File file = new File(selected); + + if(file.exists()) { + MessageDialog dialog = new MessageDialog(shell, "Overwrite " + file.getName() + "?", null, file.getName() + " exits. Do you wan't to overwrite it?", 0, + new String[] { "Yes", "No" }, 0); + dialog.create(); + if (dialog.open() == 1) + continue; + } + + try { + ChartUtils.writeSVG( + trendView.getPanel().getChart(), + new Rectangle2D.Double(0, 0, trendView.getPanel().getWidth(), trendView.getPanel().getHeight()), + file); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + } -// final Shell shell = HandlerUtil.getActiveShellChecked(event); -// FileDialog fd = new FileDialog(shell, SWT.SAVE); -// fd.setText("Export trend to PNG"); -// String[] ext = {"*.svg"}; -// fd.setFilterExtensions(ext); -// String selected = fd.open(); -// -// File file = new File(selected); -// JFreeChart chart = TrendView.chart; -// -// DOMImplementation domImpl -// = GenericDOMImplementation.getDOMImplementation(); -// // Create an instance of org.w3c.dom.Document -// Document document = domImpl.createDocument(null, "svg", null); -// // Create an instance of the SVG Generator -// SVGGraphics2D svgGenerator = new SVGGraphics2D(document); -// // set the precision to avoid a null pointer exception in Batik 1.5 -// svgGenerator.getGeneratorContext().setPrecision(6); -// // Ask the chart to render into the SVG Graphics2D implementation -// chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null); -// // Finally, stream out SVG to a file using UTF-8 character to -// // byte encoding -// boolean useCSS = true; -// Writer out = null; -// try { -// out = new OutputStreamWriter( -// new FileOutputStream(file), "UTF-8"); -// } catch (UnsupportedEncodingException e) { -// e.printStackTrace(); -// } catch (FileNotFoundException e) { -// e.printStackTrace(); -// } -// try { -// svgGenerator.stream(out, useCSS); -// } catch (SVGGraphics2DIOException e) { -// e.printStackTrace(); -// } - System.out.println("Add Batik-libraries"); return null; }