KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > SVGPlotter


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit;
20
21 import java.io.FileWriter JavaDoc;
22
23 import jcckit.data.DataPlot;
24 import jcckit.graphic.ClippingRectangle;
25 import jcckit.plot.PlotCanvas;
26 import jcckit.renderer.SVGRenderer;
27 import jcckit.util.ConfigParameters;
28 import jcckit.util.Factory;
29 import jcckit.util.Format;
30 import jcckit.util.PropertiesBasedConfigData;
31
32 /**
33  * Creates an SVG document from a plot by using the
34  * {@link jcckit.renderer.SVGRenderer}.
35  *
36  * @author Franz-Josef Elmer
37  */

38 public class SVGPlotter {
39   private static final Format HEADER_FORMAT = new Format(
40         "<?xml version='1.0' encoding='ISO-8859-1'?>\n"
41       + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" "
42       + "\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
43       + "<svg viewBox='%9.7g %9.7g %9.7g %9.7g' preserveAspectRatio='x");
44
45   /**
46    * Creates an SVG document from the specified plot canvas. The root
47    * element is an <tt>&lt;svg&gt;</tt> element with the attributes
48    * <tt>viewBox</tt> determined by {@link jcckit.plot.PlotCanvas#getPaper}
49    * and <tt>preserveAspectRatio</tt> determined by
50    * {@link jcckit.plot.PlotCanvas#getHorizontalAnchor} and
51    * {@link jcckit.plot.PlotCanvas#getVerticalAnchor}.
52    * @param canvas Canvas from which the SVG document is created.
53    * @param renderer Fully qualified class name of the renderer.
54    * @throws ClassCastException if <tt>renderer</tt> is not of type
55    * {@link SVGRenderer}.
56    */

57   public static String JavaDoc makeSVG(PlotCanvas canvas, String JavaDoc renderer) {
58     ClippingRectangle paper = canvas.getPaper();
59     double w = paper.getMaxX() - paper.getMinX();
60     double h = paper.getMaxY() - paper.getMinY();
61     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(
62         HEADER_FORMAT.form(new double[] {paper.getMinX(), -paper.getMaxY(),
63                                          w, h}));
64     buffer.append(mapAnchor(canvas.getHorizontalAnchor().getFactor()))
65           .append('Y')
66           .append(mapAnchor(2 - canvas.getVerticalAnchor().getFactor()))
67           .append("'>\n");
68     
69     canvas.getPlot().getCompletePlot().renderWith(
70                 ((SVGRenderer) Factory.create(renderer))
71                         .init(buffer, 1, Math.sqrt(w * w + h * h)));
72     buffer.append("</svg>\n");
73     return new String JavaDoc(buffer);
74   }
75
76   private static String JavaDoc mapAnchor(int anchorFactor) {
77     return anchorFactor == 0 ? "Min" : (anchorFactor == 1 ? "Mid" : "Max");
78   }
79
80   /**
81    * Creates an SVG document based on a <tt>.properties</tt> file.
82    * <p>
83    * Usage: <tt>java jcckit.SVGPlotter [-o &lt;<i>output file</i>&gt;]
84    * &lt;<i>properties file</i>&gt;</tt>
85    * <p>
86    * If the <tt>-o</tt> option isn't present the SVG output will be
87    * printed onto the console.
88    */

89   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
90     if (args.length == 0) {
91       showUsageAndExit();
92     }
93     int index = 0;
94     String JavaDoc propFileName = args[0];
95     String JavaDoc outputFileName = null;
96     if (propFileName.equals("-o")) {
97       if (args.length > 2) {
98         outputFileName = args[1];
99         propFileName = args[2];
100       } else {
101         showUsageAndExit();
102       }
103       index += 2;
104     }
105     propFileName = args[index];
106     String JavaDoc renderer = "jcckit.renderer.SVGRenderer";
107     if (propFileName.equals("-r")) {
108       if (args.length > index + 2) {
109         renderer = args[++index];
110         propFileName = args[++index];
111       } else {
112         showUsageAndExit();
113       }
114     }
115     ConfigParameters config
116         = new ConfigParameters(new PropertiesBasedConfigData(propFileName));
117     PlotCanvas plotCanvas = new PlotCanvas(config);
118     plotCanvas.connect(DataPlot.create(config));
119     String JavaDoc output = makeSVG(plotCanvas, renderer);
120     if (outputFileName == null) {
121       System.out.println(output);
122     } else {
123       FileWriter JavaDoc writer = new FileWriter JavaDoc(outputFileName);
124       writer.write(output, 0, output.length());
125       writer.close();
126     }
127   }
128
129   private static void showUsageAndExit() {
130     System.out.println("Usage: java jcckit.SVGPlotter [-o <output file>] "
131                        + "[-r <renderer class>] <properties file>");
132     System.exit(1);
133   }
134 }
135
Popular Tags