]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Added support for exporting JFreeChart charts as SVG (fixes #4344)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 14 Jun 2013 07:46:27 +0000 (07:46 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 14 Jun 2013 07:46:27 +0000 (07:46 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27542 ac1ea38d-2e2b-0410-8846-a27921b304fc

14 files changed:
org.simantics.jfreechart/.classpath
org.simantics.jfreechart/META-INF/MANIFEST.MF
org.simantics.jfreechart/batik/batik-1.7.zip [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-awt-util.jar [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-dom.jar [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-ext.jar [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-src-1.7.zip [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-svg-dom.jar [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-svggen.jar [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-util.jar [new file with mode: 0644]
org.simantics.jfreechart/batik/batik-xml.jar [new file with mode: 0644]
org.simantics.jfreechart/build.properties
org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToSvg.java

index 8a8f1668cdcc5c73a6921162616f01cb556ae7c1..ed5bcd8e6623466a755db5d0c6757274dba0e8b5 100644 (file)
@@ -1,5 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <classpath>\r
+       <classpathentry exported="true" kind="lib" path="batik/batik-awt-util.jar"/>\r
+       <classpathentry exported="true" kind="lib" path="batik/batik-dom.jar"/>\r
+       <classpathentry exported="true" kind="lib" path="batik/batik-ext.jar"/>\r
+       <classpathentry exported="true" kind="lib" path="batik/batik-svg-dom.jar"/>\r
+       <classpathentry exported="true" kind="lib" path="batik/batik-svggen.jar"/>\r
+       <classpathentry exported="true" kind="lib" path="batik/batik-util.jar"/>\r
+       <classpathentry exported="true" kind="lib" path="batik/batik-xml.jar"/>\r
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>\r
        <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>\r
        <classpathentry kind="src" path="src"/>\r
index 945a09d642ea3405bcf0d2592bc7f911a15a62f9..fb0ab09c9417bf85331f832f45d4ed451b60e385 100644 (file)
@@ -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 (file)
index 0000000..b1f32e7
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 (file)
index 0000000..e64605a
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 (file)
index 0000000..32d5b46
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 (file)
index 0000000..8c904e1
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 (file)
index 0000000..77478cd
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 (file)
index 0000000..b4c8a62
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 (file)
index 0000000..4d6bb14
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 (file)
index 0000000..86d75e7
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 (file)
index 0000000..d05eb25
Binary files /dev/null and b/org.simantics.jfreechart/batik/batik-xml.jar differ
index 5948409ce6293059224853be1efc4eb16f4acde0..3806e2b78e1c562a7c98c5c0d06f90beed5048c9 100644 (file)
@@ -4,4 +4,11 @@ bin.includes = META-INF/,\
                .,\\r
                plugin.xml,\\r
                adapters.xml,\\r
-               icons/\r
+               icons/,\\r
+               batik/batik-awt-util.jar,\\r
+               batik/batik-dom.jar,\\r
+               batik/batik-ext.jar,\\r
+               batik/batik-svg-dom.jar,\\r
+               batik/batik-svggen.jar,\\r
+               batik/batik-util.jar,\\r
+               batik/batik-xml.jar\r
index 150c6a263d14ba09b51be63f51ea8c69400fcda8..ae9a23c41a1b2c3669c54d89c9f74515166d6542 100644 (file)
@@ -1,8 +1,18 @@
 package org.simantics.jfreechart.chart;\r
 \r
+import java.awt.Dimension;\r
+import java.awt.geom.Rectangle2D;\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.io.OutputStreamWriter;\r
+import java.io.Writer;\r
 import java.util.ArrayList;\r
 import java.util.UUID;\r
 \r
+import org.apache.batik.dom.GenericDOMImplementation;\r
+import org.apache.batik.svggen.SVGGraphics2D;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
@@ -15,6 +25,8 @@ import org.simantics.layer0.Layer0;
 import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.operation.Layer0X;\r
 import org.simantics.sysdyn.JFreeChartResource;\r
+import org.w3c.dom.DOMImplementation;\r
+import org.w3c.dom.Document;\r
 \r
 /**\r
  * Utilities for handling charts\r
@@ -147,4 +159,29 @@ public class ChartUtils {
         \r
         return realizationURI;        \r
     }\r
+    \r
+\r
+    public static void writeSVG(org.jfree.chart.JFreeChart chart, Rectangle2D bounds, File file) throws IOException {\r
+\r
+        // Get a DOMImplementation.\r
+        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();\r
+\r
+        // Create an instance of org.w3c.dom.Document.\r
+        Document document = domImpl.createDocument(null, "svg", null);\r
+\r
+        // Create an instance of the SVG Generator.\r
+        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);\r
+\r
+        // Paint chart panel\r
+        svgGenerator.setSVGCanvasSize(new Dimension((int)bounds.getWidth(), (int)bounds.getHeight()));\r
+        chart.draw(svgGenerator, bounds);\r
+\r
+        // Finally, stream out SVG to the standard output using\r
+        // UTF-8 encoding.\r
+        OutputStream outputStream = new FileOutputStream(file);\r
+        Writer out = new OutputStreamWriter(outputStream, "UTF-8");\r
+        svgGenerator.stream(out, false);\r
+        outputStream.flush();\r
+        outputStream.close();\r
+    }\r
 }\r
index 0bd78c8ab625ab4f0ed52e10239001073bfb9958..9da9370e8b03592689d1c595e3d8c7beda66809b 100644 (file)
@@ -8,66 +8,65 @@ package org.simantics.sysdyn.ui.trend;
 //import java.io.UnsupportedEncodingException;\r
 //import java.io.Writer;\r
 \r
+import java.awt.geom.Rectangle2D;\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
 import org.eclipse.core.commands.AbstractHandler;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
 import org.eclipse.core.commands.ExecutionException;\r
-//import org.eclipse.swt.SWT;\r
-//import org.eclipse.swt.widgets.FileDialog;\r
-//import org.eclipse.swt.widgets.Shell;\r
-//import org.eclipse.ui.handlers.HandlerUtil;\r
-//import org.jfree.chart.JFreeChart;\r
-//import org.w3c.dom.DOMImplementation;\r
-//import org.w3c.dom.Document;\r
-//import org.apache.batik.*;\r
-//import org.apache.batik.dom.GenericDOMImplementation;\r
-//import org.apache.batik.svggen.SVGGraphics2D;\r
-//import org.apache.batik.svggen.SVGGraphics2DIOException;\r
-\r
-//This class needs Batik libraries to be imported, if SVG picture is really needed.\r
+import org.eclipse.core.expressions.EvaluationContext;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.jfreechart.chart.ChartUtils;\r
 \r
 public class TrendToSvg extends AbstractHandler {\r
 \r
     @Override\r
     public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        EvaluationContext c = (EvaluationContext)event.getApplicationContext();\r
+        Object o = c.getParent().getVariable("activePart");\r
+        if(o != null && o instanceof TrendView) {\r
+            TrendView trendView = (TrendView) o;\r
+\r
+            Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+            \r
+            while(true) {\r
+                FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
+                fd.setText("Export trend to SVG");\r
+                String[] ext = {"*.svg"};\r
+                fd.setFilterExtensions(ext);\r
+                String selected = fd.open();\r
+\r
+                if(selected == null)\r
+                    return null;\r
+\r
+                File file = new File(selected);\r
+\r
+                if(file.exists()) {\r
+                    MessageDialog dialog = new MessageDialog(shell, "Overwrite " + file.getName() + "?", null, file.getName() + " exits. Do you wan't to overwrite it?", 0,\r
+                            new String[] { "Yes", "No" }, 0);\r
+                    dialog.create();\r
+                    if (dialog.open() == 1)\r
+                        continue;\r
+                }\r
+\r
+                try {\r
+                    ChartUtils.writeSVG(\r
+                            trendView.getPanel().getChart(), \r
+                            new Rectangle2D.Double(0, 0, trendView.getPanel().getWidth(), trendView.getPanel().getHeight()),\r
+                            file);\r
+                } catch (IOException e) {\r
+                    e.printStackTrace();\r
+                } \r
+                \r
+                return null;\r
+            }\r
+        }\r
 \r
-//     final Shell shell = HandlerUtil.getActiveShellChecked(event);\r
-//     FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
-//     fd.setText("Export trend to PNG");\r
-//             String[] ext = {"*.svg"};\r
-//             fd.setFilterExtensions(ext);\r
-//     String selected = fd.open();\r
-//     \r
-//     File file = new File(selected);\r
-//     JFreeChart chart = TrendView.chart;\r
-//     \r
-//     DOMImplementation domImpl\r
-//     = GenericDOMImplementation.getDOMImplementation();\r
-//     // Create an instance of org.w3c.dom.Document\r
-//     Document document = domImpl.createDocument(null, "svg", null);\r
-//     // Create an instance of the SVG Generator\r
-//     SVGGraphics2D svgGenerator = new SVGGraphics2D(document);\r
-//     // set the precision to avoid a null pointer exception in Batik 1.5\r
-//     svgGenerator.getGeneratorContext().setPrecision(6);\r
-//     // Ask the chart to render into the SVG Graphics2D implementation\r
-//     chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);\r
-//     // Finally, stream out SVG to a file using UTF-8 character to\r
-//     // byte encoding\r
-//     boolean useCSS = true;\r
-//     Writer out = null;\r
-//             try {\r
-//                     out = new OutputStreamWriter(\r
-//                     new FileOutputStream(file), "UTF-8");\r
-//             } catch (UnsupportedEncodingException e) {\r
-//                     e.printStackTrace();\r
-//             } catch (FileNotFoundException e) {\r
-//                     e.printStackTrace();\r
-//             }\r
-//     try {\r
-//                     svgGenerator.stream(out, useCSS);\r
-//             } catch (SVGGraphics2DIOException e) {\r
-//                     e.printStackTrace();\r
-//             }\r
-       System.out.println("Add Batik-libraries");\r
         return null;\r
     }\r
    \r