Example #1
0
  /**
   * Given a JFreeChart dataset and a commons math function, return a JFreeChart dataset in which
   * the original x values are now accompanied by the y values predicted by the function
   *
   * @param data input JFreeChart data set
   * @param type one of the Fitter.FunctionType predefined functions
   * @param parms parameters describing the function. These need to match the selected function or
   *     an IllegalArgumentEception will be thrown
   * @return JFreeChart dataset with original x values and fitted y values.
   */
  public static XYSeries getFittedSeries(XYSeries data, FunctionType type, double[] parms) {
    XYSeries result = new XYSeries(data.getItemCount() * 10);
    double minRange = data.getMinX();
    double maxRange = data.getMaxX();
    double xStep = (maxRange - minRange) / (data.getItemCount() * 10);
    switch (type) {
      case NoFit:
        {
          try {
            XYSeries resCopy = data.createCopy(0, data.getItemCount() - 1);
            return resCopy;
          } catch (CloneNotSupportedException ex) {
            return null;
          }
        }
      case Pol1:
      case Pol2:
      case Pol3:
        checkParms(type, parms);
        PolynomialFunction polFunction = new PolynomialFunction(parms);
        for (int i = 0; i < data.getItemCount() * 10; i++) {
          double x = minRange + i * xStep;
          double y = polFunction.value(x);
          result.add(x, y);
        }
        break;
      case Gaussian:
        checkParms(type, parms);
        Gaussian.Parametric gf = new Gaussian.Parametric();
        for (int i = 0; i < data.getItemCount() * 10; i++) {
          double x = minRange + i * xStep;
          double[] gparms = new double[3];
          System.arraycopy(parms, 0, gparms, 0, 3);
          double y = gf.value(x, gparms) + parms[3];
          result.add(x, y);
        }
        break;
    }

    return result;
  }