KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > model > FunctionPlotter


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     FunctionPlotter.java
21     Created on 7. September 2001, 17:14
22 */

23
24 package de.progra.charting.model;
25
26 import org.nfunk.jep.JEP;
27
28 /**
29  * This class can be used to create a ChartDataModel from a mathematical
30  * function. A Python interpreter implemented in Java is called to calculate
31  * the function values. Therefore, I didn't need to implement a parsing
32  * algorithm. Of course, you need to know the Python syntax for mathematical
33  * functions. The quadratical function <code>5x<sup>2</sup>-3x+4</code> e.g.
34  * would be written as <code>5*x**2-3*x+4</code>. Please note, that it takes
35  * approx. 1 s to plot a quadratical function like this computing 2000 single
36  * values.
37  * @author mueller
38  * @version 1.0
39  */

40 public class FunctionPlotter {
41
42     /** Creates new FunctionPlot */
43     private FunctionPlotter() {}
44     
45     /** This method creates a new ChartDataModel from the computed
46      * function data. In order to compute the needed values, a Python script is
47      * created using the provided parameters and is executed afterwards:<p>
48        <pre><code>
49         String pyfile = "" +
50         "AMOUNT = "+amount+"\n"+
51         "columns = []\n"+
52         "model = []\n"+
53         "lowrange = "+lowrange+"\n"+
54         "highrange = "+highrange+"\n"+
55         "for i in range(AMOUNT) :\n"+
56         " x = lowrange + i * (float(abs(highrange - lowrange)) / AMOUNT)\n"+
57         " columns.append(x)\n"+
58         " model.append("+function+")\n";
59                 
60         interp.exec(pyfile);
61        </code></pre>
62        Finally the values are extracted via the PythonInterpreter:<p>
63        <pre><code>
64        pymodel[0] = (double[])interp.get("model", double[].class);
65        pycolumns = (double[])interp.get("columns", double[].class);
66        </code></pre>
67      * @param lowrange the lower x-value to begin from
68      * @param highrange the highest x-value to reach
69      * @param amount the amount of values that should be computed (2000 is
70      * a good value for smooth curves)
71      * @param function the mathematical function in Python notation
72      * @param rows the array with the DataSet title
73      * @throws IllegalArgumentException if the function contains more than
74      * one variable.
75      */

76     
77     public static DefaultChartDataModel createChartDataModelInstance(double lowrange,
78                                                               double highrange,
79                                                               int amount,
80                                                               String JavaDoc function,
81                                                               String JavaDoc[] rows) {
82         double x = 0.0;
83         JEP jep = new JEP();
84         jep.addStandardConstants();
85         jep.addStandardFunctions();
86         
87         jep.parseExpression(function);
88         
89         String JavaDoc[] vars = jep.getVariables();
90         String JavaDoc var = "x";
91             
92         if(vars.length > 1)
93             throw new IllegalArgumentException JavaDoc("The supplied function contains more than one variable.");
94         
95         if(vars.length == 1)
96             var = vars[0];
97         
98         double[][] model = new double[1][amount];
99         double[] columns = new double[amount];
100                         
101         for(int i = 1; i <= amount; i++) {
102             x = lowrange + i * ((double)Math.abs(highrange - lowrange) / amount);
103             columns[i-1] = x;
104             jep.addVariable(var, x);
105             model[0][i-1] = jep.getValue();
106             //System.out.println("** Calc("+columns[i-1]+") = "+model[0][i-1]);
107
}
108                 
109         return new DefaultChartDataModel(model, columns, rows);
110     }
111     
112     /** This method creates a new ChartDataModel from the computed
113      * function data. This implementation automatically uses the function
114      * as DataSet title.
115      * @param lowrange the lower x-value to begin from
116      * @param hirange the highest x-value to reach
117      * @param amount the amount of values that should be computed (2000 is
118      * a good value for smooth curves)
119      * @param function the mathematical function in Python notation
120      */

121     
122     public static DefaultChartDataModel createChartDataModelInstance(double lowrange,
123                                                               double hirange,
124                                                               int amount,
125                                                               String JavaDoc function) {
126             return createChartDataModelInstance(lowrange, hirange,
127                                                 amount, function,
128                                                 new String JavaDoc[] {function});
129     }
130     
131 }
132
Popular Tags