├── .gitignore
├── .hgignore
├── CHANGELOG.txt
├── LICENCE.txt
├── README.txt
├── base
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── panayotis
│ │ └── gnuplot
│ │ ├── GNUPlot.java
│ │ ├── GNUPlotException.java
│ │ ├── GNUPlotExec.java
│ │ ├── GNUPlotParameters.java
│ │ ├── JavaPlot.java
│ │ ├── PropertiesHolder.java
│ │ ├── dataset
│ │ ├── ArrayDataSet.java
│ │ ├── DataSet.java
│ │ ├── FileDataSet.java
│ │ ├── GenericDataSet.java
│ │ ├── Point.java
│ │ ├── PointDataSet.java
│ │ └── parser
│ │ │ ├── DataParser.java
│ │ │ ├── DoubleDataParser.java
│ │ │ ├── FloatDataParser.java
│ │ │ ├── IntegerDataParser.java
│ │ │ ├── LongDataParser.java
│ │ │ └── NumericDataParser.java
│ │ ├── layout
│ │ ├── AutoGraphLayout.java
│ │ ├── GraphLayout.java
│ │ ├── GridGraphLayout.java
│ │ ├── LayoutMetrics.java
│ │ ├── ManualGraphLayout.java
│ │ └── StripeLayout.java
│ │ ├── plot
│ │ ├── AbstractPlot.java
│ │ ├── Axis.java
│ │ ├── DataSetPlot.java
│ │ ├── FunctionPlot.java
│ │ ├── Graph.java
│ │ ├── Graph3D.java
│ │ ├── Page.java
│ │ └── Plot.java
│ │ ├── style
│ │ ├── FillStyle.java
│ │ ├── NamedPlotColor.java
│ │ ├── PlotColor.java
│ │ ├── PlotStyle.java
│ │ ├── RgbPlotColor.java
│ │ ├── Smooth.java
│ │ └── Style.java
│ │ ├── swing
│ │ ├── JPlot.form
│ │ └── JPlot.java
│ │ ├── terminal
│ │ ├── CustomTerminal.java
│ │ ├── DefaultTerminal.java
│ │ ├── ExpandableTerminal.java
│ │ ├── FileTerminal.java
│ │ ├── GNUPlotTerminal.java
│ │ ├── ImageTerminal.java
│ │ ├── PostscriptTerminal.java
│ │ └── TextFileTerminal.java
│ │ └── utils
│ │ ├── Debug.java
│ │ └── FileUtils.java
│ └── test
│ └── java
│ └── com
│ └── panayotis
│ └── gnuplot
│ └── demo
│ └── BaseDemo.java
├── build.gradle
├── pom.xml
└── svg
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── panayotis
│ └── gnuplot
│ └── terminal
│ └── SVGTerminal.java
└── test
└── java
└── com
└── panayotis
└── gnuplot
└── demo
└── SVGDemo.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /base/target/
2 | /svg/target/
3 |
4 | **/*.iml
5 |
--------------------------------------------------------------------------------
/.hgignore:
--------------------------------------------------------------------------------
1 | # These lines are suggested according to the svn:ignore property
2 | # Feel free to enable them by uncommenting them
3 |
4 | syntax: regexp
5 |
6 | \.gradle/
7 | \.nb-gradle/
8 | gradle.properties
9 |
--------------------------------------------------------------------------------
/CHANGELOG.txt:
--------------------------------------------------------------------------------
1 | 0.5.0
2 | Fix Windows issues: use temporary files instead of pipes.
3 |
4 | 0.4.0
5 | Basic support of Graph3D (splot). Implementation of user-defined
6 | terminals.
7 |
8 | 0.3.0
9 | Support of various data formats, i.e. date formats.
10 | Better plot layout support
11 |
12 | 0.2.0
13 | Support of multiplots
14 |
15 |
16 | 0.1.0
17 | Initial release
18 |
--------------------------------------------------------------------------------
/LICENCE.txt:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
35 | * It also provides some convenient methods in order to set various parameters. 36 | *
37 | * This object is not serializable, use GNUPlotParameters instead. 38 | * 39 | * @author teras 40 | * @see com.panayotis.gnuplot.JavaPlot 41 | * @see #addPlot(Plot) 42 | */ 43 | public class GNUPlot { 44 | 45 | private static final long serialVersionUID = GNUPlot.serialVersionUID; 46 | /** 47 | * GNUPlot parameters. Here we store all gnuplot information. 48 | */ 49 | private GNUPlotParameters param; 50 | /** 51 | * The GNUPlot parameter to use 52 | */ 53 | private transient GNUPlotTerminal term; 54 | /** 55 | * The GNUPlotExec to use. 56 | */ 57 | private transient GNUPlotExec exec; 58 | private static transient Debug dbg = new Debug(); 59 | 60 | /** 61 | * Create a new instance of gnuplot, using the default parameters 62 | * 63 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable 64 | * is not found, this exception is thrown. Typically at this case there is 65 | * need to use a constructor which defines the gnuplot path. 66 | * @see #GNUPlot(String) 67 | * @see #GNUPlot(GNUPlotParameters,String,GNUPlotTerminal,boolean) 68 | */ 69 | public GNUPlot() throws GNUPlotException { 70 | this(null, null, null, false); 71 | } 72 | 73 | /** 74 | * Create a new instance of gnuplot, using the default parameters. Use this 75 | * method if you want to specifically define that the default plot is 76 | * Graph3D 77 | * 78 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable 79 | * is not found, this exception is thrown. Typically at this case there is 80 | * need to use a constructor which defines the gnuplot path. 81 | * @param isGraph3D true if the default plot is Graph3D 82 | * @see #GNUPlot(String) 83 | * @see #GNUPlot(GNUPlotParameters,String,GNUPlotTerminal,boolean) 84 | */ 85 | public GNUPlot(boolean isGraph3D) throws GNUPlotException { 86 | this(null, null, null, isGraph3D); 87 | } 88 | 89 | /** 90 | * Create a new instance of gnuplot, with a given set of parameters. 91 | * 92 | * @see #GNUPlot(String) 93 | * @see #GNUPlot(GNUPlotParameters,String,GNUPlotTerminal,boolean) 94 | * @param par Use this set of parameters, instead of a default one. 95 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable 96 | * is not found, this exception is thrown. Typically at this case there is 97 | * need to use a constructor which defines the gnuplot path. 98 | */ 99 | public GNUPlot(GNUPlotParameters par) throws GNUPlotException { 100 | this(par, null, null, false); 101 | } 102 | 103 | /** 104 | * Create a new instance of gnuplot, with a given path for gnuplot. This 105 | * constructor is useful if the automatic path search for gnuplot is not 106 | * fruitful, or the user wants to point to a specific gnuplot executable. 107 | * 108 | * @param gnuplotpath The pathname of the gnuplot executable. If this 109 | * parameter is set to null, use the default path. 110 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable 111 | * is not found, this exception is thrown. It means that the provided path 112 | * for gnuplot is not valid. 113 | */ 114 | public GNUPlot(String gnuplotpath) throws GNUPlotException { 115 | this(null, gnuplotpath, null, false); 116 | } 117 | 118 | /** 119 | * Create a new instance of gnuplot, with a given path for gnuplot. This 120 | * constructor is useful if the automatic path search for gnuplot is not 121 | * fruitful, or the user wants to point to a specific gnuplot executable. 122 | * 123 | * @param gnuplotpath The pathname of the gnuplot executable. If this 124 | * parameter is set to null, use the default path. 125 | * @param isGraph3D true if the default plot is Graph3D 126 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable 127 | * is not found, this exception is thrown. It means that the provided path 128 | * for gnuplot is not valid. 129 | */ 130 | public GNUPlot(String gnuplotpath, boolean isGraph3D) throws GNUPlotException { 131 | this(null, gnuplotpath, null, isGraph3D); 132 | } 133 | 134 | /** 135 | * Create a new instance of gnuplot, with given parameters and given path 136 | * for gnuplot. 137 | *
138 | * This constructor is useful if the user wants to fine tune eny aspect of 139 | * GNUPlot object, and especially if there is need to define a priori the 140 | * output terminal. 141 | *
142 | * Any parameters set to null, produce the default parameters. 143 | * 144 | * @param par GNUPlot parameters to use. These parameters encapsulate the 145 | * whole gnuplot variables, including data sets. 146 | * @param gnuplotpath The pathname of the gnuplot executable. If this 147 | * parameter is set to null, use the default path. 148 | * @param term The gnuplot terminal to use 149 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable 150 | * is not found, this exception is thrown. It means that the provided path 151 | * for gnuplot is not valid. 152 | */ 153 | public GNUPlot(GNUPlotParameters par, String gnuplotpath, GNUPlotTerminal term) throws GNUPlotException { 154 | this(par, gnuplotpath, term, false); 155 | } 156 | 157 | /** 158 | * Create a new instance of gnuplot, with given parameters and given path 159 | * for gnuplot. 160 | *
161 | * This constructor is useful if the user wants to fine tune eny aspect of 162 | * GNUPlot object, and especially if there is need to define a priori the 163 | * output terminal. 164 | *
165 | * Any parameters set to null, produce the default parameters.
166 | *
167 | * @param par GNUPlot parameters to use. These parameters encapsulate the
168 | * whole gnuplot variables, including data sets.
169 | * @param gnuplotpath The pathname of the gnuplot executable. If this
170 | * parameter is set to null, use the default path.
171 | * @param term The gnuplot terminal to use
172 | * @param isGraph3D true, if this is a Graph3D object
173 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable
174 | * is not found, this exception is thrown. It means that the provided path
175 | * for gnuplot is not valid.
176 | */
177 | public GNUPlot(GNUPlotParameters par, String gnuplotpath, GNUPlotTerminal term, boolean isGraph3D) throws GNUPlotException {
178 | if (par == null)
179 | par = new GNUPlotParameters(isGraph3D);
180 | this.param = par;
181 |
182 | if (term == null)
183 | term = new DefaultTerminal();
184 | this.term = term;
185 |
186 | try {
187 | exec = new GNUPlotExec(gnuplotpath);
188 | } catch (IOException e) {
189 | String msg = e.getMessage();
190 | if (gnuplotpath == null)
191 | msg += " Please provide gnuplot path to the constructor of GNUPlot.";
192 | throw new GNUPlotException(msg);
193 | }
194 | }
195 |
196 | /**
197 | * Set various GNUPlot parameters. All parameters added here will be used in
198 | * the form of "set key value"
199 | *
200 | * @param key The key to use for this gnuplot
201 | * @param value The value of this key
202 | */
203 | public void set(String key, String value) {
204 | param.set(key, value);
205 | }
206 |
207 | /**
208 | * Use this method to get a reference to the plot axis, in order to set
209 | * various parameters.
210 | *
211 | * @param axisname The name of the axis. This typically is "x", "y", "z".
212 | * @return The requested Axis, or null if axis is not found
213 | */
214 | public Axis getAxis(String axisname) {
215 | return param.getAxis(axisname);
216 | }
217 |
218 | /**
219 | * Add a new Plot
220 | *
221 | * @param plot The plot to add to the list of plots.
222 | * @see com.panayotis.gnuplot.plot.Plot
223 | */
224 | public void addPlot(Plot plot) {
225 | if (plot == null)
226 | return;
227 | param.addPlot(plot);
228 | }
229 |
230 | /**
231 | * Add a defined graph to this GNUPlot object.
232 | *
233 | * @param gr Graph object to be added
234 | * @see #newGraph()
235 | */
236 | public void addGraph(Graph gr) {
237 | param.addGraph(gr);
238 | }
239 |
240 | /**
241 | * Add a new Graph object. This method is used to create a multiplot graph.
242 | * Every "plot" command corresponds to a different Graph object. In order to
243 | * draw to a new plot gnuplot object, create a new page.
244 | *
245 | * @see #newGraph3D()
246 | */
247 | public void newGraph() {
248 | param.newGraph();
249 | }
250 |
251 | /**
252 | * Add a new Graph3D object. This method is used to create a multiplot
253 | * graph. Every "splot" command corresponds to a different Graph object. In
254 | * order to draw to a new plot gnuplot object, create a new page.
255 | *
256 | * @see #newGraph()
257 | */
258 | public void newGraph3D() {
259 | param.newGraph3D();
260 | }
261 |
262 | /**
263 | * Set the title of all graph objects, in multiplot environment.
264 | *
265 | * @param title The title to use
266 | */
267 | public void setMultiTitle(String title) {
268 | param.setMultiTitle(title);
269 | }
270 |
271 | /**
272 | * Get a list of the (default) plots used in this set. This method is a way
273 | * to enumerate the plots already inserted, epspecially if a plot is added
274 | * on the fly.
275 | *
276 | * @return An array of stored plots.
277 | */
278 | public List
30 | * This object is not serializable, use GNUPlotParameters instead.
31 | *
32 | * @author teras
33 | */
34 | public class JavaPlot extends GNUPlot {
35 |
36 | /**
37 | * Create a new instance of JavaPlot, with the default parameters
38 | *
39 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable
40 | * is not found, this exception is thrown. Typically at this case there is
41 | * need to use a constructor which defines the gnuplot path.
42 | * @see GNUPlot#GNUPlot()
43 | */
44 | public JavaPlot() throws GNUPlotException {
45 | super();
46 | }
47 |
48 | /**
49 | * Create a new instance of JavaPlot, using the default parameters. Use this
50 | * method if you want to specifically define that the default plot is
51 | * Graph3D
52 | *
53 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable
54 | * is not found, this exception is thrown. Typically at this case there is
55 | * need to use a constructor which defines the gnuplot path.
56 | * @param isGraph3D true if the default plot is Graph3D
57 | * @see GNUPlot#GNUPlot(boolean)
58 | */
59 | public JavaPlot(boolean isGraph3D) throws GNUPlotException {
60 | super(isGraph3D);
61 | }
62 |
63 | /**
64 | * Create a new JavaPlot object with a given gnuplot path
65 | *
66 | * @param gnuplotpath
67 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable
68 | * is not found, this exception is thrown. It means that the provided path
69 | * for gnuplot is not valid.
70 | * @see GNUPlot#GNUPlot(String)
71 | */
72 | public JavaPlot(String gnuplotpath) throws GNUPlotException {
73 | super(gnuplotpath);
74 | }
75 |
76 | /**
77 | * Create a new instance of gnuplot, with a given path for gnuplot. This
78 | * constructor is useful if the automatic path search for gnuplot is not
79 | * fruitful, or the user wants to point to a specific gnuplot executable.
80 | *
81 | * @param gnuplotpath The pathname of the gnuplot executable. If this
82 | * parameter is set to null, use the default path.
83 | * @param isGraph3D true if the default plot is Graph3D
84 | * @throws com.panayotis.gnuplot.GNUPlotException If the gnuplot executable
85 | * is not found, this exception is thrown. It means that the provided path
86 | * for gnuplot is not valid.
87 | * @see GNUPlot#GNUPlot(String,boolean)
88 | */
89 | public JavaPlot(String gnuplotpath, boolean isGraph3D) throws GNUPlotException {
90 | super(gnuplotpath, isGraph3D);
91 | }
92 |
93 | /**
94 | * Create a new JavaPlot object with given parameters
95 | *
96 | * @param par
97 | * @throws com.panayotis.gnuplot.GNUPlotException
98 | * @see GNUPlot#GNUPlot(GNUPlotParameters)
99 | */
100 | public JavaPlot(GNUPlotParameters par) throws GNUPlotException {
101 | super(par);
102 | }
103 |
104 | /**
105 | * Create a new JavaPlot object with given parameters, gnuplot path and
106 | * terminal
107 | *
108 | * @param par
109 | * @param gnuplotpath
110 | * @param term
111 | * @throws com.panayotis.gnuplot.GNUPlotException
112 | * @see GNUPlot#GNUPlot(GNUPlotParameters,String,GNUPlotTerminal)
113 | */
114 | public JavaPlot(GNUPlotParameters par, String gnuplotpath, GNUPlotTerminal term) throws GNUPlotException {
115 | super(par, gnuplotpath, term);
116 | }
117 |
118 | /**
119 | * Create a new JavaPlot object with given parameters, gnuplot path terminal
120 | * ans isGraph3D
121 | *
122 | * @param par
123 | * @param gnuplotpath
124 | * @param term
125 | * @param isGraph3D
126 | * @throws com.panayotis.gnuplot.GNUPlotException
127 | * @see GNUPlot#GNUPlot(GNUPlotParameters,String,GNUPlotTerminal,boolean)
128 | */
129 | public JavaPlot(GNUPlotParameters par, String gnuplotpath, GNUPlotTerminal term, boolean isGraph3D) throws GNUPlotException {
130 | super(par, gnuplotpath, term, isGraph3D);
131 | }
132 |
133 | /**
134 | * Set the graph Title
135 | *
136 | * @param title Title of the graph
137 | */
138 | public void setTitle(String title) {
139 | setTitle(title, null, -1);
140 | }
141 |
142 | /**
143 | * Set the graph title and the title font
144 | *
145 | * @param title Title of the graph
146 | * @param font Font name of this title text
147 | * @param size Font size of this title text
148 | */
149 | public void setTitle(String title, String font, int size) {
150 | String fontname = "";
151 | if (font != null)
152 | fontname = " font '" + font + ((size > 1) ? "," + size : "") + "'";
153 | set("title", "'" + title + "'" + fontname);
154 | }
155 |
156 | /**
157 | *
158 | */
159 | public static enum Key {
160 |
161 | OFF, TOP_RIGHT, BOTTOM_RIGHT, TOP_LEFT, BOTTOM_LEFT, BELOW, OUTSIDE
162 | };
163 |
164 | /**
165 | *
166 | * @param position
167 | */
168 | public void setKey(Key position) {
169 | if (position == null)
170 | set("key", null);
171 | else
172 | set("key", position.name().replace('_', ' ').toLowerCase());
173 | }
174 |
175 | /**
176 | *
177 | * @param points
178 | */
179 | public void addPlot(double[][] points) {
180 | addPlot(new DataSetPlot(points));
181 | }
182 |
183 | /**
184 | *
185 | * @param points
186 | */
187 | public void addPlot(float[][] points) {
188 | addPlot(new DataSetPlot(points));
189 | }
190 |
191 | /**
192 | *
193 | * @param points
194 | */
195 | public void addPlot(int[][] points) {
196 | addPlot(new DataSetPlot(points));
197 | }
198 |
199 | /**
200 | *
201 | * @param points
202 | */
203 | public void addPlot(long[][] points) {
204 | addPlot(new DataSetPlot(points));
205 | }
206 |
207 | /**
208 | *
209 | * @param function
210 | */
211 | public void addPlot(String function) {
212 | addPlot(new FunctionPlot(function));
213 | }
214 |
215 | /**
216 | *
217 | * @param set
218 | */
219 | public void addPlot(DataSet set) {
220 | addPlot(new DataSetPlot(set));
221 | }
222 | }
223 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/PropertiesHolder.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 19, 2007, 2:20 AM
17 | */
18 | package com.panayotis.gnuplot;
19 |
20 | import java.util.HashMap;
21 | import java.util.Map.Entry;
22 |
23 | /**
24 | * This object is a data placeholder of various text-based parameters, used in
25 | * JavaPlot graph objects.
28 | * In this dataset one can use any type of data, while in PointDataSet the data
29 | * are stricted to numerical data only. Thus, data such as dates can be used.
30 | *
31 | * @see com.panayotis.gnuplot.dataset.parser.DataParser
32 | * @see com.panayotis.gnuplot.dataset.PointDataSet
33 | * @author teras
34 | */
35 | public class GenericDataSet implements DataSet, Serializable {
36 |
37 | private final DataParser parser;
38 | private List
28 | * If your data are not only numerical, consider using a GenericDataSet instead.
29 | *
30 | * @param
24 | * If you manually set metrics and use this, these metrics will be lost
25 | *
26 | * @author teras
27 | */
28 | public class AutoGraphLayout implements GraphLayout, Serializable {
29 |
30 | /**
31 | * Orientation of the graph layout
32 | */
33 | public static final boolean DOWNWARDS = true, UPWARDS = false;
34 | /**
35 | * Draw rows or columns first
36 | */
37 | public static final boolean ROWSFIRST = true, COLUMNSFIRST = false;
38 | private boolean orientation;
39 | private boolean drawfirst;
40 | private int rows, cols;
41 |
42 | /**
43 | * Create a new automatic grid layout. Default values are ROWSFIRST,
44 | * DOWNWARDS, automatic layout of components
45 | */
46 | public AutoGraphLayout() {
47 | drawfirst = ROWSFIRST;
48 | orientation = DOWNWARDS;
49 | rows = -1;
50 | cols = -1;
51 | }
52 |
53 | /**
54 | * Set where the first graph will be put
55 | *
56 | * @param drawfirst Position of the first graph
57 | * @see #ROWSFIRST
58 | * @see #COLUMNSFIRST
59 | */
60 | public void setDrawFirst(boolean drawfirst) {
61 | this.drawfirst = drawfirst;
62 | }
63 |
64 | /**
65 | * Sey the orientation of the graphs, as being put
66 | *
67 | * @param orientation Selected orientation
68 | * @see #DOWNWARDS
69 | * @see #UPWARDS
70 | */
71 | public void setOrientation(boolean orientation) {
72 | this.orientation = orientation;
73 | }
74 |
75 | /**
76 | * Manually set the number of rows. This method overrides the automatic
77 | * component layout.
78 | *
79 | * @param rows Desired number of rows. Set it to -1 to be automatically
80 | * computed.
81 | */
82 | public void setRows(int rows) {
83 | this.rows = rows;
84 | }
85 |
86 | /**
87 | * Manually set the number of columns. This method overrides the automatic
88 | * component layout.
89 | *
90 | * @param cols Desired number of columns. Set it to -1 to be automatically
91 | * computed.
92 | */
93 | public void setColumns(int cols) {
94 | this.cols = cols;
95 | }
96 |
97 | private int getOtherDimension(int size, int dim) {
98 | return (int) Math.ceil((double) size / dim);
99 | }
100 |
101 | /**
102 | * Update the capacity of this layout. This manager creates a grid, as much
103 | * square as possible, if automatic layout is wanted.
104 | *
105 | * @param page The page with the elements we would like to position
106 | * @param buffer Where to send commands, just after the "set multiplot"
107 | * part.
108 | */
109 | @Override
110 | public void setDefinition(Page page, StringBuilder buffer) {
111 | int size = page.size();
112 |
113 | if (size <= 0)
114 | return;
115 |
116 | int drawcols, drawrows;
117 | if (cols > 0 && rows > 0) {
118 | drawcols = cols;
119 | drawrows = rows;
120 | } else if (cols > 0) {
121 | drawcols = cols;
122 | drawrows = getOtherDimension(size, drawcols);
123 | } else if (rows > 0) {
124 | drawrows = rows;
125 | drawcols = getOtherDimension(size, drawrows);
126 | } else {
127 | drawrows = (int) (Math.floor(Math.sqrt(size)));
128 | drawcols = getOtherDimension(size, drawrows);
129 | }
130 |
131 | buffer.append(" layout ");
132 | buffer.append(drawrows).append(',').append(drawcols);
133 |
134 | if (drawfirst == ROWSFIRST)
135 | buffer.append(" rowsfirst");
136 | else
137 | buffer.append(" columnsfirst");
138 |
139 | if (orientation == DOWNWARDS)
140 | buffer.append(" downwards");
141 | else
142 | buffer.append(" upwards");
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/layout/GraphLayout.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | */
16 | package com.panayotis.gnuplot.layout;
17 |
18 | import com.panayotis.gnuplot.plot.Page;
19 |
20 | /**
21 | * This Object is used to define how graphs will be positioned on the whole page
22 | *
23 | * @author teras
24 | */
25 | public interface GraphLayout {
26 |
27 | /**
28 | * Sets the required definitions in the "set multiplot" part of gnuplot
29 | * commands. It can be also used to set various parameters, such as X/Y
30 | * position or dimension.
31 | *
32 | * @param page The Page we are referring to
33 | * @param buffer Where to send commands, just after the "set multiplot"
34 | * part. It might not be used.
35 | */
36 | public abstract void setDefinition(Page page, StringBuilder buffer);
37 | }
38 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/layout/GridGraphLayout.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | * and open the template in the editor.
16 | */
17 | package com.panayotis.gnuplot.layout;
18 |
19 | import com.panayotis.gnuplot.plot.Page;
20 | import java.io.Serializable;
21 |
22 | /**
23 | * Align graphs evenly on the page, in a grid layout.
24 | *
25 | * If you manually set metrics and use this, these metrics will be lost. Do not
26 | * use this layout, use AutoGraphLayout instead.
27 | *
28 | * @author teras
29 | * @deprecated
30 | */
31 | public class GridGraphLayout implements GraphLayout, Serializable {
32 |
33 | /**
34 | * Where the first graph will be put
35 | */
36 | public enum Start {
37 |
38 | UPLEFT, UPRIGHT, DOWNLEFT, DOWNRIGHT;
39 | }
40 | /**
41 | * Orientation of the graph layout
42 | */
43 | public static final boolean HORIZONTAL = true, VERTICAL = false;
44 | private Start start;
45 | private boolean orientation;
46 |
47 | /**
48 | * Create a new grid layout
49 | */
50 | public GridGraphLayout() {
51 | start = Start.UPLEFT;
52 | orientation = HORIZONTAL;
53 | }
54 |
55 | /**
56 | * Set where the first graph will be put
57 | *
58 | * @param start Position of the first graph
59 | */
60 | public void setStartPosition(Start start) {
61 | this.start = start;
62 | }
63 |
64 | /**
65 | * Sey the orientation of the graphs, as being put
66 | *
67 | * @param orientation Selected orientation
68 | */
69 | public void setOrientation(boolean orientation) {
70 | this.orientation = orientation;
71 | }
72 |
73 | /**
74 | * Update the capacity of this layout. This manager tries to create grid, as
75 | * much square as possible
76 | *
77 | * @param page The page with the elements we would like to position
78 | * @param buffer Where to send commands - not used.
79 | */
80 | @Override
81 | public void setDefinition(Page page, StringBuilder buffer) {
82 |
83 | int size = page.size();
84 |
85 | if (size <= 0)
86 | return;
87 |
88 | int width, height;
89 | height = (int) (Math.floor(Math.sqrt(size)));
90 | width = (int) Math.ceil((double) size / height);
91 |
92 | float dx = 1f / width;
93 | float dy = 1f / height;
94 | int col, lin;
95 | for (int index = 0; index < size; index++) {
96 | if (orientation) {
97 | col = index % width;
98 | lin = index / width;
99 | } else {
100 | lin = index % height;
101 | col = index / height;
102 | }
103 |
104 | if (start == Start.UPRIGHT || start == Start.DOWNRIGHT)
105 | col = width - col - 1;
106 | if (start == Start.UPLEFT || start == Start.UPRIGHT) // Positioning (0,0) in GNUPlot is in lower left corner
107 | lin = height - lin - 1;
108 |
109 | page.get(index).setMetrics(dx * col, dy * lin, dx, dy);
110 | }
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/layout/LayoutMetrics.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | */
16 | package com.panayotis.gnuplot.layout;
17 |
18 | import java.io.Serializable;
19 |
20 | /**
21 | * Container of the metrics for a specific graph
22 | *
23 | * @author teras
24 | */
25 | public class LayoutMetrics implements Serializable {
26 |
27 | private final static float MINSIZE = 0.001f;
28 |
29 | private float x, y, width, height;
30 |
31 | /**
32 | * Set default position, covering the whole screen
33 | */
34 | public LayoutMetrics() {
35 | this(0, 0, 1, 1);
36 | }
37 |
38 | /**
39 | * Set a specific position, in the area of 0,0-1,1 and with a minimum size
40 | *
41 | * @param x horizontal position
42 | * @param y vertical position
43 | * @param width width
44 | * @param height height
45 | */
46 | public LayoutMetrics(float x, float y, float width, float height) {
47 | if (width < 0)
48 | width = MINSIZE;
49 | if (height < 0)
50 | height = MINSIZE;
51 | if (width > 1)
52 | width = 1;
53 | if (height > 1)
54 | height = 1;
55 |
56 | if (x < 0)
57 | x = 0;
58 | if (y < 0)
59 | y = 0;
60 | if (x > 1)
61 | x = 1;
62 | if (y > 1)
63 | y = 1;
64 |
65 | float x2 = x + width;
66 | float y2 = y + height;
67 | if (x2 > 1) {
68 | x = 1 - MINSIZE;
69 | width = MINSIZE;
70 | }
71 | if (y2 > 1) {
72 | y = 1 - MINSIZE;
73 | height = MINSIZE;
74 | }
75 |
76 | this.x = x;
77 | this.y = y;
78 | this.width = width;
79 | this.height = height;
80 | }
81 |
82 | /**
83 | * Get horizontal position
84 | *
85 | * @return horizontal position
86 | */
87 | public float getX() {
88 | return x;
89 | }
90 |
91 | /**
92 | * Get vertical position
93 | *
94 | * @return vertical position
95 | */
96 | public float getY() {
97 | return y;
98 | }
99 |
100 | /**
101 | * Get width
102 | *
103 | * @return width
104 | */
105 | public float getWidth() {
106 | return width;
107 | }
108 |
109 | /**
110 | * Get height
111 | *
112 | * @return height
113 | */
114 | public float getHeight() {
115 | return height;
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/layout/ManualGraphLayout.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | */
16 | package com.panayotis.gnuplot.layout;
17 |
18 | import com.panayotis.gnuplot.plot.Page;
19 | import java.io.Serializable;
20 |
21 | /**
22 | * Position graphs in absolute coordinates. This is actually a dummy layout - no
23 | * layout information is used.
24 | *
25 | * @author teras
26 | */
27 | public class ManualGraphLayout implements GraphLayout, Serializable {
28 |
29 | /**
30 | * This is a dummy layout manager, which actually does nothing
31 | *
32 | * @param page The Page which layout we want to calculate
33 | * @param buffer The definition part of the multiplot layout
34 | */
35 | @Override
36 | public void setDefinition(Page page, StringBuilder buffer) {
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/layout/StripeLayout.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | */
16 | package com.panayotis.gnuplot.layout;
17 |
18 | /**
19 | * Align graphs evenly on the page This Layout is based on AutoGraphLayout
20 | */
21 | public class StripeLayout extends AutoGraphLayout {
22 |
23 | /**
24 | * Information if rows or columns are added automatically
25 | */
26 | public static final boolean EXPANDROWS = true, EXPANDCOLUMNS = false;
27 |
28 | /**
29 | * Create a new Strip layout. Default behavior is EXPANDROWS.
30 | */
31 | @SuppressWarnings("OverridableMethodCallInConstructor")
32 | public StripeLayout() {
33 | setType(EXPANDROWS);
34 | }
35 |
36 | /**
37 | * Set the default behavior
38 | *
39 | * @param type Whether EXPANDROWS or EXPANDCOLUMNS is desired.
40 | * @see #EXPANDROWS #EXPANDCOLUMNS
41 | */
42 | public void setType(boolean type) {
43 | if (type == EXPANDROWS) {
44 | super.setRows(-1);
45 | super.setColumns(1);
46 | } else {
47 | super.setRows(1);
48 | super.setColumns(-1);
49 | }
50 | }
51 |
52 | /**
53 | * Set behavior, depending on the number of rows. It always creates stripes
54 | * and it might change to EXPANDCOLUMNS if rows are less than 2.
55 | *
56 | * @param rows Number of desired rows
57 | */
58 | @Override
59 | public void setRows(int rows) {
60 | if (rows > 1)
61 | setType(EXPANDROWS);
62 | else
63 | setType(EXPANDCOLUMNS);
64 | }
65 |
66 | /**
67 | * Set behaviour, depending on the number of columns. It always creates
68 | * stripes and it might change to EXPANDROWS if columns are less than 2.
69 | *
70 | * @param cols Number of desired columns
71 | */
72 | @Override
73 | public void setColumns(int cols) {
74 | if (cols > 1)
75 | setType(EXPANDCOLUMNS);
76 | else
77 | setType(EXPANDROWS);
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/plot/AbstractPlot.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on 12 Οκτώβριος 2007, 4:07 μμ
17 | */
18 | package com.panayotis.gnuplot.plot;
19 |
20 | import com.panayotis.gnuplot.PropertiesHolder;
21 | import com.panayotis.gnuplot.style.PlotStyle;
22 | import com.panayotis.gnuplot.style.Smooth;
23 |
24 | /**
25 | * This is the base class of all plot objects. It is used to define the basic
26 | * characteristics of plot handling, such as definition and styles
27 | *
28 | * @author teras
29 | */
30 | public abstract class AbstractPlot extends PropertiesHolder implements Plot {
31 |
32 | private static int last_id = 0;
33 | private String definition = "";
34 | private PlotStyle style = null;
35 | private Smooth smooth = null;
36 |
37 | /**
38 | * General purpose constructor of a generic plot object
39 | */
40 | public AbstractPlot() {
41 | super(" ", "");
42 | setTitle("Datafile " + (++last_id));
43 | }
44 |
45 | /**
46 | * Set the plot definition for this plot object. It is the part that the
47 | * plot command parses.
48 | *
49 | * @param definition The string representing the plot definition.
50 | */
51 | protected void setDefinition(String definition) {
52 | this.definition = definition;
53 | }
54 |
55 | /**
56 | * Retrieve the definition of this Plot object. If styles or smoothing
57 | * engines were used, the appropriate arguments are appended
58 | *
59 | * @param buf The buffer to store the data set.
60 | */
61 | @Override
62 | public void retrieveDefinition(StringBuilder buf) {
63 | buf.append(definition);
64 | appendProperties(buf);
65 | if (smooth != null)
66 | buf.append(smooth.toString());
67 | if (style != null)
68 | style.appendProperties(buf);
69 | }
70 |
71 | /**
72 | * Set how this plot will be presented. Provide information about colors and
73 | * sizes, in accordance with the selected terminal. Give "null" if you want
74 | * to turn off this feature.
75 | *
76 | * @param style The style to use
77 | */
78 | public void setPlotStyle(PlotStyle style) {
79 | this.style = style;
80 | }
81 |
82 | /**
83 | * Retrieve the style of this Plot command
84 | *
85 | * @return The style being used
86 | */
87 | public PlotStyle getPlotStyle() {
88 | if (style == null)
89 | style = new PlotStyle();
90 | return style;
91 | }
92 |
93 | /**
94 | * Define if this plot should be smoothed.
95 | *
96 | * @param smooth The smooth definition. Give "null" if you want to turn off
97 | * this feature.
98 | */
99 | public void setSmooth(Smooth smooth) {
100 | this.smooth = smooth;
101 | }
102 |
103 | /**
104 | * A convenient method to set the title of this plot
105 | *
106 | * @param title The title to use for this plot
107 | */
108 | public final void setTitle(String title) {
109 | set("title", "\"" + title.replaceAll("[\"]", "\\\\\\\"") + "\"");
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/plot/Axis.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 14, 2007, 10:59 PM
17 | */
18 | package com.panayotis.gnuplot.plot;
19 |
20 | import com.panayotis.gnuplot.*;
21 |
22 | /**
23 | * This class represents the various axes of the plot. It is used to set various
24 | * axis-related parameters
25 | *
26 | * @author teras
27 | */
28 | public class Axis extends PropertiesHolder {
29 |
30 | private final String name;
31 |
32 | /**
33 | * Creates a new instance of Axis.
34 | *
35 | * @param name The name of the axis
36 | */
37 | Axis(String name) {
38 | this.name = name;
39 | }
40 |
41 | /**
42 | * Get the name of this axis as a String.
43 | *
44 | * @return The name of the axis. Usually it is "x" or "y".
45 | */
46 | public String getName() {
47 | return name;
48 | }
49 |
50 | /**
51 | * Set whether this axis is in logarithmic scale or not
52 | *
53 | * @param log Set, if this axis is in logarithmic scale
54 | */
55 | public void setLogScale(boolean log) {
56 | if (log)
57 | set("logscale", getName());
58 | else
59 | unset("logscale");
60 | }
61 |
62 | /**
63 | * Set the label of this axis.
64 | *
65 | * @param label The label of this axis
66 | * @see #setLabel(String,String,int)
67 | */
68 | public void setLabel(String label) {
69 | setLabel(label, null, -1);
70 | }
71 |
72 | /**
73 | * Set the label and the font of the current axis
74 | *
75 | * @param label The label of this axis
76 | * @param font Font name
77 | * @param size Font size
78 | */
79 | public void setLabel(String label, String font, int size) {
80 | String fontname = "";
81 | if (font != null)
82 | fontname = " font '" + font + ((size > 1) ? "," + size : "") + "'";
83 | set(getName() + "label", "'" + label + "'" + fontname);
84 | }
85 |
86 | /**
87 | * Define the area to plot.
27 | * For single plots, better have a look at Plot objects and GNUPlot.addPlot()
28 | * command
29 | *
30 | * @author teras
31 | */
32 | public class Graph extends ArrayList
28 | * Note that in many operating systems this terminal is not really useful, or
29 | * even visible. Please use a specific terminal yourself.
30 | *
31 | * @author teras
32 | */
33 | public class DefaultTerminal implements GNUPlotTerminal, Serializable {
34 |
35 | /**
36 | * This Terminal has no type.
37 | *
38 | * @return Always returns "".
39 | */
40 | @Override
41 | public String getType() {
42 | return "";
43 | }
44 |
45 | /**
46 | * No output is defined for this terminal
47 | *
48 | * @return Always returns "".
49 | */
50 | @Override
51 | public String getOutputFile() {
52 | return "";
53 | }
54 |
55 | /**
56 | * No processing is performed. The plot is displayed in the default output.
57 | *
58 | * @param stdout The output of GNUPlot. Not processed.
59 | */
60 | @Override
61 | public String processOutput(InputStream stdout) {
62 | return null;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/terminal/ExpandableTerminal.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 21, 2007, 6:58 PM
17 | */
18 | package com.panayotis.gnuplot.terminal;
19 |
20 | import com.panayotis.gnuplot.PropertiesHolder;
21 | import java.io.BufferedInputStream;
22 | import java.io.IOException;
23 | import java.io.InputStream;
24 |
25 | /**
26 | * This is the base class of all special terminals found in JavaPlot. It
27 | * provides support for terminal types and consumes unwanted gnuplot output.
28 | *
29 | * @author teras
30 | */
31 | public abstract class ExpandableTerminal extends PropertiesHolder implements GNUPlotTerminal {
32 |
33 | private final String type;
34 |
35 | /**
36 | * Create a new Terminal with a given type
37 | *
38 | * @param type The terminal to use
39 | */
40 | public ExpandableTerminal(String type) {
41 | super(" ", "");
42 | if (type == null)
43 | type = "unknown";
44 | this.type = type;
45 | }
46 |
47 | /**
48 | * Get the type of this terminal
49 | *
50 | * @return String representation of this terminal type
51 | */
52 | @Override
53 | public String getType() {
54 | StringBuilder buf = new StringBuilder();
55 | buf.append(type);
56 | appendProperties(buf);
57 | return buf.toString();
58 | }
59 |
60 | /**
61 | * This method only consumes gnuplot stdout input stream. It is performed to
62 | * prevent a possible thread lockup.
63 | *
64 | * @param stdout The output of GNUPlot. It will be consumed.
65 | */
66 | @SuppressWarnings("empty-statement")
67 | @Override
68 | public String processOutput(InputStream stdout) {
69 | byte[] buffer = new byte[1000];
70 | BufferedInputStream in = new BufferedInputStream(stdout);
71 | try {
72 | while (in.read(buffer) >= 0); // consume stream
73 | in.close();
74 | } catch (IOException ex) {
75 | return "I/O error while processing gnuplot output: " + ex.getMessage();
76 | }
77 | return null;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/terminal/FileTerminal.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 17, 2007, 2:51 AM
17 | */
18 | package com.panayotis.gnuplot.terminal;
19 |
20 | /**
21 | * This Terminal supports file operations. The results of the gnuplot commands
22 | * can be stored in a file, if desired.
23 | *
24 | * @author teras
25 | */
26 | public class FileTerminal extends ExpandableTerminal {
27 |
28 | private String filename;
29 |
30 | /**
31 | * Creates a new instance of FileTerminal and output to stadard out
32 | *
33 | * @param type The terminal type
34 | */
35 | public FileTerminal(String type) {
36 | this(type, "");
37 | }
38 |
39 | /**
40 | * Creates a new instance of FileTerminal and output to a specific file
41 | *
42 | * @param type The terminal type
43 | * @param filename e filename to use as an output for this terminal
44 | * @see #getOutputFile()
45 | */
46 | public FileTerminal(String type, String filename) {
47 | super(type);
48 | if (filename == null)
49 | filename = "";
50 | this.filename = filename;
51 | }
52 |
53 | /**
54 | * Retrieve the filename to use as an output for this terminal. If the
55 | * filename empty, then the output will be dumped to standard output, and
56 | * retrieved by JavaPlot
57 | *
58 | * @return If this parameter is not empty, the output filename
59 | */
60 | @Override
61 | public String getOutputFile() {
62 | return filename;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/terminal/GNUPlotTerminal.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 14, 2007, 2:36 PM
17 | */
18 | package com.panayotis.gnuplot.terminal;
19 |
20 | import java.io.InputStream;
21 |
22 | /**
23 | * This interface represents any GNUPlot terminal. "Terminal" by the definition
24 | * of GNUPlot is any device which will present the drawn plots.
25 | *
26 | * @author teras
27 | */
28 | public interface GNUPlotTerminal {
29 |
30 | /**
31 | * Get the terminal type / terminal name. A list of available terminal names
32 | * can be found through gnuplot if you issue the command "set term".
33 | *
34 | * @return the terminal type
35 | */
36 | public String getType();
37 |
38 | /**
39 | * Get the output filename. Use "" if not output file is desired or needed.
40 | *
41 | * @return The output filename
42 | */
43 | public String getOutputFile();
44 |
45 | /**
46 | * This method is executed bu GNUPlot, when the plot has been performed. It
47 | * actually transfers GNUPlot output to this method, if parsing is required.
48 | *
49 | * Note that if no output filename is given, then it is necessary to
50 | * "consume" this stream, or else a thread lockup might happen.
51 | *
52 | * @param stdout The output stream of GNUPlot. Note that since it is
53 | * required to read from this stream, it is given as InputStream.
54 | * @return The definition of the error, if something went wrong. If
55 | * everything is OK, it is necessary to return null.
56 | */
57 | public String processOutput(InputStream stdout);
58 | }
59 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/terminal/ImageTerminal.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 16, 2007, 1:34 AM
17 | */
18 | package com.panayotis.gnuplot.terminal;
19 |
20 | import java.awt.image.BufferedImage;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 | import javax.imageio.ImageIO;
24 |
25 | /**
26 | * This terminal is able to process gnuplot output as an image. The image
27 | * produced can be used by any Java object which is able to handle BufferedImage
28 | *
29 | * @author teras
30 | */
31 | public class ImageTerminal extends FileTerminal {
32 |
33 | private BufferedImage img;
34 |
35 | /**
36 | * Create a new image terminal, and use PNG as it's backend
37 | */
38 | public ImageTerminal() {
39 | super("png");
40 | }
41 |
42 | /**
43 | * Read the produced image from gnuplot standard output
44 | *
45 | * @param stdout The gnuplot output stream
46 | * @return The error definition, if any
47 | */
48 | @Override
49 | public String processOutput(InputStream stdout) {
50 | try {
51 | img = ImageIO.read(stdout);
52 | } catch (IOException ex) {
53 | return "Unable to create PNG image: " + ex.getMessage();
54 | }
55 | if (img == null)
56 | return "Unable to create image from the gnuplot output. Null image created.";
57 | return null;
58 | }
59 |
60 | /**
61 | * Get a handler of the produced image by this terminal
62 | *
63 | * @return the plot image
64 | */
65 | public BufferedImage getImage() {
66 | return img;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/terminal/PostscriptTerminal.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 16, 2007, 1:34 AM
17 | */
18 | package com.panayotis.gnuplot.terminal;
19 |
20 | /**
21 | * This terminal uses postscript as it's backend
22 | *
23 | * @author teras
24 | */
25 | public class PostscriptTerminal extends TextFileTerminal {
26 |
27 | /**
28 | * Create a new instance of PostscriptTerminal. It is recommended to use
29 | * PostscriptTerminal(String filename) instead, since this constructor does
30 | * not produce any output file.
31 | */
32 | public PostscriptTerminal() {
33 | this("");
34 | }
35 |
36 | /**
37 | * Create a new Postscript terminal and save output to the specified file
38 | *
39 | * @param filename The filename of the output postscript file
40 | */
41 | @SuppressWarnings("OverridableMethodCallInConstructor")
42 | public PostscriptTerminal(String filename) {
43 | super("postscript", filename);
44 | setColor(true);
45 | setEPS(true);
46 | }
47 |
48 | /**
49 | * Select if the output will be in EPS format or not
50 | *
51 | * @param eps If EPS mode will be used
52 | */
53 | public void setEPS(boolean eps) {
54 | if (eps)
55 | set("eps");
56 | else
57 | unset("eps");
58 | }
59 |
60 | /**
61 | * Select if the output will be color or not (monochrome)
62 | *
63 | * @param color If the ouput will be in color
64 | */
65 | public void setColor(boolean color) {
66 | if (color) {
67 | set("color");
68 | unset("monochrome");
69 | } else {
70 | set("monochrome");
71 | unset("color");
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/terminal/TextFileTerminal.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 23, 2007, 10:46 AM
17 | */
18 | package com.panayotis.gnuplot.terminal;
19 |
20 | import java.io.BufferedReader;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 | import java.io.InputStreamReader;
24 |
25 | /**
26 | * Base class of all terminals with output in text format.
27 | *
28 | * @author teras
29 | */
30 | public class TextFileTerminal extends FileTerminal {
31 |
32 | /**
33 | *
34 | */
35 | protected String output = "";
36 |
37 | /**
38 | * Creates a new instance of TextFileTerminal. The output will be parsed by
39 | * JavaPlot and stored in a String, since it is expected to be a text and
40 | * not binary data.
41 | *
42 | * @param type the terminal type
43 | */
44 | public TextFileTerminal(String type) {
45 | this(type, "");
46 | }
47 |
48 | /**
49 | * Creates a new instance of TextFileTerminal and output to a specific file
50 | *
51 | * @param type the terminal type
52 | * @param filename the file to save output to
53 | */
54 | public TextFileTerminal(String type, String filename) {
55 | super(type, filename);
56 | }
57 |
58 | /**
59 | * Process output of this terminal. Since this is a text terminal, the
60 | * output will be stored in a String
61 | *
62 | * @param stdout The gnuplot output stream
63 | * @return Return the error as a String, if an error occured.
64 | */
65 | @Override
66 | public String processOutput(InputStream stdout) {
67 | StringBuilder out = new StringBuilder();
68 | BufferedReader in = new BufferedReader(new InputStreamReader(stdout));
69 | String line;
70 | try {
71 | while ((line = in.readLine()) != null)
72 | out.append(line);
73 | in.close();
74 | } catch (IOException ex) {
75 | return "I/O error while processing gnuplot output: " + ex.getMessage();
76 | }
77 | output = out.toString();
78 | return null;
79 | }
80 |
81 | /**
82 | * Retrieve the String with the output of the last gnuplot command
83 | *
84 | * @return The String with gnuplot output
85 | */
86 | public String getTextOutput() {
87 | return output;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/utils/Debug.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 17, 2007, 3:01 PM
17 | */
18 | package com.panayotis.gnuplot.utils;
19 |
20 | import java.io.BufferedWriter;
21 | import java.io.IOException;
22 | import java.io.OutputStreamWriter;
23 | import java.io.Writer;
24 |
25 | /**
26 | * This object is responsible to display debug and/or general information on any
27 | * given Writer (by default to System.err)
28 | * .
29 | * It is possible to set another output writer, in order to redirect the errors
30 | * whenever the programmer wants.
31 | *
32 | * @author teras
33 | */
34 | public class Debug {
35 |
36 | /**
37 | * Absolutely no debug information is presented
38 | */
39 | public final static int QUIET = 0;
40 | /**
41 | * Only critical information is presented
42 | */
43 | public final static int CRITICAL = 10;
44 | /**
45 | * Only errors and critical information is presented
46 | */
47 | public final static int ERROR = 20;
48 | /**
49 | * Warnings, as well as errors and critical information is presented
50 | */
51 | public final static int WARNING = 30;
52 | /**
53 | * All messages except verbose messages are presented
54 | */
55 | public final static int INFO = 40;
56 | /**
57 | * All messages are presented
58 | */
59 | public final static int VERBOSE = 50;
60 | private static final String NL = System.getProperty("line.separator");
61 | private int level;
62 | private Writer out;
63 |
64 | /**
65 | * Creates a new instance of Debug
66 | */
67 | public Debug() {
68 | setLevel(WARNING);
69 | out = new BufferedWriter(new OutputStreamWriter(System.err));
70 | }
71 |
72 | /**
73 | * Set the level of verbosity.
74 | *
75 | * @param level Only messages at least as critical as this level are
76 | * presented.
77 | */
78 | public final void setLevel(int level) {
79 | this.level = level;
80 | }
81 |
82 | /**
83 | * Present a message on the Debug stream
84 | *
85 | * @param message Message to display. Automatically adds a newline at the
86 | * end of the string.
87 | * @param level Level of verbosity. If this level is les critical than the
88 | * desired level, the message is not displayed.
89 | */
90 | public void msg(String message, int level) {
91 | if (message == null || message.equals(""))
92 | return;
93 | if (level > QUIET && level <= this.level)
94 | try {
95 | out.write(message);
96 | if (!message.endsWith(NL))
97 | out.write(NL);
98 | out.flush();
99 | } catch (IOException ex) {
100 | System.out.println(ex.getMessage());
101 | }
102 | }
103 |
104 | /**
105 | * Set the output writer. By default this is the System.err stream.
106 | *
107 | * @param out The Debug stream writer.
108 | */
109 | public void setWriter(Writer out) {
110 | if (out == null)
111 | throw new NullPointerException("Debug: set of null output device.");
112 | this.out = out;
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/utils/FileUtils.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | */
16 | package com.panayotis.gnuplot.utils;
17 |
18 | import java.io.BufferedWriter;
19 | import java.io.File;
20 | import java.io.FileWriter;
21 | import java.io.IOException;
22 | import java.util.StringTokenizer;
23 |
24 | /**
25 | *
26 | * @author teras
27 | */
28 | public class FileUtils {
29 |
30 | /**
31 | * This method browses current path to search for a file. Typically this
32 | * should be an executable, but it is impossible under Java to check if this
33 | * file has the execution bit on. Apart from the user defined $PATH
34 | * variable, common bin-directory places are searched.
35 | *
36 | * @return The path of the specified program. If it is not found, "gnuplot"
37 | * is returned.
38 | */
39 | public static String findPathExec() {
40 | /* Check for default locations */
41 | for (String path : new String[]{
42 | "/bin/gnuplot", "/usr/bin/gnuplot", "/usr/local/bin/gnuplot",
43 | "/sbin/gnuplot", "/usr/sbin/gnuplot", "/usr/local/sbin/gnuplot",
44 | "/opt/bin/gnuplot", "/opt/local/bin/gnuplot",
45 | "/opt/sbin/gnuplot", "/opt/local/sbin/gnuplot",
46 | "/sw/bin/gnuplot",
47 | "C:\\Program Files (x86)\\gnuplot\\bin\\wgnuplot.exe",
48 | "C:\\Program Files\\gnuplot\\bin\\wgnuplot.exe",
49 | "c:\\cygwin\\bin\\gnuplot.exe"})
50 | if (new File(path).isFile())
51 | return path;
52 |
53 | /* Check for PATH-based locations */
54 | StringTokenizer st = new StringTokenizer(System.getenv("PATH"), File.pathSeparator);
55 | String dir, file;
56 | while (st.hasMoreTokens()) {
57 | dir = st.nextToken();
58 | file = dir + File.separator + "gnuplot";
59 | if (new File(file).isFile())
60 | return file;
61 | file = dir + File.separator + "wgnuplot.exe";
62 | if (new File(file).isFile())
63 | return file;
64 | file = dir + File.separator + "gnuplot.exe";
65 | if (new File(file).isFile())
66 | return file;
67 | }
68 | return "gnuplot";
69 | }
70 |
71 | public static String createTempFile(String contents) throws IOException {
72 | File file = File.createTempFile("gnuplot_", ".dat");
73 | BufferedWriter out = new BufferedWriter(new FileWriter(file));
74 | out.append(contents);
75 | out.close();
76 | return file.getAbsolutePath();
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/base/src/test/java/com/panayotis/gnuplot/demo/BaseDemo.java:
--------------------------------------------------------------------------------
1 | package com.panayotis.gnuplot.demo;
2 |
3 | /* Copyright (c) 2007-2014 by panayotis.com
4 | *
5 | * JavaPlot is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, version 2.
8 | *
9 | * JavaPlot is free 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 | *
14 | * You should have received a copy of the GNU Lesser General Public License
15 | * along with CrossMobile; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 | */
18 | import com.panayotis.gnuplot.GNUPlotParameters;
19 | import com.panayotis.gnuplot.JavaPlot;
20 | import com.panayotis.gnuplot.dataset.FileDataSet;
21 | import com.panayotis.gnuplot.layout.StripeLayout;
22 | import com.panayotis.gnuplot.plot.AbstractPlot;
23 | import com.panayotis.gnuplot.plot.DataSetPlot;
24 | import com.panayotis.gnuplot.style.NamedPlotColor;
25 | import com.panayotis.gnuplot.style.PlotStyle;
26 | import com.panayotis.gnuplot.style.Style;
27 | import com.panayotis.gnuplot.swing.JPlot;
28 | import com.panayotis.gnuplot.terminal.PostscriptTerminal;
29 | import com.panayotis.gnuplot.utils.Debug;
30 | import java.io.File;
31 | import java.io.FileInputStream;
32 | import java.io.FileOutputStream;
33 | import java.io.IOException;
34 | import java.io.ObjectInputStream;
35 | import java.io.ObjectOutputStream;
36 | import javax.swing.JFrame;
37 |
38 | /**
39 | * This Object is used to demonstrate JavaPlot library
40 | *
41 | * @author teras
42 | */
43 | public class BaseDemo {
44 |
45 | /**
46 | * @param args the command line arguments. First argument is the path of
47 | * gnuplot application
48 | */
49 | public static void main(String[] args) {
50 | String path = null;
51 | if (args.length > 0)
52 | path = args[0];
53 |
54 | //simple();
55 | //simple3D();
56 | // defaultTerminal(path);
57 | // EPSTerminal(path);
58 | // SVGTerminal(path);
59 | JPlotTerminal(path);
60 | //serialization(defaultTerminal(path));
61 | //file();
62 |
63 | }
64 |
65 | /* This is a very simple plot to demonstrate JavaPlot graphs */
66 | private static void simple() {
67 | JavaPlot p = new JavaPlot();
68 | p.addPlot("sin(x)");
69 | p.plot();
70 | }
71 |
72 | /* This is a very simple plot to demonstrate JavaPlot 3d graphs */
73 | private static void simple3D() {
74 | JavaPlot p = new JavaPlot(true);
75 | p.addPlot("sin(x)*y");
76 | p.plot();
77 | }
78 |
79 | /* This demo code uses default terminal. Use it as reference for other javaplot arguments */
80 | private static JavaPlot defaultTerminal(String gnuplotpath) {
81 | JavaPlot p = new JavaPlot(gnuplotpath);
82 | JavaPlot.getDebugger().setLevel(Debug.VERBOSE);
83 |
84 | p.setTitle("Default \"Terminal Title\"");
85 | p.getAxis("x").setLabel("X axis", "Arial", 20);
86 | p.getAxis("y").setLabel("Y axis");
87 |
88 | p.getAxis("x").setBoundaries(-30, 20);
89 | p.setKey(JavaPlot.Key.TOP_RIGHT);
90 |
91 | double[][] plot = {{1, 1.1}, {2, 2.2}, {3, 3.3}, {4, 4.3}};
92 | DataSetPlot s = new DataSetPlot(plot);
93 | p.addPlot(s);
94 | p.addPlot("besj0(x)*0.12e1");
95 | PlotStyle stl = ((AbstractPlot) p.getPlots().get(1)).getPlotStyle();
96 | stl.setStyle(Style.POINTS);
97 | stl.setLineType(NamedPlotColor.GOLDENROD);
98 | stl.setPointType(5);
99 | stl.setPointSize(8);
100 | p.addPlot("sin(x)");
101 |
102 | p.newGraph();
103 | p.addPlot("sin(x)");
104 |
105 | p.newGraph3D();
106 | double[][] plot3d = {{1, 1.1, 3}, {2, 2.2, 3}, {3, 3.3, 3.4}, {4, 4.3, 5}};
107 | p.addPlot(plot3d);
108 |
109 | p.newGraph3D();
110 | p.addPlot("sin(x)*sin(y)");
111 |
112 | p.setMultiTitle("Global test title");
113 | StripeLayout lo = new StripeLayout();
114 | lo.setColumns(9999);
115 | p.getPage().setLayout(lo);
116 | p.plot();
117 |
118 | return p;
119 | }
120 |
121 | /* This demo code creates a EPS file on home directory */
122 | private static JavaPlot EPSTerminal(String gnuplotpath) {
123 | JavaPlot p = new JavaPlot();
124 |
125 | PostscriptTerminal epsf = new PostscriptTerminal(System.getProperty("user.home")
126 | + System.getProperty("file.separator") + "output.eps");
127 | epsf.setColor(true);
128 | p.setTerminal(epsf);
129 |
130 | p.setTitle("Postscript Terminal Title");
131 | p.addPlot("sin (x)");
132 | p.addPlot("sin(x)*cos(x)");
133 | p.newGraph();
134 | p.addPlot("cos(x)");
135 | p.setTitle("Trigonometric functions -1");
136 | p.setMultiTitle("Trigonometric functions");
137 | p.plot();
138 | return p;
139 | }
140 |
141 | /* This demo code displays plot on screen using image terminal */
142 | private static JavaPlot JPlotTerminal(String gnuplotpath) {
143 | JPlot plot = new JPlot();
144 | plot.getJavaPlot().addPlot("sqrt(x)/x");
145 | plot.getJavaPlot().addPlot("x*sin(x)");
146 | plot.plot();
147 |
148 | JFrame f = new JFrame();
149 | f.getContentPane().add(plot);
150 | f.pack();
151 | f.setLocationRelativeTo(null);
152 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
153 | f.setVisible(true);
154 |
155 | return plot.getJavaPlot();
156 | }
157 |
158 | private static void serialization(JavaPlot p) {
159 | ObjectOutputStream out = null;
160 | ObjectInputStream in = null;
161 | try {
162 | out = new ObjectOutputStream(new FileOutputStream("koko.lala"));
163 | out.writeObject(p.getParameters());
164 |
165 | in = new ObjectInputStream(new FileInputStream("koko.lala"));
166 | JavaPlot q = new JavaPlot((GNUPlotParameters) in.readObject());
167 | q.plot();
168 | } catch (ClassNotFoundException ex) {
169 | ex.printStackTrace();
170 | } catch (IOException ex) {
171 | ex.printStackTrace();
172 | } finally {
173 | try {
174 | out.close();
175 | } catch (IOException ex) {
176 | ex.printStackTrace();
177 | }
178 | }
179 | }
180 |
181 |
182 | /* This is a simple plot to demonstrate file datasets */
183 | private static void file() {
184 | try {
185 | JavaPlot p = new JavaPlot();
186 | p.addPlot(new FileDataSet(new File("lala")));
187 | p.plot();
188 | } catch (IOException ex) {
189 | ex.printStackTrace();
190 | }
191 | }
192 | }
193 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin:'java'
2 | apply plugin:'application'
3 | defaultTasks 'run'
4 | mainClassName='test.Main'
5 |
6 | apply plugin: 'maven'
7 | apply plugin: 'signing'
8 |
9 | group = "com.panayotis"
10 | archivesBaseName = "javaplot"
11 | version = "0.5.0"
12 |
13 | sourceCompatibility = 1.6
14 | targetCompatibility = 1.6
15 |
16 | repositories {
17 | mavenCentral()
18 | }
19 |
20 | dependencies {
21 | compile 'com.kitfox.svg:svg-salamander:1.1.2'
22 | }
23 |
24 | task dist(dependsOn:'jar') << {
25 | exec () { executable 'ant'; workingDir 'demo'; args 'jar' }
26 | delete 'build', 'demo/build', 'one', "/tmp/JavaPlot-${version}", "JavaPlot-${version}.tar.bz2", "JavaPlot-${version}.tar.bz2"
27 | ant.copy(todir: "/tmp/JavaPlot-${version}", preservelastmodified: "true") {
28 | fileset(dir: "../JavaPlot", excludes:"**/.DS_Store .hg/** .gradle/** .nb-gradle/**")
29 | }
30 | exec () { executable 'tar'; workingDir '/tmp'; args "-jcvf", "JavaPlot-${version}.tar.bz2", "JavaPlot-${version}" }
31 | exec () { executable 'zip'; workingDir '/tmp'; args "-9", "-r", "JavaPlot-${version}.zip", "JavaPlot-${version}" }
32 | copy() { from "/tmp/JavaPlot-${version}.tar.bz2"; into "."}
33 | copy() { from "/tmp/JavaPlot-${version}.zip"; into="."}
34 | delete "/tmp/JavaPlot-${version}", "/tmp/JavaPlot-${version}.tar.bz2", "/tmp/JavaPlot-${version}.zip"
35 | }
36 |
37 | clean << {
38 | exec () {
39 | executable 'ant'
40 | workingDir 'demo'
41 | args 'clean'
42 | }
43 | delete "JavaPlot-${version}.tar.bz2", "JavaPlot-${version}.zip"
44 | }
45 |
46 | task javadocJar(type: Jar) {
47 | classifier = 'javadoc'
48 | from javadoc
49 | }
50 |
51 | task sourcesJar(type: Jar) {
52 | classifier = 'sources'
53 | from sourceSets.main.allSource
54 | }
55 |
56 | artifacts {
57 | archives javadocJar, sourcesJar
58 | }
59 |
60 | signing {
61 | sign configurations.archives
62 | }
63 |
64 | uploadArchives {
65 | repositories {
66 | mavenDeployer {
67 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
68 |
69 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
70 | authentication(userName: ossrhUsername, password: ossrhPassword)
71 | }
72 |
73 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
74 | authentication(userName: ossrhUsername, password: ossrhPassword)
75 | }
76 |
77 | pom.project {
78 | name 'JavaPlot'
79 | packaging 'jar'
80 | description 'A pure Java programming interface library for GNUPlot'
81 | url 'http://http://javaplot.panayotis.com/'
82 |
83 | scm {
84 | connection 'scm:hg:http://hg.code.sf.net/p/gnujavaplot/hg gnujavaplot-hg'
85 | developerConnection 'scm:hg:https://hg.code.sf.net/p/gnujavaplot/hg'
86 | url 'https://sourceforge.net/projects/gnujavaplot/'
87 | }
88 |
89 | licenses {
90 | license {
91 | name 'The GNU Lesser General Public License, version 3.0'
92 | url 'https://opensource.org/licenses/lgpl-3.0.html'
93 | }
94 | }
95 |
96 | developers {
97 | developer {
98 | id 'teras'
99 | name 'Panayotis Katsaloulis'
100 | email 'panayotis@panayotis.com'
101 | }
102 | }
103 | }
104 | }
105 | }
106 | }
107 |
108 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
317 | * This method is used for debugging purposes.
318 | *
319 | * @return The commands to send to the gnuplot executable
320 | */
321 | public String getCommands() {
322 | return exec.getCommands(param, term);
323 | }
324 |
325 | // public void splot() throws GNUPlotException {
326 | // exec.splot(param, term);
327 | // }
328 | /**
329 | * Set the desired path for gnuplot executable.
330 | *
331 | * @param path Filename of gnuplot executable
332 | * @throws java.io.IOException gnuplot is not found, or not valid
333 | */
334 | public void setGNUPlotPath(String path) throws IOException {
335 | exec.setGNUPlotPath(path);
336 | }
337 |
338 | /**
339 | * Retrieve the file path of gnuplot
340 | *
341 | * @return The gnuplot file path
342 | */
343 | public String getGNUPlotPath() {
344 | return exec.getGNUPlotPath();
345 | }
346 |
347 | /**
348 | * Set all terminals to be persistent. Thus, after executing plot command,
349 | * the graph window stays open and does not disappear automatically.
350 | *
351 | * @param ispersist whether the terminal window should be persistent
352 | */
353 | public void setPersist(boolean ispersist) {
354 | exec.setPersist(ispersist);
355 | }
356 |
357 | /**
358 | * Set gnuplot parameters to another set of parameters.
359 | *
360 | * @param parameters The new GNUPlot parameters.
361 | */
362 | public void setParameters(GNUPlotParameters parameters) {
363 | if (param == null)
364 | return;
365 | param = parameters;
366 | }
367 |
368 | /**
369 | * Ge the actual gnuplot parameters. This method is useful if the developer
370 | * wants to have access to lower level GNUPlotparameter methods.
371 | *
372 | * @return Object having all information on how to make the plot.
373 | */
374 | public GNUPlotParameters getParameters() {
375 | return param;
376 | }
377 |
378 | /**
379 | * Change gnuplot terminal. Use this method to make gnuplot draw to another
380 | * terminal than the default
381 | *
382 | * @param term The terminal to use
383 | */
384 | public void setTerminal(GNUPlotTerminal term) {
385 | if (term == null)
386 | return;
387 | this.term = term;
388 | }
389 |
390 | /**
391 | * Get the current used terminal
392 | *
393 | * @return The used terminal
394 | */
395 | public GNUPlotTerminal getTerminal() {
396 | return term;
397 | }
398 |
399 | /**
400 | * Get the specific GNUPlot Debug object
401 | *
402 | * @return The Debug object
403 | */
404 | public static Debug getDebugger() {
405 | return dbg;
406 | }
407 |
408 | /**
409 | * Execute gnuplot commands before any kind of initialization. This method
410 | * together with getPostInit() is useful to add basic commands to gnuplot
411 | * exetutable, if the library does not support the desired functionality
412 | *
413 | * @return Array of pre-init commands
414 | */
415 | public List
This
93 | * method is used for debugging purposes.
94 | *
95 | * @return The commands to send to the gnuplot executable
96 | */
97 | public String getCommands(GNUPlotParameters par, GNUPlotTerminal terminal) {
98 | // Could cache these commands..
99 | return par.getPlotCommands(terminal);
100 | }
101 |
102 | /**
103 | * Plot using specific parameters and selected terminal.
104 | *
105 | * @param par The parameters to use
106 | * @param terminal The terminal to use
107 | * @throws com.panayotis.gnuplot.GNUPlotException throw if something goes
108 | * wrong
109 | */
110 | void plot(GNUPlotParameters par, GNUPlotTerminal terminal) throws GNUPlotException {
111 | try {
112 | final GNUPlotTerminal term = terminal; // Use this thread-aware variable instead of "terminal"
113 | final String comms = getCommands(par, term); // Get the commands to send to gnuplot
114 | final Messages msg = new Messages(); // Where to store messages from output threads
115 |
116 | /*
117 | * Display plot commands to send to gnuplot
118 | */
119 | GNUPlot.getDebugger().msg("** Start of plot commands **", Debug.INFO);
120 | GNUPlot.getDebugger().msg(comms, Debug.INFO);
121 | GNUPlot.getDebugger().msg("** End of plot commands **", Debug.INFO);
122 |
123 | /*
124 | * It's time now to start the actual gnuplot application
125 | */
126 | String[] command;
127 | if (ispersist)
128 | command = persistcommand;
129 | else
130 | command = nopersist;
131 | command[0] = getGNUPlotPath();
132 | command[1] = FileUtils.createTempFile(comms);
133 |
134 | {
135 | String cmdStr = "";
136 | for (String cmd : command)
137 | cmdStr += cmd + " ";
138 | GNUPlot.getDebugger().msg("exec(" + cmdStr + ")", Debug.INFO);
139 | }
140 | final Process proc = Runtime.getRuntime().exec(command);
141 |
142 | /*
143 | * Windows buffers DEMAND asynchronus read & write
144 | */
145 |
146 | /*
147 | * Thread to process the STDERR of gnuplot
148 | */
149 | Thread err_thread = new Thread() {
150 |
151 | @Override
152 | public void run() {
153 | BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
154 | StringBuilder buf = new StringBuilder();
155 | String line;
156 | try {
157 | while ((line = err.readLine()) != null)
158 | if (!line.trim().equals(""))
159 | buf.append(line).append('\n');
160 | err.close();
161 | msg.output = buf.toString(); // Store output stream
162 | } catch (IOException ex) {
163 | ex.printStackTrace(new PrintStream(System.out));
164 | }
165 | }
166 | };
167 | /*
168 | * Thread to process the STDOUT of gnuplot
169 | */
170 | err_thread.start();
171 | Thread out_thread = new Thread() {
172 |
173 | @Override
174 | public void run() {
175 | msg.process = term.processOutput(proc.getInputStream()); // Execute terminal specific output parsing
176 | }
177 | };
178 | out_thread.start();
179 |
180 | try {
181 | proc.waitFor(); // wait for process to finish
182 | out_thread.join(); // wait for output (terminal related) thread to finish
183 | err_thread.join(); // wait for error (messages) output to finish
184 | } catch (InterruptedException ex) {
185 | throw new GNUPlotException("Interrupted execution of gnuplot");
186 | }
187 | new File(command[1]).delete();
188 |
189 | /*
190 | * Find the error message, if any, with precendence to the error
191 | * thread
192 | */
193 | String message = msg.error != null ? msg.error : msg.process;
194 |
195 | /*
196 | * Determine if error stream should be dumbed or not
197 | */
198 | int level = Debug.VERBOSE;
199 | if (message != null)
200 | level = Debug.ERROR;
201 | GNUPlot.getDebugger().msg("** Start of error stream **", level);
202 | GNUPlot.getDebugger().msg(msg.output, level);
203 | GNUPlot.getDebugger().msg("** End of error stream **", level);
204 |
205 | /*
206 | * Throw an exception if an error occured
207 | */
208 | if (message != null)
209 | throw new GNUPlotException(message);
210 |
211 | } catch (IOException ex) {
212 | throw new GNUPlotException("IOException while executing \"" + getGNUPlotPath() + "\":" + ex.getLocalizedMessage());
213 | }
214 |
215 | }
216 |
217 | void setPersist(boolean persist) {
218 | ispersist = persist;
219 | }
220 |
221 | private class Messages {
222 |
223 | String output = "";
224 | String error = null;
225 | String process = null;
226 | }
227 | }
228 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/GNUPlotParameters.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 13, 2007, 4:02 PM
17 | */
18 | package com.panayotis.gnuplot;
19 |
20 | import com.panayotis.gnuplot.layout.GraphLayout;
21 | import com.panayotis.gnuplot.plot.Axis;
22 | import com.panayotis.gnuplot.plot.Graph;
23 | import com.panayotis.gnuplot.plot.Graph3D;
24 | import com.panayotis.gnuplot.plot.Page;
25 | import com.panayotis.gnuplot.plot.Plot;
26 | import com.panayotis.gnuplot.terminal.GNUPlotTerminal;
27 | import java.io.Serializable;
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | /**
32 | * This is a placeholder for the parameters used to create the actual plot.
33 | *
34 | * @author teras
35 | */
36 | public class GNUPlotParameters extends PropertiesHolder implements Serializable {
37 |
38 | private Page page;
39 | private int defaultgraph;
40 | private List
It is possible to retrieve all it's data, as long
26 | * a valid key-value pair is present.
27 | *
28 | * @author teras
29 | */
30 | public class PropertiesHolder extends HashMap
The prefix in this case is the token "set " and the suffix is
39 | * the newline character.
40 | */
41 | public PropertiesHolder() {
42 | this("set ", NL);
43 | }
44 |
45 | /**
46 | * Creates a new instance of PropertiesHolder with given prefix and suffix
47 | * tokens.
48 | *
49 | * @param prefix The prefix to use
50 | * @param suffix The suffix to use.
51 | */
52 | public PropertiesHolder(String prefix, String suffix) {
53 | super();
54 | this.prefix = prefix;
55 | this.suffix = suffix;
56 | }
57 |
58 | /**
59 | * Add a specific key-value pair to this object.
60 | *
61 | * @param key The key to use
62 | * @param value The value of the specified parameter.
If value is null,
63 | * then this key will be removed.
64 | */
65 | public void set(String key, String value) {
66 | if (key != null)
67 | if (value == null)
68 | unset(key);
69 | else
70 | put(key, value);
71 | }
72 |
73 | /**
74 | * Set a specific key to this object, without a value
75 | *
76 | * @param key The key to add to this object
77 | */
78 | public void set(String key) {
79 | set(key, "");
80 | }
81 |
82 | /**
83 | * Remove a key from this object
84 | *
85 | * @param key The key to be removed
86 | */
87 | public void unset(String key) {
88 | remove(key);
89 | }
90 |
91 | /**
92 | * Retrieve the list of the stored key-value pairs in this object. Every
93 | * pair will be prefixed with "prefix" and suffixed with "suffix". Between
94 | * key and value will be a space character, if and only if the value is
95 | * present.
96 | *
97 | * @param bf The StringBuilder to store the representation of this object.
98 | */
99 | public void appendProperties(StringBuilder bf) {
100 | Object val;
101 | for (Entry e : entrySet()) {
102 | bf.append(prefix).append(e.getKey());
103 | val = e.getValue();
104 | if (val != null && (!val.equals("")))
105 | bf.append(' ').append(e.getValue());
106 | bf.append(suffix);
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/dataset/ArrayDataSet.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on 15 Οκτώβριος 2007, 12:29 μμ
17 | */
18 | package com.panayotis.gnuplot.dataset;
19 |
20 | import java.io.Serializable;
21 |
22 | /**
23 | * Store data sets in a static primitive 2D array
24 | *
25 | * @author teras
26 | */
27 | public class ArrayDataSet implements DataSet, Serializable {
28 |
29 | private String[][] val;
30 |
31 | /**
32 | * Creates a new instance of ArrayDataSet from a double precision 2D array
33 | *
34 | * @param values the 2D array in double precision to retrieve data from
35 | */
36 | public ArrayDataSet(double[][] values) {
37 | int length = values.length;
38 | int dimension = values[0].length;
39 | int i, j;
40 |
41 | if (length == 0)
42 | throw new ArrayStoreException("Array has zero points");
43 | val = new String[length][dimension];
44 | for (i = 0; i < length; i++) {
45 | if (values[i].length != dimension)
46 | throw new ArrayStoreException("Array has not consistent size, was " + dimension + ", found " + values[i].length);
47 | for (j = 0; j < dimension; j++)
48 | val[i][j] = Double.toString(values[i][j]);
49 | }
50 | }
51 |
52 | /**
53 | * Creates a new instance of ArrayDataSet from a float precision 2D array
54 | *
55 | * @param values the 2D array in float precision to retrieve data from
56 | */
57 | public ArrayDataSet(float[][] values) {
58 | int length = values.length;
59 | int dimension = values[0].length;
60 | int i, j;
61 |
62 | if (length == 0)
63 | throw new ArrayStoreException("Array has zero points");
64 | val = new String[length][dimension];
65 | for (i = 0; i < length; i++) {
66 | if (values[i].length != dimension)
67 | throw new ArrayStoreException("Array has not consistent size, was " + dimension + ", found " + values[i].length);
68 | for (j = 0; j < dimension; j++)
69 | val[i][j] = Float.toString(values[i][j]);
70 | }
71 | }
72 |
73 | /**
74 | * Creates a new instance of ArrayDataSet from a int precision 2D array
75 | *
76 | * @param values the 2D array in int precision to retrieve data from
77 | */
78 | public ArrayDataSet(int[][] values) {
79 | int length = values.length;
80 | int dimension = values[0].length;
81 | int i, j;
82 |
83 | if (length == 0)
84 | throw new ArrayStoreException("Array has zero points");
85 | val = new String[length][dimension];
86 | for (i = 0; i < length; i++) {
87 | if (values[i].length != dimension)
88 | throw new ArrayStoreException("Array has not consistent size, was " + dimension + ", found " + values[i].length);
89 | for (j = 0; j < dimension; j++)
90 | val[i][j] = Integer.toString(values[i][j]);
91 | }
92 | }
93 |
94 | /**
95 | * Creates a new instance of ArrayDataSet from a long precision 2D array
96 | *
97 | * @param values the 2D array in long precision to retrieve data from
98 | */
99 | public ArrayDataSet(long[][] values) {
100 | int length = values.length;
101 | int dimension = values[0].length;
102 | int i, j;
103 |
104 | if (length == 0)
105 | throw new ArrayStoreException("Array has zero points");
106 | val = new String[length][dimension];
107 | for (i = 0; i < length; i++) {
108 | if (values[i].length != dimension)
109 | throw new ArrayStoreException("Array has not consistent size, was " + dimension + ", found " + values[i].length);
110 | for (j = 0; j < dimension; j++)
111 | val[i][j] = Long.toString(values[i][j]);
112 | }
113 | }
114 |
115 | /**
116 | * Creates a new instance of ArrayDataSet from a String 2D array. No check
117 | * on the data format is performed, can store any kind of value. Do not use
118 | * this method.
119 | *
120 | * @deprecated
121 | * @param values the 2D array in String format to retrieve data from
122 | */
123 | public ArrayDataSet(String[][] values) {
124 | int length = values.length;
125 | int dimension = values[0].length;
126 | int i, j;
127 |
128 | if (length == 0)
129 | throw new ArrayStoreException("Array has zero points");
130 | val = new String[length][dimension];
131 | for (i = 0; i < length; i++) {
132 | if (values[i].length != dimension)
133 | throw new ArrayStoreException("Array has not consistent size, was " + dimension + ", found " + values[i].length);
134 | System.arraycopy(values, 0, val, 0, values.length);
135 | }
136 | }
137 |
138 | /**
139 | * Retrieve how many points this data set has.
140 | *
141 | * @return the number of points
142 | */
143 | @Override
144 | public int size() {
145 | return val.length;
146 | }
147 |
148 | /**
149 | * Retrieve how many dimensions this dataset refers to.
150 | *
151 | * @return the number of dimensions
152 | * @see DataSet#getDimensions()
153 | */
154 | @Override
155 | public int getDimensions() {
156 | if (val[0] == null)
157 | return -1;
158 | return val[0].length;
159 | }
160 |
161 | /**
162 | * Retrieve data information from a point.
163 | *
164 | * @param point The point number
165 | * @param dimension The point dimension (or "column") to request data from
166 | * @return the point data for this dimension
167 | * @see DataSet#getPointValue(int,int)
168 | */
169 | @Override
170 | public String getPointValue(int point, int dimension) {
171 | return val[point][dimension];
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/dataset/DataSet.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on 15 Οκτώβριος 2007, 12:14 μμ
17 | */
18 | package com.panayotis.gnuplot.dataset;
19 |
20 | /**
21 | * This is the generic interface which every data set object should provide. By
22 | * implementing this interface the author can create his own data objects which
23 | * can be used in JavaPlot
24 | *
25 | * @author teras
26 | */
27 | public interface DataSet {
28 |
29 | /**
30 | * Retrieve how many points this data set has.
31 | *
32 | * @return the number of points
33 | */
34 | public int size();
35 |
36 | /**
37 | * Retrieve how many dimensions this dataset refers to. Typically, for every
38 | * point, this method informs JavaPlot how many "columns" of data this point
39 | * has. Make sure that every point has at least as many dimensions as what
40 | * is reported here .
41 | *
42 | * @return the number of dimensions
43 | */
44 | public int getDimensions();
45 |
46 | /**
47 | * Retrieve data information from a point. To retrieve information for each
48 | * point, a continuous call to this method will be executed, keeping the
49 | * item number constant and increasing the dimension.
50 | *
51 | * @param point The point number
52 | * @param dimension The point dimension (or "column") to request data from
53 | * @return the point data for this dimension
54 | */
55 | public String getPointValue(int point, int dimension);
56 | }
57 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/dataset/FileDataSet.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on October 24, 2007, 2:00 AM
17 | */
18 | package com.panayotis.gnuplot.dataset;
19 |
20 | import java.io.BufferedReader;
21 | import java.io.File;
22 | import java.io.FileReader;
23 | import java.io.IOException;
24 | import java.util.ArrayList;
25 | import java.util.List;
26 | import java.util.StringTokenizer;
27 |
28 | /**
29 | * This object uses data sets already stored in files.
30 | *
31 | * @author teras
32 | */
33 | public class FileDataSet extends GenericDataSet {
34 |
35 | /**
36 | * Creates a new instance of a data set, stored in a file. When this object
37 | * is initialized, the file is read into memory.
38 | *
39 | * @param datafile The file containing the data set
40 | * @throws java.io.IOException when a I/O error is found
41 | * @throws java.lang.ArrayIndexOutOfBoundsException when the file has not
42 | * consistent number of columns
43 | * @throws java.lang.NumberFormatException when the numbers inside the file
44 | * are not parsable
45 | */
46 | public FileDataSet(File datafile) throws IOException, NumberFormatException, ArrayIndexOutOfBoundsException {
47 | super(true);
48 |
49 | BufferedReader in = new BufferedReader(new FileReader(datafile));
50 | String line;
51 | List> data;
39 |
40 | /**
41 | * Create a new instance of GenericDataSet, with the default DataParser
42 | * (DoubleDataParser)
43 | *
44 | * @see com.panayotis.gnuplot.dataset.parser.DoubleDataParser
45 | */
46 | public GenericDataSet() {
47 | this(new DoubleDataParser());
48 | }
49 |
50 | /**
51 | * Create a new instance of GenericDataSet, with the default DataParser
52 | * (DoubleDataParser), and the information that the first column is in date
53 | * format.
54 | *
55 | * @param first_column_date Whether the first column is in date format
56 | */
57 | public GenericDataSet(boolean first_column_date) {
58 | this(new DoubleDataParser(first_column_date));
59 | }
60 |
61 | /**
62 | * Create a new instance of GenericDataSet, with a given DataParser
63 | *
64 | * @param parser The DataParser to use
65 | */
66 | public GenericDataSet(DataParser parser) {
67 | this.parser = parser;
68 | data = new ArrayList
>();
69 | }
70 |
71 | @Override
72 | public int size() {
73 | return data.size();
74 | }
75 |
76 | /**
77 | * Retrieve how many dimensions this dataset refers to.
78 | *
79 | * @return the number of dimensions
80 | * @see DataSet#getDimensions()
81 | */
82 | @Override
83 | public int getDimensions() {
84 | if (size() < 1)
85 | return -1;
86 | return data.get(0).size();
87 | }
88 |
89 | /**
90 | * Retrieve data information from a point.
91 | *
92 | * @param point The point number
93 | * @param dimension The point dimension (or "column") to request data from
94 | * @return the point data for this dimension
95 | * @see DataSet#getPointValue(int,int)
96 | */
97 | @Override
98 | public String getPointValue(int point, int dimension) {
99 | return data.get(point).get(dimension);
100 | }
101 |
102 | /**
103 | * Add a new point to this DataSet
104 | *
105 | * @param point The point to add to this DataSet
106 | * @return Whether the collection changed with this call
107 | * @throws java.lang.NumberFormatException If the given collection is not in
108 | * the correct format
109 | */
110 | public boolean add(List
Note that if we have chosen log scale, then
88 | * the values should be guaranteed to be larger than zero. If the axis is in
89 | * log scale, do not set a value less than zero or else a plot error will
90 | * occur.
91 | *
92 | * @param from The minimum value
93 | * @param to The maximum value
94 | */
95 | public void setBoundaries(double from, double to) {
96 | if (from == Double.POSITIVE_INFINITY || from == Double.NEGATIVE_INFINITY || from == Double.MAX_VALUE || from == Double.MIN_VALUE)
97 | return;
98 | if (to == Double.POSITIVE_INFINITY || to == Double.NEGATIVE_INFINITY || to == Double.MAX_VALUE || to == Double.MIN_VALUE)
99 | return;
100 | if (to < from) {
101 | double swap = to;
102 | to = from;
103 | from = swap;
104 | }
105 | set(getName() + "range", "[" + from + ":" + to + "]");
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/plot/DataSetPlot.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on 12 Οκτώβριος 2007, 4:07 μμ
17 | */
18 | package com.panayotis.gnuplot.plot;
19 |
20 | import com.panayotis.gnuplot.dataset.ArrayDataSet;
21 | import com.panayotis.gnuplot.dataset.DataSet;
22 | import com.panayotis.gnuplot.dataset.PointDataSet;
23 |
24 | /**
25 | * This plot uses data sets as coordinates of the points to e displayed. The
26 | * user can provide data either statically (through the specialized constructors
27 | * with native base types) or with the more flexible generic object
28 | * PointDataSet.
29 | *
30 | * @author teras
31 | */
32 | public class DataSetPlot extends AbstractPlot {
33 |
34 | private DataSet dataset;
35 |
36 | /**
37 | * Create a new data set with a default data set.
38 | */
39 | public DataSetPlot() {
40 | this(new PointDataSet());
41 | }
42 |
43 | /**
44 | * Create a new data set with the specified double-precision array as a data
45 | * set
46 | *
47 | * @param dataset A 2D double table with the data set
48 | */
49 | public DataSetPlot(double[][] dataset) {
50 | this(new ArrayDataSet(dataset));
51 | }
52 |
53 | /**
54 | * Create a new data set with the specified float-precision array as a data
55 | * set
56 | *
57 | * @param dataset A 2D float table with the data set
58 | */
59 | public DataSetPlot(float[][] dataset) {
60 | this(new ArrayDataSet(dataset));
61 | }
62 |
63 | /**
64 | * Create a new data set with the specified integer-precision array as a
65 | * data set
66 | *
67 | * @param dataset A 2D integer table with the data set
68 | */
69 | public DataSetPlot(int[][] dataset) {
70 | this(new ArrayDataSet(dataset));
71 | }
72 |
73 | /**
74 | * Create a new data set with the specified long-precision array as a data
75 | * set
76 | *
77 | * @param dataset A 2D long table with the data set
78 | */
79 | public DataSetPlot(long[][] dataset) {
80 | this(new ArrayDataSet(dataset));
81 | }
82 |
83 | /**
84 | * Create a new object with a specific data set
85 | *
86 | * @param dataset The data set to use
87 | */
88 | public DataSetPlot(DataSet dataset) {
89 | setDataSet(dataset);
90 | setDefinition("'-'");
91 | }
92 |
93 | /**
94 | * Retrieve the data set of this plot command. It is used internally by
95 | * JavaPlot library. Please do not use it.
96 | *
97 | * @param bf The buffer to store the data set
98 | */
99 | @Override
100 | public void retrieveData(StringBuilder bf) {
101 | int i, j;
102 | int isize, jsize;
103 |
104 | if (dataset != null) {
105 | isize = dataset.size();
106 | jsize = dataset.getDimensions();
107 | for (i = 0; i < isize; i++) {
108 | for (j = 0; j < jsize; j++)
109 | bf.append(dataset.getPointValue(i, j)).append(' ');
110 | bf.append(NL);
111 | }
112 | }
113 | bf.append("e").append(NL);
114 | }
115 |
116 | /**
117 | * Set the data set of this plot to the specified one
118 | *
119 | * @param set The data set to use
120 | */
121 | public final void setDataSet(DataSet set) {
122 | dataset = set;
123 | }
124 |
125 | /**
126 | * Retrieve the data set of this plot
127 | *
128 | * @return The data set of this plot
129 | */
130 | public DataSet getDataSet() {
131 | return dataset;
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/plot/FunctionPlot.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | *
16 | * Created on 12 Οκτώβριος 2007, 5:17 μμ
17 | */
18 | package com.panayotis.gnuplot.plot;
19 |
20 | /**
21 | * This type of Plot is used to provide an interface to the functional plots of
22 | * gnuplot. For example plots like sin(x) or x**2+1
It can also be used as
23 | * a generic plot command, if the user wishes to manually provide any plot
24 | * information, without the interference of JavaPlot library.
25 | *
26 | * @author teras
27 | */
28 | public class FunctionPlot extends AbstractPlot {
29 |
30 | /**
31 | * Creates a new instance of FunctionPlot.
32 | *
33 | * @param function The function definition. It is a free text describing the
34 | * function to be plotted. The independent variable (for 2D plots) is x
35 | */
36 | public FunctionPlot(String function) {
37 | if (function == null)
38 | function = "0";
39 | set("title", "'" + function + "'");
40 | setDefinition(function);
41 | }
42 |
43 | /**
44 | * This method is unused in this object. It is here only for compatibility
45 | * reasons with Plot object.
46 | *
47 | * @param buf This parameter is not used
48 | */
49 | @Override
50 | public void retrieveData(StringBuilder buf) {
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/base/src/main/java/com/panayotis/gnuplot/plot/Graph.java:
--------------------------------------------------------------------------------
1 | /* Copyright (c) 2007-2014 by panayotis.com
2 | *
3 | * JavaPlot is free software; you can redistribute it and/or modify
4 | * it under the terms of the GNU Lesser General Public License as published by
5 | * the Free Software Foundation, version 2.
6 | *
7 | * JavaPlot is free in the hope that it will be useful,
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | * GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License
13 | * along with CrossMobile; if not, write to the Free Software
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 | */
16 | package com.panayotis.gnuplot.plot;
17 |
18 | import com.panayotis.gnuplot.layout.LayoutMetrics;
19 | import java.util.ArrayList;
20 | import java.util.HashMap;
21 |
22 | /**
23 | * Graph objects are parts of a multi-plot drawing. Each graph contains other
24 | * plots which share the same axis. All gnuplot objects have at least one graph
25 | * object.
26 | *