1 23 24 package de.progra.charting.model; 25 26 import org.nfunk.jep.JEP; 27 28 40 public class FunctionPlotter { 41 42 43 private FunctionPlotter() {} 44 45 76 77 public static DefaultChartDataModel createChartDataModelInstance(double lowrange, 78 double highrange, 79 int amount, 80 String function, 81 String [] 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 [] vars = jep.getVariables(); 90 String var = "x"; 91 92 if(vars.length > 1) 93 throw new IllegalArgumentException ("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 } 108 109 return new DefaultChartDataModel(model, columns, rows); 110 } 111 112 121 122 public static DefaultChartDataModel createChartDataModelInstance(double lowrange, 123 double hirange, 124 int amount, 125 String function) { 126 return createChartDataModelInstance(lowrange, hirange, 127 amount, function, 128 new String [] {function}); 129 } 130 131 } 132 | Popular Tags |